123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Symfony\Tests\Component\Validator;
- use Symfony\Component\Validator\Constraints\File;
- use Symfony\Component\Validator\Constraints\FileValidator;
- class FileValidatorTest extends \PHPUnit_Framework_TestCase
- {
- protected $validator;
- protected $path;
- protected $file;
- protected function setUp()
- {
- $this->validator = new FileValidator();
- $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
- $this->file = fopen($this->path, 'w');
- }
- protected function tearDown()
- {
- fclose($this->file);
- }
- public function testNullIsValid()
- {
- $this->assertTrue($this->validator->isValid(null, new File()));
- }
- public function testEmptyStringIsValid()
- {
- $this->assertTrue($this->validator->isValid('', new File()));
- }
- public function testExpectsStringCompatibleTypeOrFile()
- {
- $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
- $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(
- '{{ limit }}' => '10 bytes',
- '{{ size }}' => '11 bytes',
- '{{ file }}' => $this->path,
- ));
- }
- 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(
- '{{ limit }}' => '1 kB',
- '{{ size }}' => '1.4 kB',
- '{{ file }}' => $this->path,
- ));
- }
- 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(
- '{{ limit }}' => '1 MB',
- '{{ size }}' => '1.4 MB',
- '{{ file }}' => $this->path,
- ));
- }
- public function testInvalidMaxSize()
- {
- $constraint = new File(array(
- 'maxSize' => '1abc',
- ));
- $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
- $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(
- '{{ file }}' => 'foobar',
- ));
- }
- public function testValidMimeType()
- {
- $file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
- $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()
- {
- $file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
- $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(
- '{{ type }}' => '"application/pdf"',
- '{{ types }}' => '"image/png", "image/jpg"',
- '{{ file }}' => $this->path,
- ));
- }
- }
|