ImageTest.php 11 KB

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