TestCommon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. require_once 'Zend/Barcode.php';
  27. require_once 'Zend/Config.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Barcode
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Barcode_Renderer_TestCommon extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * @var Zend_Barcode_Renderer
  39. */
  40. protected $_renderer = null;
  41. abstract protected function _getRendererObject($options = null);
  42. public function setUp()
  43. {
  44. Zend_Barcode::setBarcodeFont(dirname(__FILE__) . '/../Object/_fonts/Vera.ttf');
  45. $this->_renderer = $this->_getRendererObject();
  46. }
  47. public function tearDown()
  48. {
  49. Zend_Barcode::setBarcodeFont('');
  50. }
  51. public function testSetBarcodeObject()
  52. {
  53. require_once 'Zend/Barcode/Object/Code39.php';
  54. $barcode = new Zend_Barcode_Object_Code39();
  55. $this->_renderer->setBarcode($barcode);
  56. $this->assertSame($barcode, $this->_renderer->getBarcode());
  57. }
  58. /**
  59. * @expectedException Zend_Barcode_Renderer_Exception
  60. */
  61. public function testSetInvalidBarcodeObject()
  62. {
  63. $barcode = new StdClass();
  64. $this->_renderer->setBarcode($barcode);
  65. }
  66. public function testGoodModuleSize()
  67. {
  68. $this->_renderer->setModuleSize(2.34);
  69. $this->assertSame(2.34, $this->_renderer->getModuleSize());
  70. }
  71. /**
  72. * @expectedException Zend_Barcode_Renderer_Exception
  73. */
  74. public function testModuleSizeAsString()
  75. {
  76. $this->_renderer->setModuleSize('abc');
  77. }
  78. /**
  79. * @expectedException Zend_Barcode_Renderer_Exception
  80. */
  81. public function testModuleSizeLessThan0()
  82. {
  83. $this->_renderer->setModuleSize(-0.5);
  84. }
  85. public function testAutomaticRenderError()
  86. {
  87. $this->_renderer->setAutomaticRenderError(true);
  88. $this->assertSame(true, $this->_renderer->getAutomaticRenderError());
  89. $this->_renderer->setAutomaticRenderError(1);
  90. $this->assertSame(true, $this->_renderer->getAutomaticRenderError());
  91. }
  92. public function testGoodHorizontalPosition()
  93. {
  94. foreach (array('left' , 'center' , 'right') as $position) {
  95. $this->_renderer->setHorizontalPosition($position);
  96. $this->assertSame($position,
  97. $this->_renderer->getHorizontalPosition());
  98. }
  99. }
  100. /**
  101. * @expectedException Zend_Barcode_Renderer_Exception
  102. */
  103. public function testBadHorizontalPosition()
  104. {
  105. $this->_renderer->setHorizontalPosition('none');
  106. }
  107. public function testGoodVerticalPosition()
  108. {
  109. foreach (array('top' , 'middle' , 'bottom') as $position) {
  110. $this->_renderer->setVerticalPosition($position);
  111. $this->assertSame($position,
  112. $this->_renderer->getVerticalPosition());
  113. }
  114. }
  115. /**
  116. * @expectedException Zend_Barcode_Renderer_Exception
  117. */
  118. public function testBadVerticalPosition()
  119. {
  120. $this->_renderer->setVerticalPosition('none');
  121. }
  122. public function testGoodLeftOffset()
  123. {
  124. $this->assertSame(0, $this->_renderer->getLeftOffset());
  125. $this->_renderer->setLeftOffset(123);
  126. $this->assertSame(123, $this->_renderer->getLeftOffset());
  127. $this->_renderer->setLeftOffset(0);
  128. $this->assertSame(0, $this->_renderer->getLeftOffset());
  129. }
  130. /**
  131. * @expectedException Zend_Barcode_Renderer_Exception
  132. */
  133. public function testBadLeftOffset()
  134. {
  135. $this->_renderer->setLeftOffset(- 1);
  136. }
  137. public function testGoodTopOffset()
  138. {
  139. $this->assertSame(0, $this->_renderer->getTopOffset());
  140. $this->_renderer->setTopOffset(123);
  141. $this->assertSame(123, $this->_renderer->getTopOffset());
  142. $this->_renderer->setTopOffset(0);
  143. $this->assertSame(0, $this->_renderer->getTopOffset());
  144. }
  145. /**
  146. * @expectedException Zend_Barcode_Renderer_Exception
  147. */
  148. public function testBadTopOffset()
  149. {
  150. $this->_renderer->setTopOffset(- 1);
  151. }
  152. public function testConstructorWithArray()
  153. {
  154. $renderer = $this->_getRendererObject(
  155. array('automaticRenderError' => true ,
  156. 'unkownProperty' => 'aValue'));
  157. $this->assertEquals(true, $renderer->getAutomaticRenderError());
  158. }
  159. public function testConstructorWithZendConfig()
  160. {
  161. $config = new Zend_Config(
  162. array('automaticRenderError' => true ,
  163. 'unkownProperty' => 'aValue'));
  164. $renderer = $this->_getRendererObject($config);
  165. $this->assertEquals(true, $renderer->getAutomaticRenderError());
  166. }
  167. public function testSetOptions()
  168. {
  169. $this->assertEquals(false, $this->_renderer->getAutomaticRenderError());
  170. $this->_renderer->setOptions(
  171. array('automaticRenderError' => true ,
  172. 'unkownProperty' => 'aValue'));
  173. $this->assertEquals(true, $this->_renderer->getAutomaticRenderError());
  174. }
  175. public function testSetConfig()
  176. {
  177. $this->assertEquals(false, $this->_renderer->getAutomaticRenderError());
  178. $config = new Zend_Config(
  179. array('automaticRenderError' => true ,
  180. 'unkownProperty' => 'aValue'));
  181. $this->_renderer->setConfig($config);
  182. $this->assertEquals(true, $this->_renderer->getAutomaticRenderError());
  183. }
  184. public function testRendererNamespace()
  185. {
  186. $this->_renderer->setRendererNamespace('My_Namespace');
  187. $this->assertEquals('My_Namespace', $this->_renderer->getRendererNamespace());
  188. }
  189. /**
  190. * @expectedException Zend_Barcode_Renderer_Exception
  191. */
  192. public function testRendererWithUnkownInstructionProvideByObject()
  193. {
  194. require_once dirname(__FILE__) . '/../Object/_files/BarcodeTest.php';
  195. $object = new Zend_Barcode_Object_Test();
  196. $object->setText('test');
  197. $object->addInstruction(array('type' => 'unknown'));
  198. $this->_renderer->setBarcode($object);
  199. $this->_renderer->draw();
  200. }
  201. /**
  202. * @expectedException Zend_Barcode_Renderer_Exception
  203. */
  204. public function testBarcodeObjectProvided()
  205. {
  206. $this->_renderer->draw();
  207. }
  208. abstract public function testDrawReturnResource();
  209. abstract public function testDrawWithExistantResourceReturnResource();
  210. abstract protected function _getRendererWithWidth500AndHeight300();
  211. public function testHorizontalPositionToLeft()
  212. {
  213. $renderer = $this->_getRendererWithWidth500AndHeight300();
  214. $renderer->setModuleSize(1);
  215. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  216. $this->assertEquals(211, $barcode->getWidth());
  217. $renderer->setBarcode($barcode);
  218. $renderer->draw();
  219. $this->assertEquals(0, $renderer->getLeftOffset());
  220. }
  221. public function testHorizontalPositionToCenter()
  222. {
  223. $renderer = $this->_getRendererWithWidth500AndHeight300();
  224. $renderer->setModuleSize(1);
  225. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  226. $this->assertEquals(211, $barcode->getWidth());
  227. $renderer->setBarcode($barcode);
  228. $renderer->setHorizontalPosition('center');
  229. $renderer->draw();
  230. $this->assertEquals(144, $renderer->getLeftOffset());
  231. }
  232. public function testHorizontalPositionToRight()
  233. {
  234. $renderer = $this->_getRendererWithWidth500AndHeight300();
  235. $renderer->setModuleSize(1);
  236. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  237. $this->assertEquals(211, $barcode->getWidth());
  238. $renderer->setBarcode($barcode);
  239. $renderer->setHorizontalPosition('right');
  240. $renderer->draw();
  241. $this->assertEquals(289, $renderer->getLeftOffset());
  242. }
  243. public function testLeftOffsetOverrideHorizontalPosition()
  244. {
  245. $renderer = $this->_getRendererWithWidth500AndHeight300();
  246. $renderer->setModuleSize(1);
  247. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  248. $this->assertEquals(211, $barcode->getWidth());
  249. $renderer->setBarcode($barcode);
  250. $renderer->setLeftOffset(12);
  251. $renderer->setHorizontalPosition('center');
  252. $renderer->draw();
  253. $this->assertEquals(12, $renderer->getLeftOffset());
  254. }
  255. public function testVerticalPositionToTop()
  256. {
  257. $renderer = $this->_getRendererWithWidth500AndHeight300();
  258. $renderer->setModuleSize(1);
  259. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  260. $this->assertEquals(62, $barcode->getHeight());
  261. $renderer->setBarcode($barcode);
  262. $renderer->setVerticalPosition('top');
  263. $renderer->draw();
  264. $this->assertEquals(0, $renderer->getTopOffset());
  265. }
  266. public function testVerticalPositionToMiddle()
  267. {
  268. $renderer = $this->_getRendererWithWidth500AndHeight300();
  269. $renderer->setModuleSize(1);
  270. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  271. $this->assertEquals(62, $barcode->getHeight());
  272. $renderer->setBarcode($barcode);
  273. $renderer->setVerticalPosition('middle');
  274. $renderer->draw();
  275. $this->assertEquals(119, $renderer->getTopOffset());
  276. }
  277. public function testVerticalPositionToBottom()
  278. {
  279. $renderer = $this->_getRendererWithWidth500AndHeight300();
  280. $renderer->setModuleSize(1);
  281. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  282. $this->assertEquals(62, $barcode->getHeight());
  283. $renderer->setBarcode($barcode);
  284. $renderer->setVerticalPosition('bottom');
  285. $renderer->draw();
  286. $this->assertEquals(238, $renderer->getTopOffset());
  287. }
  288. public function testTopOffsetOverrideVerticalPosition()
  289. {
  290. $renderer = $this->_getRendererWithWidth500AndHeight300();
  291. $renderer->setModuleSize(1);
  292. $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
  293. $this->assertEquals(62, $barcode->getHeight());
  294. $renderer->setBarcode($barcode);
  295. $renderer->setTopOffset(12);
  296. $renderer->setVerticalPosition('middle');
  297. $renderer->draw();
  298. $this->assertEquals(12, $renderer->getTopOffset());
  299. }
  300. }