FileValidatorTest.php 6.1 KB

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