2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\File;
|
|
|
|
use Symfony\Component\Validator\Constraints\FileValidator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
|
|
|
protected $path;
|
|
|
|
protected $file;
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->validator = new FileValidator();
|
|
|
|
$this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
|
|
|
|
$this->file = fopen($this->path, 'w');
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function tearDown()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
fclose($this->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid(null, new File()));
|
|
|
|
}
|
|
|
|
|
2010-12-10 09:47:43 +00:00
|
|
|
public function testEmptyStringIsValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid('', new File()));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testExpectsStringCompatibleTypeOrFile()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator->isValid(new \stdClass(), new File());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidFile()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid($this->path, new File()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLargeBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 11));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'maxSize' => 10,
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ limit }}' => '10 bytes',
|
|
|
|
'{{ size }}' => '11 bytes',
|
|
|
|
'{{ file }}' => $this->path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLargeKiloBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 1400));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'maxSize' => '1k',
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ limit }}' => '1 kB',
|
|
|
|
'{{ size }}' => '1.4 kB',
|
|
|
|
'{{ file }}' => $this->path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLargeMegaBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 1400000));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'maxSize' => '1M',
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->path, $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ limit }}' => '1 MB',
|
|
|
|
'{{ size }}' => '1.4 MB',
|
|
|
|
'{{ file }}' => $this->path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidMaxSize()
|
|
|
|
{
|
|
|
|
$constraint = new File(array(
|
|
|
|
'maxSize' => '1abc',
|
|
|
|
));
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator->isValid($this->path, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileNotFound()
|
|
|
|
{
|
|
|
|
$constraint = new File(array(
|
|
|
|
'notFoundMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid('foobar', $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ file }}' => 'foobar',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidMimeType()
|
|
|
|
{
|
2010-09-09 13:40:40 +01:00
|
|
|
$file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$file->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue($this->path));
|
|
|
|
$file->expects($this->any())
|
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('image/jpg'));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/png', 'image/jpg'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($file, $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidMimeType()
|
|
|
|
{
|
2010-09-09 13:40:40 +01:00
|
|
|
$file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$file->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue($this->path));
|
|
|
|
$file->expects($this->any())
|
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('application/pdf'));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/png', 'image/jpg'),
|
|
|
|
'mimeTypesMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($file, $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ type }}' => '"application/pdf"',
|
|
|
|
'{{ types }}' => '"image/png", "image/jpg"',
|
|
|
|
'{{ file }}' => $this->path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|