FileValidatorTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraints\File;
  12. use Symfony\Component\Validator\Constraints\FileValidator;
  13. class FileValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected $path;
  17. protected $file;
  18. protected function setUp()
  19. {
  20. $this->validator = new FileValidator();
  21. $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
  22. $this->file = fopen($this->path, 'w');
  23. }
  24. protected function tearDown()
  25. {
  26. fclose($this->file);
  27. }
  28. public function testNullIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid(null, new File()));
  31. }
  32. public function testEmptyStringIsValid()
  33. {
  34. $this->assertTrue($this->validator->isValid('', new File()));
  35. }
  36. public function testExpectsStringCompatibleTypeOrFile()
  37. {
  38. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  39. $this->validator->isValid(new \stdClass(), new File());
  40. }
  41. public function testValidFile()
  42. {
  43. $this->assertTrue($this->validator->isValid($this->path, new File()));
  44. }
  45. public function testTooLargeBytes()
  46. {
  47. fwrite($this->file, str_repeat('0', 11));
  48. $constraint = new File(array(
  49. 'maxSize' => 10,
  50. 'maxSizeMessage' => 'myMessage',
  51. ));
  52. $this->assertFalse($this->validator->isValid($this->path, $constraint));
  53. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  54. $this->assertEquals($this->validator->getMessageParameters(), array(
  55. '{{ limit }}' => '10 bytes',
  56. '{{ size }}' => '11 bytes',
  57. '{{ file }}' => $this->path,
  58. ));
  59. }
  60. public function testTooLargeKiloBytes()
  61. {
  62. fwrite($this->file, str_repeat('0', 1400));
  63. $constraint = new File(array(
  64. 'maxSize' => '1k',
  65. 'maxSizeMessage' => 'myMessage',
  66. ));
  67. $this->assertFalse($this->validator->isValid($this->path, $constraint));
  68. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  69. $this->assertEquals($this->validator->getMessageParameters(), array(
  70. '{{ limit }}' => '1 kB',
  71. '{{ size }}' => '1.4 kB',
  72. '{{ file }}' => $this->path,
  73. ));
  74. }
  75. public function testTooLargeMegaBytes()
  76. {
  77. fwrite($this->file, str_repeat('0', 1400000));
  78. $constraint = new File(array(
  79. 'maxSize' => '1M',
  80. 'maxSizeMessage' => 'myMessage',
  81. ));
  82. $this->assertFalse($this->validator->isValid($this->path, $constraint));
  83. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  84. $this->assertEquals($this->validator->getMessageParameters(), array(
  85. '{{ limit }}' => '1 MB',
  86. '{{ size }}' => '1.4 MB',
  87. '{{ file }}' => $this->path,
  88. ));
  89. }
  90. public function testInvalidMaxSize()
  91. {
  92. $constraint = new File(array(
  93. 'maxSize' => '1abc',
  94. ));
  95. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  96. $this->validator->isValid($this->path, $constraint);
  97. }
  98. public function testFileNotFound()
  99. {
  100. $constraint = new File(array(
  101. 'notFoundMessage' => 'myMessage',
  102. ));
  103. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  104. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  105. $this->assertEquals($this->validator->getMessageParameters(), array(
  106. '{{ file }}' => 'foobar',
  107. ));
  108. }
  109. public function testValidMimeType()
  110. {
  111. $file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
  112. $file->expects($this->any())
  113. ->method('getPath')
  114. ->will($this->returnValue($this->path));
  115. $file->expects($this->any())
  116. ->method('getMimeType')
  117. ->will($this->returnValue('image/jpg'));
  118. $constraint = new File(array(
  119. 'mimeTypes' => array('image/png', 'image/jpg'),
  120. ));
  121. $this->assertTrue($this->validator->isValid($file, $constraint));
  122. }
  123. public function testInvalidMimeType()
  124. {
  125. $file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
  126. $file->expects($this->any())
  127. ->method('getPath')
  128. ->will($this->returnValue($this->path));
  129. $file->expects($this->any())
  130. ->method('getMimeType')
  131. ->will($this->returnValue('application/pdf'));
  132. $constraint = new File(array(
  133. 'mimeTypes' => array('image/png', 'image/jpg'),
  134. 'mimeTypesMessage' => 'myMessage',
  135. ));
  136. $this->assertFalse($this->validator->isValid($file, $constraint));
  137. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  138. $this->assertEquals($this->validator->getMessageParameters(), array(
  139. '{{ type }}' => '"application/pdf"',
  140. '{{ types }}' => '"image/png", "image/jpg"',
  141. '{{ file }}' => $this->path,
  142. ));
  143. }
  144. }