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

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

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