FileValidatorTest.php 6.2 KB

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