PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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