ImageTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-2009 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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. require_once dirname(__FILE__) . '/TestCommon.php';
  27. require_once 'Zend/Barcode/Renderer/Image.php';
  28. require_once 'Zend/Barcode/Object/Code39.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Barcode
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
  37. {
  38. protected function _getRendererObject($options = null)
  39. {
  40. return new Zend_Barcode_Renderer_Image($options);
  41. }
  42. public function testType()
  43. {
  44. $this->assertSame('image', $this->_renderer->getType());
  45. }
  46. public function testGoodImageResource()
  47. {
  48. if (! extension_loaded('gd')) {
  49. $this->markTestSkipped(
  50. 'GD extension is required to run this test');
  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. if (! extension_loaded('gd')) {
  113. $this->markTestSkipped(
  114. 'GD extension is required to run this test');
  115. }
  116. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  117. $this->_renderer->setBarcode($barcode);
  118. $resource = $this->_renderer->draw();
  119. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  120. $this->assertTrue(get_resource_type($resource) == 'gd',
  121. 'Image must be a GD resource');
  122. }
  123. public function testDrawWithExistantResourceReturnResource()
  124. {
  125. if (! extension_loaded('gd')) {
  126. $this->markTestSkipped(
  127. 'GD extension is required to run this test');
  128. }
  129. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  130. $this->_renderer->setBarcode($barcode);
  131. $imageResource = imagecreatetruecolor(500, 500);
  132. $this->_renderer->setResource($imageResource);
  133. $resource = $this->_renderer->draw();
  134. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  135. $this->assertTrue(get_resource_type($resource) == 'gd',
  136. 'Image must be a GD resource');
  137. $this->assertSame($resource, $imageResource);
  138. }
  139. public function testGoodUserHeight()
  140. {
  141. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  142. $this->assertEquals(61, $barcode->getHeight());
  143. $this->_renderer->setBarcode($barcode);
  144. $this->_renderer->setHeight(61);
  145. $this->assertTrue($this->_renderer->checkParams());
  146. }
  147. /**
  148. * @expectedException Zend_Barcode_Renderer_Exception
  149. */
  150. public function testBadUserHeightLessThanBarcodeHeight()
  151. {
  152. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  153. $this->assertEquals(61, $barcode->getHeight());
  154. $this->_renderer->setBarcode($barcode);
  155. $this->_renderer->setHeight(60);
  156. $this->_renderer->checkParams();
  157. }
  158. public function testGoodUserWidth()
  159. {
  160. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  161. $this->assertEquals(211, $barcode->getWidth());
  162. $this->_renderer->setBarcode($barcode);
  163. $this->_renderer->setWidth(211);
  164. $this->assertTrue($this->_renderer->checkParams());
  165. }
  166. /**
  167. * @expectedException Zend_Barcode_Renderer_Exception
  168. */
  169. public function testBadUserWidthLessThanBarcodeWidth()
  170. {
  171. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  172. $this->assertEquals(211, $barcode->getWidth());
  173. $this->_renderer->setBarcode($barcode);
  174. $this->_renderer->setWidth(210);
  175. $this->_renderer->checkParams();
  176. }
  177. public function testGoodHeightOfUserResource()
  178. {
  179. if (! extension_loaded('gd')) {
  180. $this->markTestSkipped(
  181. 'GD extension is required to run this test');
  182. }
  183. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  184. $this->assertEquals(61, $barcode->getHeight());
  185. $imageResource = imagecreatetruecolor(500, 61);
  186. $this->_renderer->setResource($imageResource);
  187. $this->_renderer->setBarcode($barcode);
  188. $this->assertTrue($this->_renderer->checkParams());
  189. }
  190. /**
  191. * @expectedException Zend_Barcode_Renderer_Exception
  192. */
  193. public function testBadHeightOfUserResource()
  194. {
  195. if (! extension_loaded('gd')) {
  196. $this->markTestSkipped(
  197. 'GD extension is required to run this test');
  198. }
  199. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  200. $this->assertEquals(61, $barcode->getHeight());
  201. $this->_renderer->setBarcode($barcode);
  202. $imageResource = imagecreatetruecolor(500, 60);
  203. $this->_renderer->setResource($imageResource);
  204. $this->_renderer->checkParams();
  205. }
  206. public function testGoodWidthOfUserResource()
  207. {
  208. if (! extension_loaded('gd')) {
  209. $this->markTestSkipped(
  210. 'GD extension is required to run this test');
  211. }
  212. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  213. $this->assertEquals(211, $barcode->getWidth());
  214. $imageResource = imagecreatetruecolor(211, 500);
  215. $this->_renderer->setResource($imageResource);
  216. $this->_renderer->setBarcode($barcode);
  217. $this->assertTrue($this->_renderer->checkParams());
  218. }
  219. /**
  220. * @expectedException Zend_Barcode_Renderer_Exception
  221. */
  222. public function testBadWidthOfUserResource()
  223. {
  224. if (! extension_loaded('gd')) {
  225. $this->markTestSkipped(
  226. 'GD extension is required to run this test');
  227. }
  228. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  229. $this->assertEquals(211, $barcode->getWidth());
  230. $this->_renderer->setBarcode($barcode);
  231. $imageResource = imagecreatetruecolor(210, 500);
  232. $this->_renderer->setResource($imageResource);
  233. $this->_renderer->checkParams();
  234. }
  235. /**
  236. * @expectedException Zend_Barcode_Renderer_Exception
  237. */
  238. public function testNoFontWithOrientation()
  239. {
  240. if (! extension_loaded('gd')) {
  241. $this->markTestSkipped(
  242. 'GD extension is required to run this test');
  243. }
  244. Zend_Barcode::setBarcodeFont('');
  245. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  246. $barcode->setOrientation(1);
  247. $this->_renderer->setBarcode($barcode);
  248. $this->_renderer->draw();
  249. }
  250. protected function _getRendererWithWidth500AndHeight300()
  251. {
  252. return $this->_renderer->setHeight(300)->setWidth(500);
  253. }
  254. /**
  255. * @expectedException Zend_Barcode_Renderer_Exception
  256. */
  257. public function testRendererWithUnkownInstructionProvideByObject()
  258. {
  259. if (! extension_loaded('gd')) {
  260. $this->markTestSkipped(
  261. 'GD extension is required to run this test');
  262. }
  263. parent::testRendererWithUnkownInstructionProvideByObject();
  264. }
  265. public function testHorizontalPositionToLeft()
  266. {
  267. if (! extension_loaded('gd')) {
  268. $this->markTestSkipped(
  269. 'GD extension is required to run this test');
  270. }
  271. parent::testHorizontalPositionToLeft();
  272. }
  273. public function testHorizontalPositionToCenter()
  274. {
  275. if (! extension_loaded('gd')) {
  276. $this->markTestSkipped(
  277. 'GD extension is required to run this test');
  278. }
  279. parent::testHorizontalPositionToCenter();
  280. }
  281. public function testHorizontalPositionToRight()
  282. {
  283. if (! extension_loaded('gd')) {
  284. $this->markTestSkipped(
  285. 'GD extension is required to run this test');
  286. }
  287. parent::testHorizontalPositionToRight();
  288. }
  289. public function testVerticalPositionToTop()
  290. {
  291. if (! extension_loaded('gd')) {
  292. $this->markTestSkipped(
  293. 'GD extension is required to run this test');
  294. }
  295. parent::testVerticalPositionToTop();
  296. }
  297. public function testVerticalPositionToMiddle()
  298. {
  299. if (! extension_loaded('gd')) {
  300. $this->markTestSkipped(
  301. 'GD extension is required to run this test');
  302. }
  303. parent::testVerticalPositionToMiddle();
  304. }
  305. public function testVerticalPositionToBottom()
  306. {
  307. if (! extension_loaded('gd')) {
  308. $this->markTestSkipped(
  309. 'GD extension is required to run this test');
  310. }
  311. parent::testVerticalPositionToBottom();
  312. }
  313. }