TestCommon.php 10.0 KB

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