ImageTest.php 11 KB

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