2
0

ImageTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_Barcode
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. require_once dirname(__FILE__) . '/TestCommon.php';
  23. require_once 'Zend/Barcode/Renderer/Image.php';
  24. require_once 'Zend/Barcode/Object/Code39.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Barcode
  28. * @subpackage UnitTests
  29. * @group Zend_Barcode
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
  34. {
  35. public function setUp()
  36. {
  37. if (!extension_loaded('gd')) {
  38. $this->markTestSkipped('Zend_Barcode_Renderer_ImageTest requires the GD extension');
  39. }
  40. parent::setUp();
  41. }
  42. protected function _getRendererObject($options = null)
  43. {
  44. return new Zend_Barcode_Renderer_Image($options);
  45. }
  46. public function testType()
  47. {
  48. $this->assertSame('image', $this->_renderer->getType());
  49. }
  50. public function testGoodImageResource()
  51. {
  52. $imageResource = imagecreatetruecolor(1, 1);
  53. $this->_renderer->setResource($imageResource);
  54. }
  55. /**
  56. * @expectedException Zend_Barcode_Renderer_Exception
  57. */
  58. public function testObjectImageResource()
  59. {
  60. $imageResource = new StdClass();
  61. $this->_renderer->setResource($imageResource);
  62. }
  63. public function testGoodHeight()
  64. {
  65. $this->assertSame(0, $this->_renderer->getHeight());
  66. $this->_renderer->setHeight(123);
  67. $this->assertSame(123, $this->_renderer->getHeight());
  68. $this->_renderer->setHeight(0);
  69. $this->assertSame(0, $this->_renderer->getHeight());
  70. }
  71. /**
  72. * @expectedException Zend_Barcode_Renderer_Exception
  73. */
  74. public function testBadHeight()
  75. {
  76. $this->_renderer->setHeight(- 1);
  77. }
  78. public function testGoodWidth()
  79. {
  80. $this->assertSame(0, $this->_renderer->getWidth());
  81. $this->_renderer->setWidth(123);
  82. $this->assertSame(123, $this->_renderer->getWidth());
  83. $this->_renderer->setWidth(0);
  84. $this->assertSame(0, $this->_renderer->getWidth());
  85. }
  86. /**
  87. * @expectedException Zend_Barcode_Renderer_Exception
  88. */
  89. public function testBadWidth()
  90. {
  91. $this->_renderer->setWidth(- 1);
  92. }
  93. public function testAllowedImageType()
  94. {
  95. $types = array('gif' => 'gif' , 'jpg' => 'jpeg' , 'jpeg' => 'jpeg' ,
  96. 'png' => 'png');
  97. foreach ($types as $type => $expectedType) {
  98. $this->_renderer->setImageType($type);
  99. $this->assertSame($expectedType,
  100. $this->_renderer->getImageType());
  101. }
  102. }
  103. /**
  104. * @expectedException Zend_Barcode_Renderer_Exception
  105. */
  106. public function testNonAllowedImageType()
  107. {
  108. $this->_renderer->setImageType('other');
  109. }
  110. public function testDrawReturnResource()
  111. {
  112. $this->_checkTTFRequirement();
  113. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  114. $this->_renderer->setBarcode($barcode);
  115. $resource = $this->_renderer->draw();
  116. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  117. $this->assertTrue(get_resource_type($resource) == 'gd',
  118. 'Image must be a GD resource');
  119. }
  120. public function testDrawWithExistantResourceReturnResource()
  121. {
  122. $this->_checkTTFRequirement();
  123. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  124. $this->_renderer->setBarcode($barcode);
  125. $imageResource = imagecreatetruecolor(500, 500);
  126. $this->_renderer->setResource($imageResource);
  127. $resource = $this->_renderer->draw();
  128. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  129. $this->assertTrue(get_resource_type($resource) == 'gd',
  130. 'Image must be a GD resource');
  131. $this->assertSame($resource, $imageResource);
  132. }
  133. public function testGoodUserHeight()
  134. {
  135. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  136. $this->assertEquals(62, $barcode->getHeight());
  137. $this->_renderer->setBarcode($barcode);
  138. $this->_renderer->setHeight(62);
  139. $this->assertTrue($this->_renderer->checkParams());
  140. }
  141. /**
  142. * @expectedException Zend_Barcode_Renderer_Exception
  143. */
  144. public function testBadUserHeightLessThanBarcodeHeight()
  145. {
  146. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  147. $this->assertEquals(62, $barcode->getHeight());
  148. $this->_renderer->setBarcode($barcode);
  149. $this->_renderer->setHeight(61);
  150. $this->_renderer->checkParams();
  151. }
  152. public function testGoodUserWidth()
  153. {
  154. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  155. $this->assertEquals(211, $barcode->getWidth());
  156. $this->_renderer->setBarcode($barcode);
  157. $this->_renderer->setWidth(211);
  158. $this->assertTrue($this->_renderer->checkParams());
  159. }
  160. /**
  161. * @expectedException Zend_Barcode_Renderer_Exception
  162. */
  163. public function testBadUserWidthLessThanBarcodeWidth()
  164. {
  165. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  166. $this->assertEquals(211, $barcode->getWidth());
  167. $this->_renderer->setBarcode($barcode);
  168. $this->_renderer->setWidth(210);
  169. $this->_renderer->checkParams();
  170. }
  171. public function testGoodHeightOfUserResource()
  172. {
  173. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  174. $this->assertEquals(62, $barcode->getHeight());
  175. $imageResource = imagecreatetruecolor(500, 62);
  176. $this->_renderer->setResource($imageResource);
  177. $this->_renderer->setBarcode($barcode);
  178. $this->assertTrue($this->_renderer->checkParams());
  179. }
  180. /**
  181. * @expectedException Zend_Barcode_Renderer_Exception
  182. */
  183. public function testBadHeightOfUserResource()
  184. {
  185. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  186. $this->assertEquals(62, $barcode->getHeight());
  187. $this->_renderer->setBarcode($barcode);
  188. $imageResource = imagecreatetruecolor(500, 61);
  189. $this->_renderer->setResource($imageResource);
  190. $this->_renderer->checkParams();
  191. }
  192. public function testGoodWidthOfUserResource()
  193. {
  194. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  195. $this->assertEquals(211, $barcode->getWidth());
  196. $imageResource = imagecreatetruecolor(211, 500);
  197. $this->_renderer->setResource($imageResource);
  198. $this->_renderer->setBarcode($barcode);
  199. $this->assertTrue($this->_renderer->checkParams());
  200. }
  201. /**
  202. * @expectedException Zend_Barcode_Renderer_Exception
  203. */
  204. public function testBadWidthOfUserResource()
  205. {
  206. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  207. $this->assertEquals(211, $barcode->getWidth());
  208. $this->_renderer->setBarcode($barcode);
  209. $imageResource = imagecreatetruecolor(210, 500);
  210. $this->_renderer->setResource($imageResource);
  211. $this->_renderer->checkParams();
  212. }
  213. /**
  214. * @expectedException Zend_Barcode_Renderer_Exception
  215. */
  216. public function testNoFontWithOrientation()
  217. {
  218. Zend_Barcode::setBarcodeFont('');
  219. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  220. $barcode->setOrientation(1);
  221. $this->_renderer->setBarcode($barcode);
  222. $this->_renderer->draw();
  223. }
  224. protected function _getRendererWithWidth500AndHeight300()
  225. {
  226. return $this->_renderer->setHeight(300)->setWidth(500);
  227. }
  228. /**
  229. * @expectedException Zend_Barcode_Renderer_Exception
  230. */
  231. public function testRendererWithUnknownInstructionProvideByObject()
  232. {
  233. parent::testRendererWithUnknownInstructionProvideByObject();
  234. }
  235. public function testHorizontalPositionToLeft()
  236. {
  237. $this->_checkTTFRequirement();
  238. parent::testHorizontalPositionToLeft();
  239. }
  240. public function testHorizontalPositionToCenter()
  241. {
  242. $this->_checkTTFRequirement();
  243. parent::testHorizontalPositionToCenter();
  244. }
  245. public function testHorizontalPositionToRight()
  246. {
  247. $this->_checkTTFRequirement();
  248. parent::testHorizontalPositionToRight();
  249. }
  250. public function testVerticalPositionToTop()
  251. {
  252. $this->_checkTTFRequirement();
  253. parent::testVerticalPositionToTop();
  254. }
  255. public function testVerticalPositionToMiddle()
  256. {
  257. $this->_checkTTFRequirement();
  258. parent::testVerticalPositionToMiddle();
  259. }
  260. public function testVerticalPositionToBottom()
  261. {
  262. $this->_checkTTFRequirement();
  263. parent::testVerticalPositionToBottom();
  264. }
  265. public function testLeftOffsetOverrideHorizontalPosition()
  266. {
  267. $this->_checkTTFRequirement();
  268. parent::testLeftOffsetOverrideHorizontalPosition();
  269. }
  270. public function testTopOffsetOverrideVerticalPosition()
  271. {
  272. $this->_checkTTFRequirement();
  273. parent::testTopOffsetOverrideVerticalPosition();
  274. }
  275. protected function _checkTTFRequirement()
  276. {
  277. if (!function_exists('imagettfbbox')) {
  278. $this->markTestSkipped('TTF (FreeType) support is required in order to run this test');
  279. }
  280. }
  281. }