ImageTest.php 10 KB

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