FileValidatorTest.php 5.1 KB

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