PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

https://github.com/Exercise/symfony
PHP | 324 lines | 251 code | 56 blank | 17 comment | 2 complexity | bf7991f737b172f86f01974f83a73ac5 MD5 | raw file
  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\Component\Validator\Tests\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. abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $context;
  18. protected $validator;
  19. protected $path;
  20. protected $file;
  21. protected function setUp()
  22. {
  23. if (!class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
  24. $this->markTestSkipped('The "HttpFoundation" component is not available');
  25. }
  26. $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
  27. $this->validator = new FileValidator();
  28. $this->validator->initialize($this->context);
  29. $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
  30. $this->file = fopen($this->path, 'w');
  31. }
  32. protected function tearDown()
  33. {
  34. fclose($this->file);
  35. $this->context = null;
  36. $this->validator = null;
  37. $this->path = null;
  38. $this->file = null;
  39. }
  40. public function testNullIsValid()
  41. {
  42. $this->context->expects($this->never())
  43. ->method('addViolation');
  44. $this->assertTrue($this->validator->isValid(null, new File()));
  45. }
  46. public function testEmptyStringIsValid()
  47. {
  48. $this->context->expects($this->never())
  49. ->method('addViolation');
  50. $this->assertTrue($this->validator->isValid('', new File()));
  51. }
  52. /**
  53. * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
  54. */
  55. public function testExpectsStringCompatibleTypeOrFile()
  56. {
  57. $this->validator->isValid(new \stdClass(), new File());
  58. }
  59. public function testValidFile()
  60. {
  61. $this->context->expects($this->never())
  62. ->method('addViolation');
  63. $this->assertTrue($this->validator->isValid($this->path, new File()));
  64. }
  65. public function testValidUploadedfile()
  66. {
  67. $this->context->expects($this->never())
  68. ->method('addViolation');
  69. $file = new UploadedFile($this->path, 'originalName');
  70. $this->assertTrue($this->validator->isValid($file, new File()));
  71. }
  72. public function testTooLargeBytes()
  73. {
  74. fwrite($this->file, str_repeat('0', 11));
  75. $constraint = new File(array(
  76. 'maxSize' => 10,
  77. 'maxSizeMessage' => 'myMessage',
  78. ));
  79. $this->context->expects($this->once())
  80. ->method('addViolation')
  81. ->with('myMessage', array(
  82. '{{ limit }}' => '10 bytes',
  83. '{{ size }}' => '11 bytes',
  84. '{{ file }}' => $this->path,
  85. ));
  86. $this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
  87. }
  88. public function testTooLargeKiloBytes()
  89. {
  90. fwrite($this->file, str_repeat('0', 1400));
  91. $constraint = new File(array(
  92. 'maxSize' => '1k',
  93. 'maxSizeMessage' => 'myMessage',
  94. ));
  95. $this->context->expects($this->once())
  96. ->method('addViolation')
  97. ->with('myMessage', array(
  98. '{{ limit }}' => '1 kB',
  99. '{{ size }}' => '1.4 kB',
  100. '{{ file }}' => $this->path,
  101. ));
  102. $this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
  103. }
  104. public function testTooLargeMegaBytes()
  105. {
  106. fwrite($this->file, str_repeat('0', 1400000));
  107. $constraint = new File(array(
  108. 'maxSize' => '1M',
  109. 'maxSizeMessage' => 'myMessage',
  110. ));
  111. $this->context->expects($this->once())
  112. ->method('addViolation')
  113. ->with('myMessage', array(
  114. '{{ limit }}' => '1 MB',
  115. '{{ size }}' => '1.4 MB',
  116. '{{ file }}' => $this->path,
  117. ));
  118. $this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
  119. }
  120. /**
  121. * @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
  122. */
  123. public function testInvalidMaxSize()
  124. {
  125. $constraint = new File(array(
  126. 'maxSize' => '1abc',
  127. ));
  128. $this->validator->isValid($this->path, $constraint);
  129. }
  130. public function testValidMimeType()
  131. {
  132. $file = $this
  133. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  134. ->disableOriginalConstructor()
  135. ->getMock()
  136. ;
  137. $file
  138. ->expects($this->once())
  139. ->method('getPathname')
  140. ->will($this->returnValue($this->path))
  141. ;
  142. $file
  143. ->expects($this->once())
  144. ->method('getMimeType')
  145. ->will($this->returnValue('image/jpg'))
  146. ;
  147. $this->context->expects($this->never())
  148. ->method('addViolation');
  149. $constraint = new File(array(
  150. 'mimeTypes' => array('image/png', 'image/jpg'),
  151. ));
  152. $this->assertTrue($this->validator->isValid($file, $constraint));
  153. }
  154. public function testValidWildcardMimeType()
  155. {
  156. $file = $this
  157. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  158. ->disableOriginalConstructor()
  159. ->getMock()
  160. ;
  161. $file
  162. ->expects($this->once())
  163. ->method('getPathname')
  164. ->will($this->returnValue($this->path))
  165. ;
  166. $file
  167. ->expects($this->once())
  168. ->method('getMimeType')
  169. ->will($this->returnValue('image/jpg'))
  170. ;
  171. $this->context->expects($this->never())
  172. ->method('addViolation');
  173. $constraint = new File(array(
  174. 'mimeTypes' => array('image/*'),
  175. ));
  176. $this->assertTrue($this->validator->isValid($file, $constraint));
  177. }
  178. public function testInvalidMimeType()
  179. {
  180. $file = $this
  181. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  182. ->disableOriginalConstructor()
  183. ->getMock()
  184. ;
  185. $file
  186. ->expects($this->once())
  187. ->method('getPathname')
  188. ->will($this->returnValue($this->path))
  189. ;
  190. $file
  191. ->expects($this->once())
  192. ->method('getMimeType')
  193. ->will($this->returnValue('application/pdf'))
  194. ;
  195. $constraint = new File(array(
  196. 'mimeTypes' => array('image/png', 'image/jpg'),
  197. 'mimeTypesMessage' => 'myMessage',
  198. ));
  199. $this->context->expects($this->once())
  200. ->method('addViolation')
  201. ->with('myMessage', array(
  202. '{{ type }}' => '"application/pdf"',
  203. '{{ types }}' => '"image/png", "image/jpg"',
  204. '{{ file }}' => $this->path,
  205. ));
  206. $this->assertFalse($this->validator->isValid($file, $constraint));
  207. }
  208. public function testInvalidWildcardMimeType()
  209. {
  210. $file = $this
  211. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  212. ->disableOriginalConstructor()
  213. ->getMock()
  214. ;
  215. $file
  216. ->expects($this->once())
  217. ->method('getPathname')
  218. ->will($this->returnValue($this->path))
  219. ;
  220. $file
  221. ->expects($this->once())
  222. ->method('getMimeType')
  223. ->will($this->returnValue('application/pdf'))
  224. ;
  225. $constraint = new File(array(
  226. 'mimeTypes' => array('image/*', 'image/jpg'),
  227. 'mimeTypesMessage' => 'myMessage',
  228. ));
  229. $this->context->expects($this->once())
  230. ->method('addViolation')
  231. ->with('myMessage', array(
  232. '{{ type }}' => '"application/pdf"',
  233. '{{ types }}' => '"image/*", "image/jpg"',
  234. '{{ file }}' => $this->path,
  235. ));
  236. $this->assertFalse($this->validator->isValid($file, $constraint));
  237. }
  238. /**
  239. * @dataProvider uploadedFileErrorProvider
  240. */
  241. public function testUploadedFileError($error, $message, array $params = array())
  242. {
  243. $file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
  244. $constraint = new File(array(
  245. $message => 'myMessage',
  246. ));
  247. $this->context->expects($this->once())
  248. ->method('addViolation')
  249. ->with('myMessage', $params);
  250. $this->assertFalse($this->validator->isValid($file, $constraint));
  251. }
  252. public function uploadedFileErrorProvider()
  253. {
  254. $tests = array(
  255. array(UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'),
  256. array(UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'),
  257. array(UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'),
  258. array(UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'),
  259. array(UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'),
  260. array(UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'),
  261. );
  262. if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
  263. $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array('{{ limit }}' => UploadedFile::getMaxFilesize() . ' bytes'));
  264. }
  265. return $tests;
  266. }
  267. abstract protected function getFile($filename);
  268. }