/tests/Zend/Validate/File/ImageSizeTest.php

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