/tests/Zend/Barcode/FactoryTest.php

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