FileValidatorTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. use Symfony\Component\HttpFoundation\File\File as FileObject;
  14. use Symfony\Component\HttpFoundation\File\UploadedFile;
  15. class FileValidatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $validator;
  18. protected $path;
  19. protected $file;
  20. protected function setUp()
  21. {
  22. $this->validator = new FileValidator();
  23. $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
  24. $this->file = fopen($this->path, 'w');
  25. }
  26. protected function tearDown()
  27. {
  28. fclose($this->file);
  29. $this->validator = null;
  30. $this->path = null;
  31. $this->file = null;
  32. }
  33. public function testNullIsValid()
  34. {
  35. $this->assertTrue($this->validator->isValid(null, new File()));
  36. }
  37. public function testEmptyStringIsValid()
  38. {
  39. $this->assertTrue($this->validator->isValid('', new File()));
  40. }
  41. /**
  42. * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
  43. */
  44. public function testExpectsStringCompatibleTypeOrFile()
  45. {
  46. $this->validator->isValid(new \stdClass(), new File());
  47. }
  48. public function testValidFile()
  49. {
  50. $this->assertTrue($this->validator->isValid($this->path, new File()));
  51. }
  52. public function testValidUploadedfile()
  53. {
  54. $file = new UploadedFile($this->path, 'originalName');
  55. $this->assertTrue($this->validator->isValid($file, new File()));
  56. }
  57. public function testTooLargeBytes()
  58. {
  59. fwrite($this->file, str_repeat('0', 11));
  60. $constraint = new File(array(
  61. 'maxSize' => 10,
  62. 'maxSizeMessage' => 'myMessage',
  63. ));
  64. $this->assertFileValid($this->path, $constraint, false);
  65. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  66. $this->assertEquals($this->validator->getMessageParameters(), array(
  67. '{{ limit }}' => '10 bytes',
  68. '{{ size }}' => '11 bytes',
  69. '{{ file }}' => $this->path,
  70. ));
  71. }
  72. public function testTooLargeKiloBytes()
  73. {
  74. fwrite($this->file, str_repeat('0', 1400));
  75. $constraint = new File(array(
  76. 'maxSize' => '1k',
  77. 'maxSizeMessage' => 'myMessage',
  78. ));
  79. $this->assertFileValid($this->path, $constraint, false);
  80. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  81. $this->assertEquals($this->validator->getMessageParameters(), array(
  82. '{{ limit }}' => '1 kB',
  83. '{{ size }}' => '1.4 kB',
  84. '{{ file }}' => $this->path,
  85. ));
  86. }
  87. public function testTooLargeMegaBytes()
  88. {
  89. fwrite($this->file, str_repeat('0', 1400000));
  90. $constraint = new File(array(
  91. 'maxSize' => '1M',
  92. 'maxSizeMessage' => 'myMessage',
  93. ));
  94. $this->assertFileValid($this->path, $constraint, false);
  95. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  96. $this->assertEquals($this->validator->getMessageParameters(), array(
  97. '{{ limit }}' => '1 MB',
  98. '{{ size }}' => '1.4 MB',
  99. '{{ file }}' => $this->path,
  100. ));
  101. }
  102. /**
  103. * @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
  104. */
  105. public function testInvalidMaxSize()
  106. {
  107. $constraint = new File(array(
  108. 'maxSize' => '1abc',
  109. ));
  110. $this->validator->isValid($this->path, $constraint);
  111. }
  112. public function testFileNotFound()
  113. {
  114. $constraint = new File(array(
  115. 'notFoundMessage' => 'myMessage',
  116. ));
  117. $this->assertFileValid('foobar', $constraint, false);
  118. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  119. $this->assertEquals($this->validator->getMessageParameters(), array(
  120. '{{ file }}' => 'foobar',
  121. ));
  122. }
  123. public function testValidMimeType()
  124. {
  125. $file = $this
  126. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  127. ->disableOriginalConstructor()
  128. ->getMock()
  129. ;
  130. $file
  131. ->expects($this->once())
  132. ->method('getPathname')
  133. ->will($this->returnValue($this->path))
  134. ;
  135. $file
  136. ->expects($this->once())
  137. ->method('getMimeType')
  138. ->will($this->returnValue('image/jpg'))
  139. ;
  140. $constraint = new File(array(
  141. 'mimeTypes' => array('image/png', 'image/jpg'),
  142. ));
  143. $this->assertTrue($this->validator->isValid($file, $constraint));
  144. }
  145. public function testInvalidMimeType()
  146. {
  147. $file = $this
  148. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  149. ->disableOriginalConstructor()
  150. ->getMock()
  151. ;
  152. $file
  153. ->expects($this->once())
  154. ->method('getPathname')
  155. ->will($this->returnValue($this->path))
  156. ;
  157. $file
  158. ->expects($this->exactly(2))
  159. ->method('getMimeType')
  160. ->will($this->returnValue('application/pdf'))
  161. ;
  162. $constraint = new File(array(
  163. 'mimeTypes' => array('image/png', 'image/jpg'),
  164. 'mimeTypesMessage' => 'myMessage',
  165. ));
  166. $this->assertFalse($this->validator->isValid($file, $constraint));
  167. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  168. $this->assertEquals($this->validator->getMessageParameters(), array(
  169. '{{ type }}' => '"application/pdf"',
  170. '{{ types }}' => '"image/png", "image/jpg"',
  171. '{{ file }}' => $this->path,
  172. ));
  173. }
  174. /**
  175. * @dataProvider uploadedFileErrorProvider
  176. */
  177. public function testUploadedFileError($error, $message)
  178. {
  179. $file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
  180. $options[$message] = 'myMessage';
  181. $constraint = new File($options);
  182. $this->assertFalse($this->validator->isValid($file, $constraint));
  183. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  184. }
  185. public function uploadedFileErrorProvider()
  186. {
  187. return array(
  188. array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage'),
  189. array(UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'),
  190. array(UPLOAD_ERR_PARTIAL, 'uploadErrorMessage'),
  191. array(UPLOAD_ERR_NO_TMP_DIR, 'uploadErrorMessage'),
  192. array(UPLOAD_ERR_EXTENSION, 'uploadErrorMessage'),
  193. );
  194. }
  195. protected function assertFileValid($filename, File $constraint, $valid = true)
  196. {
  197. $this->assertEquals($this->validator->isValid($filename, $constraint), $valid);
  198. if (file_exists($filename)) {
  199. $this->assertEquals($this->validator->isValid(new FileObject($filename), $constraint), $valid);
  200. }
  201. }
  202. }