FactoryTest.php 12 KB

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