ImageValidatorTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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\Image;
  12. use Symfony\Component\Validator\Constraints\ImageValidator;
  13. use Symfony\Component\Validator\Validation;
  14. /**
  15. * @requires extension fileinfo
  16. */
  17. class ImageValidatorTest extends AbstractConstraintValidatorTest
  18. {
  19. protected $context;
  20. /**
  21. * @var ImageValidator
  22. */
  23. protected $validator;
  24. protected $path;
  25. protected $image;
  26. protected $imageLandscape;
  27. protected $imagePortrait;
  28. protected $image4By3;
  29. protected function getApiVersion()
  30. {
  31. return Validation::API_VERSION_2_5;
  32. }
  33. protected function createValidator()
  34. {
  35. return new ImageValidator();
  36. }
  37. protected function setUp()
  38. {
  39. parent::setUp();
  40. $this->image = __DIR__.'/Fixtures/test.gif';
  41. $this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
  42. $this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
  43. $this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
  44. }
  45. public function testNullIsValid()
  46. {
  47. $this->validator->validate(null, new Image());
  48. $this->assertNoViolation();
  49. }
  50. public function testEmptyStringIsValid()
  51. {
  52. $this->validator->validate('', new Image());
  53. $this->assertNoViolation();
  54. }
  55. public function testValidImage()
  56. {
  57. $this->validator->validate($this->image, new Image());
  58. $this->assertNoViolation();
  59. }
  60. public function testFileNotFound()
  61. {
  62. // Check that the logic from FileValidator still works
  63. $constraint = new Image(array(
  64. 'notFoundMessage' => 'myMessage',
  65. ));
  66. $this->validator->validate('foobar', $constraint);
  67. $this->buildViolation('myMessage')
  68. ->setParameter('{{ file }}', '"foobar"')
  69. ->setCode(Image::NOT_FOUND_ERROR)
  70. ->assertRaised();
  71. }
  72. public function testValidSize()
  73. {
  74. $constraint = new Image(array(
  75. 'minWidth' => 1,
  76. 'maxWidth' => 2,
  77. 'minHeight' => 1,
  78. 'maxHeight' => 2,
  79. ));
  80. $this->validator->validate($this->image, $constraint);
  81. $this->assertNoViolation();
  82. }
  83. public function testWidthTooSmall()
  84. {
  85. $constraint = new Image(array(
  86. 'minWidth' => 3,
  87. 'minWidthMessage' => 'myMessage',
  88. ));
  89. $this->validator->validate($this->image, $constraint);
  90. $this->buildViolation('myMessage')
  91. ->setParameter('{{ width }}', '2')
  92. ->setParameter('{{ min_width }}', '3')
  93. ->setCode(Image::TOO_NARROW_ERROR)
  94. ->assertRaised();
  95. }
  96. public function testWidthTooBig()
  97. {
  98. $constraint = new Image(array(
  99. 'maxWidth' => 1,
  100. 'maxWidthMessage' => 'myMessage',
  101. ));
  102. $this->validator->validate($this->image, $constraint);
  103. $this->buildViolation('myMessage')
  104. ->setParameter('{{ width }}', '2')
  105. ->setParameter('{{ max_width }}', '1')
  106. ->setCode(Image::TOO_WIDE_ERROR)
  107. ->assertRaised();
  108. }
  109. public function testHeightTooSmall()
  110. {
  111. $constraint = new Image(array(
  112. 'minHeight' => 3,
  113. 'minHeightMessage' => 'myMessage',
  114. ));
  115. $this->validator->validate($this->image, $constraint);
  116. $this->buildViolation('myMessage')
  117. ->setParameter('{{ height }}', '2')
  118. ->setParameter('{{ min_height }}', '3')
  119. ->setCode(Image::TOO_LOW_ERROR)
  120. ->assertRaised();
  121. }
  122. public function testHeightTooBig()
  123. {
  124. $constraint = new Image(array(
  125. 'maxHeight' => 1,
  126. 'maxHeightMessage' => 'myMessage',
  127. ));
  128. $this->validator->validate($this->image, $constraint);
  129. $this->buildViolation('myMessage')
  130. ->setParameter('{{ height }}', '2')
  131. ->setParameter('{{ max_height }}', '1')
  132. ->setCode(Image::TOO_HIGH_ERROR)
  133. ->assertRaised();
  134. }
  135. /**
  136. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  137. */
  138. public function testInvalidMinWidth()
  139. {
  140. $constraint = new Image(array(
  141. 'minWidth' => '1abc',
  142. ));
  143. $this->validator->validate($this->image, $constraint);
  144. }
  145. /**
  146. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  147. */
  148. public function testInvalidMaxWidth()
  149. {
  150. $constraint = new Image(array(
  151. 'maxWidth' => '1abc',
  152. ));
  153. $this->validator->validate($this->image, $constraint);
  154. }
  155. /**
  156. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  157. */
  158. public function testInvalidMinHeight()
  159. {
  160. $constraint = new Image(array(
  161. 'minHeight' => '1abc',
  162. ));
  163. $this->validator->validate($this->image, $constraint);
  164. }
  165. /**
  166. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  167. */
  168. public function testInvalidMaxHeight()
  169. {
  170. $constraint = new Image(array(
  171. 'maxHeight' => '1abc',
  172. ));
  173. $this->validator->validate($this->image, $constraint);
  174. }
  175. public function testRatioTooSmall()
  176. {
  177. $constraint = new Image(array(
  178. 'minRatio' => 2,
  179. 'minRatioMessage' => 'myMessage',
  180. ));
  181. $this->validator->validate($this->image, $constraint);
  182. $this->buildViolation('myMessage')
  183. ->setParameter('{{ ratio }}', 1)
  184. ->setParameter('{{ min_ratio }}', 2)
  185. ->setCode(Image::RATIO_TOO_SMALL_ERROR)
  186. ->assertRaised();
  187. }
  188. public function testRatioTooBig()
  189. {
  190. $constraint = new Image(array(
  191. 'maxRatio' => 0.5,
  192. 'maxRatioMessage' => 'myMessage',
  193. ));
  194. $this->validator->validate($this->image, $constraint);
  195. $this->buildViolation('myMessage')
  196. ->setParameter('{{ ratio }}', 1)
  197. ->setParameter('{{ max_ratio }}', 0.5)
  198. ->setCode(Image::RATIO_TOO_BIG_ERROR)
  199. ->assertRaised();
  200. }
  201. public function testMaxRatioUsesTwoDecimalsOnly()
  202. {
  203. $constraint = new Image(array(
  204. 'maxRatio' => 1.33,
  205. ));
  206. $this->validator->validate($this->image4By3, $constraint);
  207. $this->assertNoViolation();
  208. }
  209. /**
  210. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  211. */
  212. public function testInvalidMinRatio()
  213. {
  214. $constraint = new Image(array(
  215. 'minRatio' => '1abc',
  216. ));
  217. $this->validator->validate($this->image, $constraint);
  218. }
  219. /**
  220. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  221. */
  222. public function testInvalidMaxRatio()
  223. {
  224. $constraint = new Image(array(
  225. 'maxRatio' => '1abc',
  226. ));
  227. $this->validator->validate($this->image, $constraint);
  228. }
  229. public function testSquareNotAllowed()
  230. {
  231. $constraint = new Image(array(
  232. 'allowSquare' => false,
  233. 'allowSquareMessage' => 'myMessage',
  234. ));
  235. $this->validator->validate($this->image, $constraint);
  236. $this->buildViolation('myMessage')
  237. ->setParameter('{{ width }}', 2)
  238. ->setParameter('{{ height }}', 2)
  239. ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
  240. ->assertRaised();
  241. }
  242. public function testLandscapeNotAllowed()
  243. {
  244. $constraint = new Image(array(
  245. 'allowLandscape' => false,
  246. 'allowLandscapeMessage' => 'myMessage',
  247. ));
  248. $this->validator->validate($this->imageLandscape, $constraint);
  249. $this->buildViolation('myMessage')
  250. ->setParameter('{{ width }}', 2)
  251. ->setParameter('{{ height }}', 1)
  252. ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
  253. ->assertRaised();
  254. }
  255. public function testPortraitNotAllowed()
  256. {
  257. $constraint = new Image(array(
  258. 'allowPortrait' => false,
  259. 'allowPortraitMessage' => 'myMessage',
  260. ));
  261. $this->validator->validate($this->imagePortrait, $constraint);
  262. $this->buildViolation('myMessage')
  263. ->setParameter('{{ width }}', 1)
  264. ->setParameter('{{ height }}', 2)
  265. ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
  266. ->assertRaised();
  267. }
  268. }