FactoryTest.php 12 KB

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