PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Barcode/Renderer/TestCommon.php

http://github.com/zendframework/zf2
PHP | 295 lines | 240 code | 39 blank | 16 comment | 1 complexity | 6025ebbcd342f1db03c9557448901968 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Barcode\Renderer;
  10. use ZendTest\Barcode\Object\TestAsset as TestAsset;
  11. use Zend\Barcode;
  12. use Zend\Barcode\Object;
  13. use Zend\Config;
  14. abstract class TestCommon extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var \Zend\Barcode\Renderer
  18. */
  19. protected $renderer = null;
  20. /**
  21. * Stores the original set timezone
  22. * @var string
  23. */
  24. private $originaltimezone;
  25. abstract protected function getRendererObject($options = null);
  26. public function setUp()
  27. {
  28. $this->originaltimezone = date_default_timezone_get();
  29. // Set timezone to avoid "It is not safe to rely on the system's timezone settings."
  30. // message if timezone is not set within php.ini
  31. date_default_timezone_set('GMT');
  32. Barcode\Barcode::setBarcodeFont(__DIR__ . '/../Object/_fonts/Vera.ttf');
  33. $this->renderer = $this->getRendererObject();
  34. }
  35. public function tearDown()
  36. {
  37. Barcode\Barcode::setBarcodeFont(null);
  38. if (!empty($this->originaltimezone)) {
  39. date_default_timezone_set($this->originaltimezone);
  40. }
  41. }
  42. public function testSetBarcodeObject()
  43. {
  44. $barcode = new Object\Code39();
  45. $this->renderer->setBarcode($barcode);
  46. $this->assertSame($barcode, $this->renderer->getBarcode());
  47. }
  48. public function testGoodModuleSize()
  49. {
  50. $this->renderer->setModuleSize(2.34);
  51. $this->assertSame(2.34, $this->renderer->getModuleSize());
  52. }
  53. public function testModuleSizeAsString()
  54. {
  55. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  56. $this->renderer->setModuleSize('abc');
  57. }
  58. public function testModuleSizeLessThan0()
  59. {
  60. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  61. $this->renderer->setModuleSize(-0.5);
  62. }
  63. public function testAutomaticRenderError()
  64. {
  65. $this->renderer->setAutomaticRenderError(true);
  66. $this->assertSame(true, $this->renderer->getAutomaticRenderError());
  67. $this->renderer->setAutomaticRenderError(1);
  68. $this->assertSame(true, $this->renderer->getAutomaticRenderError());
  69. }
  70. public function testGoodHorizontalPosition()
  71. {
  72. foreach (array('left', 'center', 'right') as $position) {
  73. $this->renderer->setHorizontalPosition($position);
  74. $this->assertSame($position,
  75. $this->renderer->getHorizontalPosition());
  76. }
  77. }
  78. public function testBadHorizontalPosition()
  79. {
  80. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  81. $this->renderer->setHorizontalPosition('none');
  82. }
  83. public function testGoodVerticalPosition()
  84. {
  85. foreach (array('top', 'middle', 'bottom') as $position) {
  86. $this->renderer->setVerticalPosition($position);
  87. $this->assertSame($position,
  88. $this->renderer->getVerticalPosition());
  89. }
  90. }
  91. public function testBadVerticalPosition()
  92. {
  93. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  94. $this->renderer->setVerticalPosition('none');
  95. }
  96. public function testGoodLeftOffset()
  97. {
  98. $this->assertSame(0, $this->renderer->getLeftOffset());
  99. $this->renderer->setLeftOffset(123);
  100. $this->assertSame(123, $this->renderer->getLeftOffset());
  101. $this->renderer->setLeftOffset(0);
  102. $this->assertSame(0, $this->renderer->getLeftOffset());
  103. }
  104. public function testBadLeftOffset()
  105. {
  106. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  107. $this->renderer->setLeftOffset(- 1);
  108. }
  109. public function testGoodTopOffset()
  110. {
  111. $this->assertSame(0, $this->renderer->getTopOffset());
  112. $this->renderer->setTopOffset(123);
  113. $this->assertSame(123, $this->renderer->getTopOffset());
  114. $this->renderer->setTopOffset(0);
  115. $this->assertSame(0, $this->renderer->getTopOffset());
  116. }
  117. public function testBadTopOffset()
  118. {
  119. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  120. $this->renderer->setTopOffset(- 1);
  121. }
  122. public function testConstructorWithArray()
  123. {
  124. $renderer = $this->getRendererObject(
  125. array('automaticRenderError' => true,
  126. 'unkownProperty' => 'aValue'));
  127. $this->assertEquals(true, $renderer->getAutomaticRenderError());
  128. }
  129. public function testConstructorWithZendConfig()
  130. {
  131. $config = new Config\Config(
  132. array('automaticRenderError' => true,
  133. 'unkownProperty' => 'aValue'));
  134. $renderer = $this->getRendererObject($config);
  135. $this->assertEquals(true, $renderer->getAutomaticRenderError());
  136. }
  137. public function testSetOptions()
  138. {
  139. $this->assertEquals(false, $this->renderer->getAutomaticRenderError());
  140. $this->renderer->setOptions(
  141. array('automaticRenderError' => true,
  142. 'unkownProperty' => 'aValue'));
  143. $this->assertEquals(true, $this->renderer->getAutomaticRenderError());
  144. }
  145. public function testRendererNamespace()
  146. {
  147. $this->renderer->setRendererNamespace('My_Namespace');
  148. $this->assertEquals('My_Namespace', $this->renderer->getRendererNamespace());
  149. }
  150. public function testRendererWithUnkownInstructionProvideByObject()
  151. {
  152. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  153. $object = new TestAsset\BarcodeTest();
  154. $object->setText('test');
  155. $object->addTestInstruction(array('type' => 'unknown'));
  156. $this->renderer->setBarcode($object);
  157. $this->renderer->draw();
  158. }
  159. public function testBarcodeObjectProvided()
  160. {
  161. $this->setExpectedException('\Zend\Barcode\Renderer\Exception\ExceptionInterface');
  162. $this->renderer->draw();
  163. }
  164. abstract public function testDrawReturnResource();
  165. abstract public function testDrawWithExistantResourceReturnResource();
  166. abstract protected function getRendererWithWidth500AndHeight300();
  167. public function testHorizontalPositionToLeft()
  168. {
  169. $renderer = $this->getRendererWithWidth500AndHeight300();
  170. $renderer->setModuleSize(1);
  171. $barcode = new Object\Code39(array('text' => '0123456789'));
  172. $this->assertEquals(211, $barcode->getWidth());
  173. $renderer->setBarcode($barcode);
  174. $renderer->draw();
  175. $this->assertEquals(0, $renderer->getLeftOffset());
  176. }
  177. public function testHorizontalPositionToCenter()
  178. {
  179. $renderer = $this->getRendererWithWidth500AndHeight300();
  180. $renderer->setModuleSize(1);
  181. $barcode = new Object\Code39(array('text' => '0123456789'));
  182. $this->assertEquals(211, $barcode->getWidth());
  183. $renderer->setBarcode($barcode);
  184. $renderer->setHorizontalPosition('center');
  185. $renderer->draw();
  186. $this->assertEquals(144, $renderer->getLeftOffset());
  187. }
  188. public function testHorizontalPositionToRight()
  189. {
  190. $renderer = $this->getRendererWithWidth500AndHeight300();
  191. $renderer->setModuleSize(1);
  192. $barcode = new Object\Code39(array('text' => '0123456789'));
  193. $this->assertEquals(211, $barcode->getWidth());
  194. $renderer->setBarcode($barcode);
  195. $renderer->setHorizontalPosition('right');
  196. $renderer->draw();
  197. $this->assertEquals(289, $renderer->getLeftOffset());
  198. }
  199. public function testLeftOffsetOverrideHorizontalPosition()
  200. {
  201. $renderer = $this->getRendererWithWidth500AndHeight300();
  202. $renderer->setModuleSize(1);
  203. $barcode = new Object\Code39(array('text' => '0123456789'));
  204. $this->assertEquals(211, $barcode->getWidth());
  205. $renderer->setBarcode($barcode);
  206. $renderer->setLeftOffset(12);
  207. $renderer->setHorizontalPosition('center');
  208. $renderer->draw();
  209. $this->assertEquals(12, $renderer->getLeftOffset());
  210. }
  211. public function testVerticalPositionToTop()
  212. {
  213. $renderer = $this->getRendererWithWidth500AndHeight300();
  214. $renderer->setModuleSize(1);
  215. $barcode = new Object\Code39(array('text' => '0123456789'));
  216. $this->assertEquals(62, $barcode->getHeight());
  217. $renderer->setBarcode($barcode);
  218. $renderer->setVerticalPosition('top');
  219. $renderer->draw();
  220. $this->assertEquals(0, $renderer->getTopOffset());
  221. }
  222. public function testVerticalPositionToMiddle()
  223. {
  224. $renderer = $this->getRendererWithWidth500AndHeight300();
  225. $renderer->setModuleSize(1);
  226. $barcode = new Object\Code39(array('text' => '0123456789'));
  227. $this->assertEquals(62, $barcode->getHeight());
  228. $renderer->setBarcode($barcode);
  229. $renderer->setVerticalPosition('middle');
  230. $renderer->draw();
  231. $this->assertEquals(119, $renderer->getTopOffset());
  232. }
  233. public function testVerticalPositionToBottom()
  234. {
  235. $renderer = $this->getRendererWithWidth500AndHeight300();
  236. $renderer->setModuleSize(1);
  237. $barcode = new Object\Code39(array('text' => '0123456789'));
  238. $this->assertEquals(62, $barcode->getHeight());
  239. $renderer->setBarcode($barcode);
  240. $renderer->setVerticalPosition('bottom');
  241. $renderer->draw();
  242. $this->assertEquals(238, $renderer->getTopOffset());
  243. }
  244. public function testTopOffsetOverrideVerticalPosition()
  245. {
  246. $renderer = $this->getRendererWithWidth500AndHeight300();
  247. $renderer->setModuleSize(1);
  248. $barcode = new Object\Code39(array('text' => '0123456789'));
  249. $this->assertEquals(62, $barcode->getHeight());
  250. $renderer->setBarcode($barcode);
  251. $renderer->setTopOffset(12);
  252. $renderer->setVerticalPosition('middle');
  253. $renderer->draw();
  254. $this->assertEquals(12, $renderer->getTopOffset());
  255. }
  256. }