ImageTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 (!function_exists('gd_info')) {
  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. if (! extension_loaded('gd')) {
  54. $this->markTestSkipped(
  55. 'GD extension is required to run this test');
  56. }
  57. $imageResource = imagecreatetruecolor(1, 1);
  58. $this->_renderer->setResource($imageResource);
  59. }
  60. /**
  61. * @expectedException Zend_Barcode_Renderer_Exception
  62. */
  63. public function testObjectImageResource()
  64. {
  65. $imageResource = new StdClass();
  66. $this->_renderer->setResource($imageResource);
  67. }
  68. public function testGoodHeight()
  69. {
  70. $this->assertSame(0, $this->_renderer->getHeight());
  71. $this->_renderer->setHeight(123);
  72. $this->assertSame(123, $this->_renderer->getHeight());
  73. $this->_renderer->setHeight(0);
  74. $this->assertSame(0, $this->_renderer->getHeight());
  75. }
  76. /**
  77. * @expectedException Zend_Barcode_Renderer_Exception
  78. */
  79. public function testBadHeight()
  80. {
  81. $this->_renderer->setHeight(- 1);
  82. }
  83. public function testGoodWidth()
  84. {
  85. $this->assertSame(0, $this->_renderer->getWidth());
  86. $this->_renderer->setWidth(123);
  87. $this->assertSame(123, $this->_renderer->getWidth());
  88. $this->_renderer->setWidth(0);
  89. $this->assertSame(0, $this->_renderer->getWidth());
  90. }
  91. /**
  92. * @expectedException Zend_Barcode_Renderer_Exception
  93. */
  94. public function testBadWidth()
  95. {
  96. $this->_renderer->setWidth(- 1);
  97. }
  98. public function testAllowedImageType()
  99. {
  100. $types = array('gif' => 'gif' , 'jpg' => 'jpeg' , 'jpeg' => 'jpeg' ,
  101. 'png' => 'png');
  102. foreach ($types as $type => $expectedType) {
  103. $this->_renderer->setImageType($type);
  104. $this->assertSame($expectedType,
  105. $this->_renderer->getImageType());
  106. }
  107. }
  108. /**
  109. * @expectedException Zend_Barcode_Renderer_Exception
  110. */
  111. public function testNonAllowedImageType()
  112. {
  113. $this->_renderer->setImageType('other');
  114. }
  115. public function testDrawReturnResource()
  116. {
  117. if (! extension_loaded('gd')) {
  118. $this->markTestSkipped(
  119. 'GD extension is required to run this test');
  120. }
  121. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  122. $this->_renderer->setBarcode($barcode);
  123. $resource = $this->_renderer->draw();
  124. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  125. $this->assertTrue(get_resource_type($resource) == 'gd',
  126. 'Image must be a GD resource');
  127. }
  128. public function testDrawWithExistantResourceReturnResource()
  129. {
  130. if (! extension_loaded('gd')) {
  131. $this->markTestSkipped(
  132. 'GD extension is required to run this test');
  133. }
  134. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  135. $this->_renderer->setBarcode($barcode);
  136. $imageResource = imagecreatetruecolor(500, 500);
  137. $this->_renderer->setResource($imageResource);
  138. $resource = $this->_renderer->draw();
  139. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  140. $this->assertTrue(get_resource_type($resource) == 'gd',
  141. 'Image must be a GD resource');
  142. $this->assertSame($resource, $imageResource);
  143. }
  144. public function testGoodUserHeight()
  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(62);
  150. $this->assertTrue($this->_renderer->checkParams());
  151. }
  152. /**
  153. * @expectedException Zend_Barcode_Renderer_Exception
  154. */
  155. public function testBadUserHeightLessThanBarcodeHeight()
  156. {
  157. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  158. $this->assertEquals(62, $barcode->getHeight());
  159. $this->_renderer->setBarcode($barcode);
  160. $this->_renderer->setHeight(61);
  161. $this->_renderer->checkParams();
  162. }
  163. public function testGoodUserWidth()
  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(211);
  169. $this->assertTrue($this->_renderer->checkParams());
  170. }
  171. /**
  172. * @expectedException Zend_Barcode_Renderer_Exception
  173. */
  174. public function testBadUserWidthLessThanBarcodeWidth()
  175. {
  176. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  177. $this->assertEquals(211, $barcode->getWidth());
  178. $this->_renderer->setBarcode($barcode);
  179. $this->_renderer->setWidth(210);
  180. $this->_renderer->checkParams();
  181. }
  182. public function testGoodHeightOfUserResource()
  183. {
  184. if (! extension_loaded('gd')) {
  185. $this->markTestSkipped(
  186. 'GD extension is required to run this test');
  187. }
  188. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  189. $this->assertEquals(62, $barcode->getHeight());
  190. $imageResource = imagecreatetruecolor(500, 62);
  191. $this->_renderer->setResource($imageResource);
  192. $this->_renderer->setBarcode($barcode);
  193. $this->assertTrue($this->_renderer->checkParams());
  194. }
  195. /**
  196. * @expectedException Zend_Barcode_Renderer_Exception
  197. */
  198. public function testBadHeightOfUserResource()
  199. {
  200. if (! extension_loaded('gd')) {
  201. $this->markTestSkipped(
  202. 'GD extension is required to run this test');
  203. }
  204. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  205. $this->assertEquals(62, $barcode->getHeight());
  206. $this->_renderer->setBarcode($barcode);
  207. $imageResource = imagecreatetruecolor(500, 61);
  208. $this->_renderer->setResource($imageResource);
  209. $this->_renderer->checkParams();
  210. }
  211. public function testGoodWidthOfUserResource()
  212. {
  213. if (! extension_loaded('gd')) {
  214. $this->markTestSkipped(
  215. 'GD extension is required to run this test');
  216. }
  217. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  218. $this->assertEquals(211, $barcode->getWidth());
  219. $imageResource = imagecreatetruecolor(211, 500);
  220. $this->_renderer->setResource($imageResource);
  221. $this->_renderer->setBarcode($barcode);
  222. $this->assertTrue($this->_renderer->checkParams());
  223. }
  224. /**
  225. * @expectedException Zend_Barcode_Renderer_Exception
  226. */
  227. public function testBadWidthOfUserResource()
  228. {
  229. if (! extension_loaded('gd')) {
  230. $this->markTestSkipped(
  231. 'GD extension is required to run this test');
  232. }
  233. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  234. $this->assertEquals(211, $barcode->getWidth());
  235. $this->_renderer->setBarcode($barcode);
  236. $imageResource = imagecreatetruecolor(210, 500);
  237. $this->_renderer->setResource($imageResource);
  238. $this->_renderer->checkParams();
  239. }
  240. /**
  241. * @expectedException Zend_Barcode_Renderer_Exception
  242. */
  243. public function testNoFontWithOrientation()
  244. {
  245. if (! extension_loaded('gd')) {
  246. $this->markTestSkipped(
  247. 'GD extension is required to run this test');
  248. }
  249. Zend_Barcode::setBarcodeFont('');
  250. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  251. $barcode->setOrientation(1);
  252. $this->_renderer->setBarcode($barcode);
  253. $this->_renderer->draw();
  254. }
  255. protected function _getRendererWithWidth500AndHeight300()
  256. {
  257. return $this->_renderer->setHeight(300)->setWidth(500);
  258. }
  259. /**
  260. * @expectedException Zend_Barcode_Renderer_Exception
  261. */
  262. public function testRendererWithUnkownInstructionProvideByObject()
  263. {
  264. if (! extension_loaded('gd')) {
  265. $this->markTestSkipped(
  266. 'GD extension is required to run this test');
  267. }
  268. parent::testRendererWithUnkownInstructionProvideByObject();
  269. }
  270. public function testHorizontalPositionToLeft()
  271. {
  272. if (! extension_loaded('gd')) {
  273. $this->markTestSkipped(
  274. 'GD extension is required to run this test');
  275. }
  276. parent::testHorizontalPositionToLeft();
  277. }
  278. public function testHorizontalPositionToCenter()
  279. {
  280. if (! extension_loaded('gd')) {
  281. $this->markTestSkipped(
  282. 'GD extension is required to run this test');
  283. }
  284. parent::testHorizontalPositionToCenter();
  285. }
  286. public function testHorizontalPositionToRight()
  287. {
  288. if (! extension_loaded('gd')) {
  289. $this->markTestSkipped(
  290. 'GD extension is required to run this test');
  291. }
  292. parent::testHorizontalPositionToRight();
  293. }
  294. public function testVerticalPositionToTop()
  295. {
  296. if (! extension_loaded('gd')) {
  297. $this->markTestSkipped(
  298. 'GD extension is required to run this test');
  299. }
  300. parent::testVerticalPositionToTop();
  301. }
  302. public function testVerticalPositionToMiddle()
  303. {
  304. if (! extension_loaded('gd')) {
  305. $this->markTestSkipped(
  306. 'GD extension is required to run this test');
  307. }
  308. parent::testVerticalPositionToMiddle();
  309. }
  310. public function testVerticalPositionToBottom()
  311. {
  312. if (! extension_loaded('gd')) {
  313. $this->markTestSkipped(
  314. 'GD extension is required to run this test');
  315. }
  316. parent::testVerticalPositionToBottom();
  317. }
  318. }