PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Validator/File/ImageSizeTest.php

https://github.com/agelang/zf2
PHP | 205 lines | 105 code | 25 blank | 75 comment | 0 complexity | 23b9abc0339d743b763b8305e745a8ee MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validator_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Validator\File;
  25. use Zend\Validator\File;
  26. use Zend\Validator;
  27. /**
  28. * @category Zend
  29. * @package Zend_Validator_File
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Validator
  34. */
  35. class ImageSizeTest extends \PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Ensures that the validator follows expected behavior
  39. *
  40. * @return void
  41. */
  42. public function testBasic()
  43. {
  44. $valuesExpected = array(
  45. array(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000), true),
  46. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), true),
  47. array(array('minwidth' => 150, 'minheight' => 150, 'maxwidth' => 200, 'maxheight' => 200), false),
  48. array(array('minwidth' => 80, 'minheight' => 0, 'maxwidth' => 80, 'maxheight' => 200), true),
  49. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 60, 'maxheight' => 200), false),
  50. array(array('minwidth' => 90, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), false),
  51. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 80), false),
  52. array(array('minwidth' => 0, 'minheight' => 110, 'maxwidth' => 200, 'maxheight' => 140), false)
  53. );
  54. foreach ($valuesExpected as $element) {
  55. $validator = new File\ImageSize($element[0]);
  56. $this->assertEquals(
  57. $element[1],
  58. $validator->isValid(__DIR__ . '/_files/picture.jpg'),
  59. "Tested with " . var_export($element, 1)
  60. );
  61. }
  62. $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  63. $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.jpg'));
  64. $failures = $validator->getMessages();
  65. $this->assertContains('is not readable', $failures['fileImageSizeNotReadable']);
  66. $file['name'] = 'TestName';
  67. $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  68. $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.jpg', $file));
  69. $failures = $validator->getMessages();
  70. $this->assertContains('TestName', $failures['fileImageSizeNotReadable']);
  71. $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  72. $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/badpicture.jpg'));
  73. $failures = $validator->getMessages();
  74. $this->assertContains('could not be detected', $failures['fileImageSizeNotDetected']);
  75. }
  76. /**
  77. * Ensures that getImageMin() returns expected value
  78. *
  79. * @return void
  80. */
  81. public function testGetImageMin()
  82. {
  83. $validator = new File\ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  84. $this->assertEquals(array('minwidth' => 1, 'minheight' => 10), $validator->getImageMin());
  85. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'greater than or equal');
  86. $validator = new File\ImageSize(array('minwidth' => 1000, 'minheight' => 100, 'maxwidth' => 10, 'maxheight' => 1));
  87. }
  88. /**
  89. * Ensures that setImageMin() returns expected value
  90. *
  91. * @return void
  92. */
  93. public function testSetImageMin()
  94. {
  95. $validator = new File\ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  96. $validator->setImageMin(array('minwidth' => 10, 'minheight' => 10));
  97. $this->assertEquals(array('minwidth' => 10, 'minheight' => 10), $validator->getImageMin());
  98. $validator->setImageMin(array('minwidth' => 9, 'minheight' => 100));
  99. $this->assertEquals(array('minwidth' => 9, 'minheight' => 100), $validator->getImageMin());
  100. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'less than or equal');
  101. $validator->setImageMin(array('minwidth' => 20000, 'minheight' => 20000));
  102. }
  103. /**
  104. * Ensures that getImageMax() returns expected value
  105. *
  106. * @return void
  107. */
  108. public function testGetImageMax()
  109. {
  110. $validator = new File\ImageSize(array('minwidth' => 10, 'minheight' => 100, 'maxwidth' => 1000, 'maxheight' => 10000));
  111. $this->assertEquals(array('maxwidth' => 1000, 'maxheight' => 10000), $validator->getImageMax());
  112. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'greater than or equal');
  113. $validator = new File\ImageSize(array('minwidth' => 10000, 'minheight' => 1000, 'maxwidth' => 100, 'maxheight' => 10));
  114. }
  115. /**
  116. * Ensures that setImageMax() returns expected value
  117. *
  118. * @return void
  119. */
  120. public function testSetImageMax()
  121. {
  122. $validator = new File\ImageSize(array('minwidth' => 10, 'minheight' => 100, 'maxwidth' => 1000, 'maxheight' => 10000));
  123. $validator->setImageMax(array('maxwidth' => 100, 'maxheight' => 100));
  124. $this->assertEquals(array('maxwidth' => 100, 'maxheight' => 100), $validator->getImageMax());
  125. $validator->setImageMax(array('maxwidth' => 110, 'maxheight' => 1000));
  126. $this->assertEquals(array('maxwidth' => 110, 'maxheight' => 1000), $validator->getImageMax());
  127. $validator->setImageMax(array('maxheight' => 1100));
  128. $this->assertEquals(array('maxwidth' => 110, 'maxheight' => 1100), $validator->getImageMax());
  129. $validator->setImageMax(array('maxwidth' => 120));
  130. $this->assertEquals(array('maxwidth' => 120, 'maxheight' => 1100), $validator->getImageMax());
  131. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'greater than or equal');
  132. $validator->setImageMax(array('maxwidth' => 10000, 'maxheight' => 1));
  133. }
  134. /**
  135. * Ensures that getImageWidth() returns expected value
  136. *
  137. * @return void
  138. */
  139. public function testGetImageWidth()
  140. {
  141. $validator = new File\ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  142. $this->assertEquals(array('minwidth' => 1, 'maxwidth' => 100), $validator->getImageWidth());
  143. }
  144. /**
  145. * Ensures that setImageWidth() returns expected value
  146. *
  147. * @return void
  148. */
  149. public function testSetImageWidth()
  150. {
  151. $validator = new File\ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  152. $validator->setImageWidth(array('minwidth' => 2000, 'maxwidth' => 2200));
  153. $this->assertEquals(array('minwidth' => 2000, 'maxwidth' => 2200), $validator->getImageWidth());
  154. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'less than or equal');
  155. $validator->setImageWidth(array('minwidth' => 20000, 'maxwidth' => 200));
  156. }
  157. /**
  158. * Ensures that getImageHeight() returns expected value
  159. *
  160. * @return void
  161. */
  162. public function testGetImageHeight()
  163. {
  164. $validator = new File\ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  165. $this->assertEquals(array('minheight' => 10, 'maxheight' => 1000), $validator->getImageHeight());
  166. }
  167. /**
  168. * Ensures that setImageHeight() returns expected value
  169. *
  170. * @return void
  171. */
  172. public function testSetImageHeight()
  173. {
  174. $validator = new File\ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  175. $validator->setImageHeight(array('minheight' => 2000, 'maxheight' => 2200));
  176. $this->assertEquals(array('minheight' => 2000, 'maxheight' => 2200), $validator->getImageHeight());
  177. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'less than or equal');
  178. $validator->setImageHeight(array('minheight' => 20000, 'maxheight' => 200));
  179. }
  180. }