/tests/Zend/Barcode/FactoryTest.php

https://bitbucket.org/ksekar/campus · PHP · 381 lines · 268 code · 55 blank · 58 comment · 4 complexity · da7c06084ab2a3196a3597e48d9a04ad MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FactoryTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. require_once 'Zend/Barcode.php';
  23. require_once 'Zend/Config.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Barcode
  27. * @subpackage UnitTests
  28. * @group Zend_Barcode
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function testMinimalFactory()
  35. {
  36. $this->_checkGDRequirement();
  37. $renderer = Zend_Barcode::factory('code39');
  38. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  39. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code39);
  40. }
  41. public function testMinimalFactoryWithRenderer()
  42. {
  43. $renderer = Zend_Barcode::factory('code39', 'pdf');
  44. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Pdf);
  45. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code39);
  46. }
  47. public function testFactoryWithOptions()
  48. {
  49. $this->_checkGDRequirement();
  50. $options = array('barHeight' => 123);
  51. $renderer = Zend_Barcode::factory('code39', 'image', $options);
  52. $this->assertEquals(123, $renderer->getBarcode()->getBarHeight());
  53. }
  54. public function testFactoryWithAutomaticExceptionRendering()
  55. {
  56. $this->_checkGDRequirement();
  57. $options = array('barHeight' => - 1);
  58. $renderer = Zend_Barcode::factory('code39', 'image', $options);
  59. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  60. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Error);
  61. }
  62. /**
  63. * @expectedException Zend_Barcode_Object_Exception
  64. */
  65. public function testFactoryWithoutAutomaticObjectExceptionRendering()
  66. {
  67. $options = array('barHeight' => - 1);
  68. $renderer = Zend_Barcode::factory('code39', 'image', $options, array(), false);
  69. }
  70. /**
  71. * @expectedException Zend_Barcode_Renderer_Exception
  72. */
  73. public function testFactoryWithoutAutomaticRendererExceptionRendering()
  74. {
  75. $this->_checkGDRequirement();
  76. $options = array('imageType' => 'my');
  77. $renderer = Zend_Barcode::factory('code39', 'image', array(), $options, false);
  78. $this->markTestIncomplete('Need to throw a configuration exception in renderer');
  79. }
  80. public function testFactoryWithZendConfig()
  81. {
  82. $this->_checkGDRequirement();
  83. $config = new Zend_Config(
  84. array('barcode' => 'code39' ,
  85. 'renderer' => 'image'));
  86. $renderer = Zend_Barcode::factory($config);
  87. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  88. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code39);
  89. }
  90. public function testFactoryWithZendConfigAndObjectOptions()
  91. {
  92. $this->_checkGDRequirement();
  93. $config = new Zend_Config(
  94. array('barcode' => 'code25' ,
  95. 'barcodeParams' => array(
  96. 'barHeight' => 123)));
  97. $renderer = Zend_Barcode::factory($config);
  98. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  99. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code25);
  100. $this->assertEquals(123, $renderer->getBarcode()->getBarHeight());
  101. }
  102. public function testFactoryWithZendConfigAndRendererOptions()
  103. {
  104. $this->_checkGDRequirement();
  105. $config = new Zend_Config(
  106. array('barcode' => 'code25' ,
  107. 'rendererParams' => array(
  108. 'imageType' => 'gif')));
  109. $renderer = Zend_Barcode::factory($config);
  110. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  111. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code25);
  112. $this->assertSame('gif', $renderer->getImageType());
  113. }
  114. public function testFactoryWithoutBarcodeWithAutomaticExceptionRender()
  115. {
  116. $this->_checkGDRequirement();
  117. $renderer = Zend_Barcode::factory(null);
  118. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  119. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Error);
  120. }
  121. public function testFactoryWithoutBarcodeWithAutomaticExceptionRenderWithZendConfig()
  122. {
  123. $this->_checkGDRequirement();
  124. $config = new Zend_Config(array('barcode' => null));
  125. $renderer = Zend_Barcode::factory($config);
  126. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  127. $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Error);
  128. }
  129. public function testFactoryWithExistingBarcodeObject()
  130. {
  131. $this->_checkGDRequirement();
  132. $barcode = new Zend_Barcode_Object_Code25();
  133. $renderer = Zend_Barcode::factory($barcode);
  134. $this->assertSame($barcode, $renderer->getBarcode());
  135. }
  136. public function testBarcodeObjectFactoryWithExistingBarcodeObject()
  137. {
  138. $barcode = new Zend_Barcode_Object_Code25();
  139. $generatedBarcode = Zend_Barcode::makeBarcode($barcode);
  140. $this->assertSame($barcode, $generatedBarcode);
  141. }
  142. public function testBarcodeObjectFactoryWithBarcodeAsString()
  143. {
  144. $barcode = Zend_Barcode::makeBarcode('code25');
  145. $this->assertTrue($barcode instanceof Zend_Barcode_Object_Code25);
  146. }
  147. public function testBarcodeObjectFactoryWithBarcodeAsStringAndConfigAsArray()
  148. {
  149. $barcode = Zend_Barcode::makeBarcode('code25', array('barHeight' => 123));
  150. $this->assertTrue($barcode instanceof Zend_Barcode_Object_Code25);
  151. $this->assertSame(123, $barcode->getBarHeight());
  152. }
  153. public function testBarcodeObjectFactoryWithBarcodeAsStringAndConfigAsZendConfig()
  154. {
  155. $config = new Zend_Config(array('barHeight' => 123));
  156. $barcode = Zend_Barcode::makeBarcode('code25', $config);
  157. $this->assertTrue($barcode instanceof Zend_Barcode_Object_Code25);
  158. $this->assertSame(123, $barcode->getBarHeight());
  159. }
  160. public function testBarcodeObjectFactoryWithBarcodeAsZendConfig()
  161. {
  162. $config = new Zend_Config(
  163. array('barcode' => 'code25' ,
  164. 'barcodeParams' => array(
  165. 'barHeight' => 123)));
  166. $barcode = Zend_Barcode::makeBarcode($config);
  167. $this->assertTrue($barcode instanceof Zend_Barcode_Object_Code25);
  168. $this->assertSame(123, $barcode->getBarHeight());
  169. }
  170. /**
  171. * @expectedException Zend_Barcode_Exception
  172. */
  173. public function testBarcodeObjectFactoryWithBarcodeAsZendConfigButNoBarcodeParameter()
  174. {
  175. $config = new Zend_Config(
  176. array(
  177. 'barcodeParams' => array(
  178. 'barHeight' => 123)));
  179. $barcode = Zend_Barcode::makeBarcode($config);
  180. }
  181. /**
  182. * @expectedException Zend_Barcode_Exception
  183. */
  184. public function testBarcodeObjectFactoryWithBarcodeAsZendConfigAndBadBarcodeParameters()
  185. {
  186. $barcode = Zend_Barcode::makeBarcode('code25', null);
  187. }
  188. public function testBarcodeObjectFactoryWithNamespace()
  189. {
  190. require_once dirname(__FILE__) . '/Object/_files/BarcodeNamespace.php';
  191. $barcode = Zend_Barcode::makeBarcode('error',
  192. array(
  193. 'barcodeNamespace' => 'My_Namespace'));
  194. $this->assertTrue($barcode instanceof My_Namespace_Error);
  195. }
  196. /**
  197. * @expectedException Zend_Barcode_Exception
  198. */
  199. public function testBarcodeObjectFactoryWithNamespaceButWithoutExtendingObjectAbstract()
  200. {
  201. require_once dirname(__FILE__) . '/Object/_files/BarcodeNamespaceWithoutExtendingObjectAbstract.php';
  202. $barcode = Zend_Barcode::makeBarcode('error',
  203. array(
  204. 'barcodeNamespace' => 'My_Namespace_Other'));
  205. }
  206. /**
  207. * @expectedException PHPUnit_Framework_Error
  208. */
  209. public function testBarcodeObjectFactoryWithUnexistantBarcode()
  210. {
  211. $barcode = Zend_Barcode::makeBarcode('zf123', array());
  212. }
  213. public function testBarcodeRendererFactoryWithExistingBarcodeRenderer()
  214. {
  215. $this->_checkGDRequirement();
  216. $renderer = new Zend_Barcode_Renderer_Image();
  217. $generatedBarcode = Zend_Barcode::makeRenderer($renderer);
  218. $this->assertSame($renderer, $generatedBarcode);
  219. }
  220. public function testBarcodeRendererFactoryWithBarcodeAsString()
  221. {
  222. $this->_checkGDRequirement();
  223. $renderer = Zend_Barcode::makeRenderer('image');
  224. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  225. }
  226. public function testBarcodeRendererFactoryWithBarcodeAsStringAndConfigAsArray()
  227. {
  228. $this->_checkGDRequirement();
  229. $renderer = Zend_Barcode::makeRenderer('image', array('imageType' => 'gif'));
  230. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  231. $this->assertSame('gif', $renderer->getImageType());
  232. }
  233. public function testBarcodeRendererFactoryWithBarcodeAsStringAndConfigAsZendConfig()
  234. {
  235. $this->_checkGDRequirement();
  236. $config = new Zend_Config(array('imageType' => 'gif'));
  237. $renderer = Zend_Barcode::makeRenderer('image', $config);
  238. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  239. $this->assertSame('gif', $renderer->getimageType());
  240. }
  241. public function testBarcodeRendererFactoryWithBarcodeAsZendConfig()
  242. {
  243. $this->_checkGDRequirement();
  244. $config = new Zend_Config(
  245. array('renderer' => 'image' ,
  246. 'rendererParams' => array(
  247. 'imageType' => 'gif')));
  248. $renderer = Zend_Barcode::makeRenderer($config);
  249. $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
  250. $this->assertSame('gif', $renderer->getimageType());
  251. }
  252. /**
  253. * @expectedException Zend_Barcode_Exception
  254. */
  255. public function testBarcodeRendererFactoryWithBarcodeAsZendConfigButNoBarcodeParameter()
  256. {
  257. $config = new Zend_Config(
  258. array(
  259. 'rendererParams' => array(
  260. 'imageType' => 'gif')));
  261. $renderer = Zend_Barcode::makeRenderer($config);
  262. }
  263. /**
  264. * @expectedException Zend_Barcode_Exception
  265. */
  266. public function testBarcodeRendererFactoryWithBarcodeAsZendConfigAndBadBarcodeParameters()
  267. {
  268. $renderer = Zend_Barcode::makeRenderer('image', null);
  269. }
  270. public function testBarcodeRendererFactoryWithNamespace()
  271. {
  272. $this->_checkGDRequirement();
  273. require_once dirname(__FILE__) . '/Renderer/_files/RendererNamespace.php';
  274. $renderer = Zend_Barcode::makeRenderer('image',
  275. array(
  276. 'rendererNamespace' => 'My_Namespace'));
  277. $this->assertTrue($renderer instanceof My_Namespace_Image);
  278. }
  279. /**
  280. * @expectedException Zend_Barcode_Exception
  281. */
  282. public function testBarcodeFactoryWithNamespaceButWithoutExtendingRendererAbstract()
  283. {
  284. require_once dirname(__FILE__) . '/Renderer/_files/RendererNamespaceWithoutExtendingRendererAbstract.php';
  285. $renderer = Zend_Barcode::makeRenderer('image',
  286. array(
  287. 'rendererNamespace' => 'My_Namespace_Other'));
  288. }
  289. /**
  290. * @expectedException PHPUnit_Framework_Error
  291. */
  292. public function testBarcodeRendererFactoryWithUnexistantRenderer()
  293. {
  294. $renderer = Zend_Barcode::makeRenderer('zend', array());
  295. }
  296. public function testProxyBarcodeRendererDrawAsImage()
  297. {
  298. if (! extension_loaded('gd')) {
  299. $this->markTestSkipped(
  300. 'GD extension is required to run this test');
  301. }
  302. $resource = Zend_Barcode::draw('code25', 'image');
  303. $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource');
  304. $this->assertTrue(get_resource_type($resource) == 'gd', 'Image must be a GD resource');
  305. }
  306. public function testProxyBarcodeRendererDrawAsPdf()
  307. {
  308. Zend_Barcode::setBarcodeFont(dirname(__FILE__) . '/Object/_fonts/Vera.ttf');
  309. $resource = Zend_Barcode::draw('code25', 'pdf');
  310. $this->assertTrue($resource instanceof Zend_Pdf);
  311. Zend_Barcode::setBarcodeFont('');
  312. }
  313. public function testProxyBarcodeObjectFont()
  314. {
  315. Zend_Barcode::setBarcodeFont('my_font.ttf');
  316. $barcode = new Zend_Barcode_Object_Code25();
  317. $this->assertSame('my_font.ttf', $barcode->getFont());
  318. Zend_Barcode::setBarcodeFont('');
  319. }
  320. protected function _checkGDRequirement()
  321. {
  322. if (!extension_loaded('gd')) {
  323. $this->markTestSkipped('This test requires the GD extension');
  324. }
  325. }
  326. }