TestCommon.php 11 KB

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