/tests/ZendTest/Barcode/Renderer/ImageTest.php

https://bitbucket.org/gencer/zf2 · PHP · 357 lines · 276 code · 55 blank · 26 comment · 6 complexity · 970559cbbf2bcf3a9f253443eda53c76 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Barcode\Renderer;
  10. use Zend\Barcode;
  11. use Zend\Barcode\Object;
  12. use Zend\Barcode\Renderer as RendererNS;
  13. /**
  14. * @group Zend_Barcode
  15. */
  16. class ImageTest extends TestCommon
  17. {
  18. public function setUp()
  19. {
  20. if (!extension_loaded('gd')) {
  21. $this->markTestSkipped('\ZendTest\Barcode\Renderer\ImageTest requires the GD extension');
  22. }
  23. parent::setUp();
  24. }
  25. protected function getRendererObject($options = null)
  26. {
  27. return new RendererNS\Image($options);
  28. }
  29. public function testType()
  30. {
  31. $this->assertSame('image', $this->renderer->getType());
  32. }
  33. public function testGoodImageResource()
  34. {
  35. $imageResource = imagecreatetruecolor(1, 1);
  36. $this->renderer->setResource($imageResource);
  37. }
  38. public function testObjectImageResource()
  39. {
  40. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  41. $imageResource = new \stdClass();
  42. $this->renderer->setResource($imageResource);
  43. }
  44. public function testGoodHeight()
  45. {
  46. $this->assertSame(0, $this->renderer->getHeight());
  47. $this->renderer->setHeight(123);
  48. $this->assertSame(123, $this->renderer->getHeight());
  49. $this->renderer->setHeight(0);
  50. $this->assertSame(0, $this->renderer->getHeight());
  51. }
  52. public function testBadHeight()
  53. {
  54. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  55. $this->renderer->setHeight(- 1);
  56. }
  57. public function testGoodWidth()
  58. {
  59. $this->assertSame(0, $this->renderer->getWidth());
  60. $this->renderer->setWidth(123);
  61. $this->assertSame(123, $this->renderer->getWidth());
  62. $this->renderer->setWidth(0);
  63. $this->assertSame(0, $this->renderer->getWidth());
  64. }
  65. public function testBadWidth()
  66. {
  67. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  68. $this->renderer->setWidth(- 1);
  69. }
  70. public function testAllowedImageType()
  71. {
  72. $types = array('gif' => 'gif' , 'jpg' => 'jpeg' , 'jpeg' => 'jpeg' ,
  73. 'png' => 'png');
  74. foreach ($types as $type => $expectedType) {
  75. $this->renderer->setImageType($type);
  76. $this->assertSame($expectedType,
  77. $this->renderer->getImageType());
  78. }
  79. }
  80. public function testNonAllowedImageType()
  81. {
  82. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  83. $this->renderer->setImageType('other');
  84. }
  85. public function testDrawReturnResource()
  86. {
  87. $this->checkTTFRequirement();
  88. $barcode = new Object\Code39(array('text' => '0123456789'));
  89. $this->renderer->setBarcode($barcode);
  90. $resource = $this->renderer->draw();
  91. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  92. $this->assertTrue(get_resource_type($resource) == 'gd', 'Image must be a GD resource');
  93. }
  94. public function testDrawWithExistantResourceReturnResource()
  95. {
  96. $this->checkTTFRequirement();
  97. $barcode = new Object\Code39(array('text' => '0123456789'));
  98. $this->renderer->setBarcode($barcode);
  99. $imageResource = imagecreatetruecolor(500, 500);
  100. $this->renderer->setResource($imageResource);
  101. $resource = $this->renderer->draw();
  102. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  103. $this->assertTrue(get_resource_type($resource) == 'gd', 'Image must be a GD resource');
  104. $this->assertSame($resource, $imageResource);
  105. }
  106. public function testGoodUserHeight()
  107. {
  108. $barcode = new Object\Code39(array('text' => '0123456789'));
  109. $this->assertEquals(62, $barcode->getHeight());
  110. $this->renderer->setBarcode($barcode);
  111. $this->renderer->setHeight(62);
  112. $this->assertTrue($this->renderer->checkParams());
  113. }
  114. public function testBadUserHeightLessThanBarcodeHeight()
  115. {
  116. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  117. $barcode = new Object\Code39(array('text' => '0123456789'));
  118. $this->assertEquals(62, $barcode->getHeight());
  119. $this->renderer->setBarcode($barcode);
  120. $this->renderer->setHeight(61);
  121. $this->renderer->checkParams();
  122. }
  123. public function testGoodUserWidth()
  124. {
  125. $barcode = new Object\Code39(array('text' => '0123456789'));
  126. $this->assertEquals(211, $barcode->getWidth());
  127. $this->renderer->setBarcode($barcode);
  128. $this->renderer->setWidth(211);
  129. $this->assertTrue($this->renderer->checkParams());
  130. }
  131. public function testBadUserWidthLessThanBarcodeWidth()
  132. {
  133. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  134. $barcode = new Object\Code39(array('text' => '0123456789'));
  135. $this->assertEquals(211, $barcode->getWidth());
  136. $this->renderer->setBarcode($barcode);
  137. $this->renderer->setWidth(210);
  138. $this->renderer->checkParams();
  139. }
  140. public function testGoodHeightOfUserResource()
  141. {
  142. $barcode = new Object\Code39(array('text' => '0123456789'));
  143. $this->assertEquals(62, $barcode->getHeight());
  144. $imageResource = imagecreatetruecolor(500, 62);
  145. $this->renderer->setResource($imageResource);
  146. $this->renderer->setBarcode($barcode);
  147. $this->assertTrue($this->renderer->checkParams());
  148. }
  149. public function testBadHeightOfUserResource()
  150. {
  151. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  152. $barcode = new Object\Code39(array('text' => '0123456789'));
  153. $this->assertEquals(62, $barcode->getHeight());
  154. $this->renderer->setBarcode($barcode);
  155. $imageResource = imagecreatetruecolor(500, 61);
  156. $this->renderer->setResource($imageResource);
  157. $this->renderer->checkParams();
  158. }
  159. public function testGoodWidthOfUserResource()
  160. {
  161. $barcode = new Object\Code39(array('text' => '0123456789'));
  162. $this->assertEquals(211, $barcode->getWidth());
  163. $imageResource = imagecreatetruecolor(211, 500);
  164. $this->renderer->setResource($imageResource);
  165. $this->renderer->setBarcode($barcode);
  166. $this->assertTrue($this->renderer->checkParams());
  167. }
  168. public function testBadWidthOfUserResource()
  169. {
  170. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  171. $barcode = new Object\Code39(array('text' => '0123456789'));
  172. $this->assertEquals(211, $barcode->getWidth());
  173. $this->renderer->setBarcode($barcode);
  174. $imageResource = imagecreatetruecolor(210, 500);
  175. $this->renderer->setResource($imageResource);
  176. $this->renderer->checkParams();
  177. }
  178. public function testNoFontWithOrientation()
  179. {
  180. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  181. Barcode\Barcode::setBarcodeFont(null);
  182. $barcode = new Object\Code39(array('text' => '0123456789'));
  183. $barcode->setOrientation(1);
  184. $this->renderer->setBarcode($barcode);
  185. $this->renderer->draw();
  186. }
  187. protected function getRendererWithWidth500AndHeight300()
  188. {
  189. return $this->renderer->setHeight(300)->setWidth(500);
  190. }
  191. public function testRendererWithUnkownInstructionProvideByObject()
  192. {
  193. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  194. parent::testRendererWithUnkownInstructionProvideByObject();
  195. }
  196. public function testHorizontalPositionToLeft()
  197. {
  198. $this->checkTTFRequirement();
  199. parent::testHorizontalPositionToLeft();
  200. }
  201. public function testHorizontalPositionToCenter()
  202. {
  203. $this->checkTTFRequirement();
  204. parent::testHorizontalPositionToCenter();
  205. }
  206. public function testHorizontalPositionToRight()
  207. {
  208. $this->checkTTFRequirement();
  209. parent::testHorizontalPositionToRight();
  210. }
  211. public function testVerticalPositionToTop()
  212. {
  213. $this->checkTTFRequirement();
  214. parent::testVerticalPositionToTop();
  215. }
  216. public function testVerticalPositionToMiddle()
  217. {
  218. $this->checkTTFRequirement();
  219. parent::testVerticalPositionToMiddle();
  220. }
  221. public function testVerticalPositionToBottom()
  222. {
  223. $this->checkTTFRequirement();
  224. parent::testVerticalPositionToBottom();
  225. }
  226. public function testLeftOffsetOverrideHorizontalPosition()
  227. {
  228. $this->checkTTFRequirement();
  229. parent::testLeftOffsetOverrideHorizontalPosition();
  230. }
  231. public function testTopOffsetOverrideVerticalPosition()
  232. {
  233. $this->checkTTFRequirement();
  234. parent::testTopOffsetOverrideVerticalPosition();
  235. }
  236. /**
  237. * @group 4708
  238. */
  239. public function testImageGifWithNoTransparency()
  240. {
  241. $barcode = new Object\Code39(array('text' => '0123456789'));
  242. $this->renderer->setBarcode($barcode);
  243. $this->renderer->setTransparentBackground(false);
  244. $this->assertFalse($this->renderer->getTransparentBackground());
  245. //Test Gif output
  246. $this->renderer->setImageType('gif');
  247. $image = $this->renderer->draw();
  248. $index = imagecolortransparent($image);
  249. $this->assertEquals($index, -1);
  250. }
  251. /**
  252. * @group 4708
  253. */
  254. public function testImagePngWithNoTransparency()
  255. {
  256. $barcode = new Object\Code39(array('text' => '0123456789'));
  257. $this->renderer->setBarcode($barcode);
  258. $this->renderer->setTransparentBackground(false);
  259. $this->assertFalse($this->renderer->getTransparentBackground());
  260. //Test PNG output
  261. $this->renderer->setImageType('png');
  262. $image = $this->renderer->draw();
  263. $index = imagecolortransparent($image);
  264. $this->assertEquals($index, -1);
  265. }
  266. /**
  267. * @group 4708
  268. */
  269. public function testImageGifWithTransparency()
  270. {
  271. $barcode = new Object\Code39(array('text' => '0123456789'));
  272. $this->renderer->setBarcode($barcode);
  273. $this->renderer->setTransparentBackground(true);
  274. $this->assertTrue($this->renderer->getTransparentBackground());
  275. //Test Gif output
  276. $this->renderer->setImageType('gif');
  277. $image = $this->renderer->draw();
  278. $index = imagecolortransparent($image);
  279. $this->assertTrue($index !== -1);
  280. }
  281. /**
  282. * @group 4708
  283. */
  284. public function testImagePngWithTransparency()
  285. {
  286. $barcode = new Object\Code39(array('text' => '0123456789'));
  287. $this->renderer->setBarcode($barcode);
  288. $this->renderer->setTransparentBackground(true);
  289. $this->assertTrue($this->renderer->getTransparentBackground());
  290. //Test PNG output
  291. $this->renderer->setImageType('png');
  292. $image = $this->renderer->draw();
  293. $index = imagecolortransparent($image);
  294. $this->assertTrue($index !== -1);
  295. }
  296. protected function checkTTFRequirement()
  297. {
  298. if (!function_exists('imagettfbbox')) {
  299. $this->markTestSkipped('TTF (FreeType) support is required in order to run this test');
  300. }
  301. }
  302. }