PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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