ImageSizeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_Validate_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ImageSizeTest.php 12004 2008-10-18 14:29:41Z mikaelkael $
  21. */
  22. // Call Zend_Validate_File_ImageSizeTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_ImageSizeTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_ImageSize
  32. */
  33. require_once 'Zend/Validate/File/ImageSize.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Validate_File
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Validate_File_ImageSizeTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_ImageSizeTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Ensures that the validator follows expected behavior
  55. *
  56. * @return void
  57. */
  58. public function testBasic()
  59. {
  60. $valuesExpected = array(
  61. array(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000), true),
  62. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), true),
  63. array(array('minwidth' => 150, 'minheight' => 150, 'maxwidth' => 200, 'maxheight' => 200), false),
  64. array(array('minwidth' => 80, 'minheight' => 0, 'maxwidth' => 80, 'maxheight' => 200), true),
  65. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 60, 'maxheight' => 200), false),
  66. array(array('minwidth' => 90, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), false),
  67. array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 80), false),
  68. array(array('minwidth' => 0, 'minheight' => 110, 'maxwidth' => 200, 'maxheight' => 140), false)
  69. );
  70. foreach ($valuesExpected as $element) {
  71. $validator = new Zend_Validate_File_ImageSize($element[0]);
  72. $this->assertEquals(
  73. $element[1],
  74. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  75. "Tested with " . var_export($element, 1)
  76. );
  77. }
  78. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  79. $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/nofile.jpg'));
  80. $failures = $validator->getMessages();
  81. $this->assertContains('can not be read', $failures['fileImageSizeNotReadable']);
  82. $file['name'] = 'TestName';
  83. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  84. $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/nofile.jpg', $file));
  85. $failures = $validator->getMessages();
  86. $this->assertContains('TestName', $failures['fileImageSizeNotReadable']);
  87. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000));
  88. $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/badpicture.jpg'));
  89. $failures = $validator->getMessages();
  90. $this->assertContains('could not be detected', $failures['fileImageSizeNotDetected']);
  91. }
  92. /**
  93. * Ensures that getImageMin() returns expected value
  94. *
  95. * @return void
  96. */
  97. public function testGetImageMin()
  98. {
  99. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  100. $this->assertEquals(array('minwidth' => 1, 'minheight' => 10), $validator->getImageMin());
  101. try {
  102. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 1000, 'minheight' => 100, 'maxwidth' => 10, 'maxheight' => 1));
  103. $this->fail("Missing exception");
  104. } catch (Zend_Validate_Exception $e) {
  105. $this->assertContains("greater than or equal", $e->getMessage());
  106. }
  107. }
  108. /**
  109. * Ensures that setImageMin() returns expected value
  110. *
  111. * @return void
  112. */
  113. public function testSetImageMin()
  114. {
  115. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  116. $validator->setImageMin(array('minwidth' => 10, 'minheight' => 10));
  117. $this->assertEquals(array('minwidth' => 10, 'minheight' => 10), $validator->getImageMin());
  118. $validator->setImageMin(array('minwidth' => 9, 'minheight' => 100));
  119. $this->assertEquals(array('minwidth' => 9, 'minheight' => 100), $validator->getImageMin());
  120. try {
  121. $validator->setImageMin(array('minwidth' => 20000, 'minheight' => 20000));
  122. $this->fail("Missing exception");
  123. } catch (Zend_Validate_Exception $e) {
  124. $this->assertContains("less than or equal", $e->getMessage());
  125. }
  126. }
  127. /**
  128. * Ensures that getImageMax() returns expected value
  129. *
  130. * @return void
  131. */
  132. public function testGetImageMax()
  133. {
  134. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 10, 'minheight' => 100, 'maxwidth' => 1000, 'maxheight' => 10000));
  135. $this->assertEquals(array('maxwidth' => 1000, 'maxheight' => 10000), $validator->getImageMax());
  136. try {
  137. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 10000, 'minheight' => 1000, 'maxwidth' => 100, 'maxheight' => 10));
  138. $this->fail("Missing exception");
  139. } catch (Zend_Validate_Exception $e) {
  140. $this->assertContains("greater than or equal", $e->getMessage());
  141. }
  142. }
  143. /**
  144. * Ensures that setImageMax() returns expected value
  145. *
  146. * @return void
  147. */
  148. public function testSetImageMax()
  149. {
  150. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 10, 'minheight' => 100, 'maxwidth' => 1000, 'maxheight' => 10000));
  151. $validator->setImageMax(array('maxwidth' => 100, 'maxheight' => 100));
  152. $this->assertEquals(array('maxwidth' => 100, 'maxheight' => 100), $validator->getImageMax());
  153. $validator->setImageMax(array('maxwidth' => 110, 'maxheight' => 1000));
  154. $this->assertEquals(array('maxwidth' => 110, 'maxheight' => 1000), $validator->getImageMax());
  155. $validator->setImageMax(array('maxheight' => 1100));
  156. $this->assertEquals(array('maxwidth' => 110, 'maxheight' => 1100), $validator->getImageMax());
  157. $validator->setImageMax(array('maxwidth' => 120));
  158. $this->assertEquals(array('maxwidth' => 120, 'maxheight' => 1100), $validator->getImageMax());
  159. try {
  160. $validator->setImageMax(array('maxwidth' => 10000, 'maxheight' => 1));
  161. $this->fail("Missing exception");
  162. } catch (Zend_Validate_Exception $e) {
  163. $this->assertContains("greater than or equal", $e->getMessage());
  164. }
  165. }
  166. /**
  167. * Ensures that getImageWidth() returns expected value
  168. *
  169. * @return void
  170. */
  171. public function testGetImageWidth()
  172. {
  173. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  174. $this->assertEquals(array('minwidth' => 1, 'maxwidth' => 100), $validator->getImageWidth());
  175. }
  176. /**
  177. * Ensures that setImageWidth() returns expected value
  178. *
  179. * @return void
  180. */
  181. public function testSetImageWidth()
  182. {
  183. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  184. $validator->setImageWidth(array('minwidth' => 2000, 'maxwidth' => 2200));
  185. $this->assertEquals(array('minwidth' => 2000, 'maxwidth' => 2200), $validator->getImageWidth());
  186. try {
  187. $validator->setImageWidth(array('minwidth' => 20000, 'maxwidth' => 200));
  188. $this->fail("Missing exception");
  189. } catch (Zend_Validate_Exception $e) {
  190. $this->assertContains("less than or equal", $e->getMessage());
  191. }
  192. }
  193. /**
  194. * Ensures that getImageHeight() returns expected value
  195. *
  196. * @return void
  197. */
  198. public function testGetImageHeight()
  199. {
  200. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 1, 'minheight' => 10, 'maxwidth' => 100, 'maxheight' => 1000));
  201. $this->assertEquals(array('minheight' => 10, 'maxheight' => 1000), $validator->getImageHeight());
  202. }
  203. /**
  204. * Ensures that setImageHeight() returns expected value
  205. *
  206. * @return void
  207. */
  208. public function testSetImageHeight()
  209. {
  210. $validator = new Zend_Validate_File_ImageSize(array('minwidth' => 100, 'minheight' => 1000, 'maxwidth' => 10000, 'maxheight' => 100000));
  211. $validator->setImageHeight(array('minheight' => 2000, 'maxheight' => 2200));
  212. $this->assertEquals(array('minheight' => 2000, 'maxheight' => 2200), $validator->getImageHeight());
  213. try {
  214. $validator->setImageHeight(array('minheight' => 20000, 'maxheight' => 200));
  215. $this->fail("Missing exception");
  216. } catch (Zend_Validate_Exception $e) {
  217. $this->assertContains("less than or equal", $e->getMessage());
  218. }
  219. }
  220. }