TestCommon.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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-2015 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 dirname(__FILE__) . '/_files/BarcodeTest.php';
  23. /**
  24. * @see Zend_Config
  25. */
  26. require_once 'Zend/Config.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Barcode
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Barcode_Object_TestCommon extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * @var Zend_Barcode_Object
  38. */
  39. protected $_object = null;
  40. abstract protected function _getBarcodeObject($options = null);
  41. protected function loadInstructionsFile($fileName)
  42. {
  43. return include_once (dirname(__FILE__) . "/_files/$fileName.php");
  44. }
  45. public function setUp()
  46. {
  47. $this->_object = $this->_getBarcodeObject();
  48. }
  49. public function tearDown()
  50. {
  51. $this->_object = null;
  52. }
  53. public function testStaticFontAsString()
  54. {
  55. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('my_static_font.ttf');
  56. $this->assertEquals('', $this->_object->getFont());
  57. $object = $this->_getBarcodeObject();
  58. $this->assertEquals('my_static_font.ttf', $object->getFont());
  59. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('');
  60. }
  61. public function testStaticFontAsNumber()
  62. {
  63. for ($i = 1; $i < 5; $i++) {
  64. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont($i);
  65. $this->assertEquals('', $this->_object->getFont());
  66. $object = $this->_getBarcodeObject();
  67. $this->assertEquals($i, $object->getFont());
  68. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('');
  69. }
  70. }
  71. public function testConstructorWithArray()
  72. {
  73. $object = $this->_getBarcodeObject(
  74. array('barHeight' => 150 ,
  75. 'unknownProperty' => 'aValue'));
  76. $this->assertEquals(150, $object->getBarHeight());
  77. }
  78. public function testConstructorWithZendConfig()
  79. {
  80. $config = new Zend_Config(
  81. array('barHeight' => 150 ,
  82. 'unknownProperty' => 'aValue'));
  83. $object = $this->_getBarcodeObject($config);
  84. $this->assertEquals(150, $object->getBarHeight());
  85. }
  86. public function testSetOptions()
  87. {
  88. $this->_object->setOptions(
  89. array('barHeight' => 150 ,
  90. 'unknownProperty' => 'aValue'));
  91. $this->assertEquals(150, $this->_object->getBarHeight());
  92. }
  93. public function testSetConfig()
  94. {
  95. $config = new Zend_Config(
  96. array('barHeight' => 150 ,
  97. 'unknownProperty' => 'aValue'));
  98. $this->_object->setConfig($config);
  99. $this->assertEquals(150, $this->_object->getBarHeight());
  100. }
  101. public function testBarcodeNamespace()
  102. {
  103. $this->_object->setBarcodeNamespace('My_Namespace');
  104. $this->assertEquals('My_Namespace', $this->_object->getBarcodeNamespace());
  105. }
  106. public function testBarHeight()
  107. {
  108. $this->_object->setBarHeight(1);
  109. $this->assertSame(1, $this->_object->getBarHeight());
  110. $this->_object->setBarHeight(true);
  111. $this->assertSame(1, $this->_object->getBarHeight());
  112. $this->_object->setBarHeight('200a');
  113. $this->assertSame(200, $this->_object->getBarHeight());
  114. }
  115. /**
  116. * @expectedException Zend_Barcode_Object_Exception
  117. */
  118. public function testNegativeBarHeight()
  119. {
  120. $this->_object->setBarHeight(- 1);
  121. }
  122. public function testBarThinWidth()
  123. {
  124. $this->_object->setBarThinWidth(1);
  125. $this->assertSame(1, $this->_object->getBarThinWidth());
  126. $this->_object->setBarThinWidth(true);
  127. $this->assertSame(1, $this->_object->getBarThinWidth());
  128. $this->_object->setBarThinWidth('200a');
  129. $this->assertSame(200, $this->_object->getBarThinWidth());
  130. }
  131. /**
  132. * @expectedException Zend_Barcode_Object_Exception
  133. */
  134. public function testNegativeBarThinWidth()
  135. {
  136. $this->_object->setBarThinWidth(- 1);
  137. }
  138. public function testBarThickWidth()
  139. {
  140. $this->_object->setBarThickWidth(1);
  141. $this->assertSame(1, $this->_object->getBarThickWidth());
  142. $this->_object->setBarThickWidth(true);
  143. $this->assertSame(1, $this->_object->getBarThickWidth());
  144. $this->_object->setBarThickWidth('200a');
  145. $this->assertSame(200, $this->_object->getBarThickWidth());
  146. }
  147. /**
  148. * @expectedException Zend_Barcode_Object_Exception
  149. */
  150. public function testNegativeBarThickWidth()
  151. {
  152. $this->_object->setBarThickWidth(- 1);
  153. }
  154. public function testFactor()
  155. {
  156. $this->_object->setFactor(1);
  157. $this->assertSame(1.0, $this->_object->getFactor());
  158. $this->_object->setFactor(1.25);
  159. $this->assertSame(1.25, $this->_object->getFactor());
  160. $this->_object->setFactor(true);
  161. $this->assertSame(1.0, $this->_object->getFactor());
  162. $this->_object->setFactor('200a');
  163. $this->assertSame(200.0, $this->_object->getFactor());
  164. }
  165. /**
  166. * @expectedException Zend_Barcode_Object_Exception
  167. */
  168. public function testNegativeFactor()
  169. {
  170. $this->_object->setFactor(- 1);
  171. }
  172. public function testForeColor()
  173. {
  174. $this->_object->setForeColor('#333333');
  175. $this->assertSame(3355443, $this->_object->getForeColor());
  176. $this->_object->setForeColor(1000);
  177. $this->assertSame(1000, $this->_object->getForeColor());
  178. }
  179. /**
  180. * @expectedException Zend_Barcode_Object_Exception
  181. */
  182. public function testNegativeForeColor()
  183. {
  184. $this->_object->setForeColor(- 1);
  185. }
  186. /**
  187. * @expectedException Zend_Barcode_Object_Exception
  188. */
  189. public function testTooHighForeColor()
  190. {
  191. $this->_object->setForeColor(16777126);
  192. }
  193. public function testBackgroundColor()
  194. {
  195. $this->_object->setBackgroundColor('#333333');
  196. $this->assertSame(3355443, $this->_object->getBackgroundColor());
  197. $this->_object->setBackgroundColor(1000);
  198. $this->assertSame(1000, $this->_object->getBackgroundColor());
  199. }
  200. /**
  201. * @expectedException Zend_Barcode_Object_Exception
  202. */
  203. public function testNegativeBackgroundColor()
  204. {
  205. $this->_object->setBackgroundColor(- 1);
  206. }
  207. /**
  208. * @expectedException Zend_Barcode_Object_Exception
  209. */
  210. public function testTooHighBackgroundColor()
  211. {
  212. $this->_object->setBackgroundColor(16777126);
  213. }
  214. public function testWithBorder()
  215. {
  216. $this->_object->setWithBorder(1);
  217. $this->assertSame(true, $this->_object->getWithBorder());
  218. $this->_object->setWithBorder(true);
  219. $this->assertSame(true, $this->_object->getWithBorder());
  220. }
  221. public function testReverseColor()
  222. {
  223. $this->_object->setForeColor(11);
  224. $this->_object->setBackgroundColor(111);
  225. $this->_object->setReverseColor();
  226. $this->assertSame(111, $this->_object->getForeColor());
  227. $this->assertSame(11, $this->_object->getBackgroundColor());
  228. }
  229. public function testOrientation()
  230. {
  231. $this->_object->setOrientation(1);
  232. $this->assertSame(1.0, $this->_object->getOrientation());
  233. $this->_object->setOrientation(1.25);
  234. $this->assertSame(1.25, $this->_object->getOrientation());
  235. $this->_object->setOrientation(true);
  236. $this->assertSame(1.0, $this->_object->getOrientation());
  237. $this->_object->setOrientation('200a');
  238. $this->assertSame(200.0, $this->_object->getOrientation());
  239. $this->_object->setOrientation(360);
  240. $this->assertSame(0.0, $this->_object->getOrientation());
  241. }
  242. public function testDrawText()
  243. {
  244. $this->_object->setDrawText(1);
  245. $this->assertSame(true, $this->_object->getDrawText());
  246. $this->_object->setDrawText(true);
  247. $this->assertSame(true, $this->_object->getDrawText());
  248. }
  249. public function testStretchText()
  250. {
  251. $this->_object->setStretchText(1);
  252. $this->assertSame(true, $this->_object->getStretchText());
  253. $this->_object->setStretchText(true);
  254. $this->assertSame(true, $this->_object->getStretchText());
  255. }
  256. public function testWithChecksum()
  257. {
  258. $this->_object->setWithChecksum(1);
  259. $this->assertSame(true, $this->_object->getWithChecksum());
  260. $this->_object->setWithChecksum(true);
  261. $this->assertSame(true, $this->_object->getWithChecksum());
  262. }
  263. public function testWithChecksumInText()
  264. {
  265. $this->_object->setWithChecksumInText(1);
  266. $this->assertSame(true, $this->_object->getWithChecksumInText());
  267. $this->_object->setWithChecksumInText(true);
  268. $this->assertSame(true, $this->_object->getWithChecksumInText());
  269. }
  270. public function testWithoutQuietZones()
  271. {
  272. $this->_object->setWithQuietZones(0);
  273. $this->assertSame(false, $this->_object->getWithQuietZones());
  274. $this->_object->setWithQuietZones(false);
  275. $this->assertSame(false, $this->_object->getWithQuietZones());
  276. }
  277. public function testSetFontAsNumberForGdImage()
  278. {
  279. if (! extension_loaded('gd')) {
  280. $this->markTestSkipped(
  281. 'GD extension is required to run this test');
  282. }
  283. $gdFontSize = array(8 , 13 , 13 , 16 , 15);
  284. for ($i = 1; $i <= 5; $i ++) {
  285. $this->_object->setFont($i);
  286. $this->assertSame($i, $this->_object->getFont());
  287. $this->assertSame($gdFontSize[$i - 1],
  288. $this->_object->getFontSize());
  289. }
  290. }
  291. /**
  292. * @expectedException Zend_Barcode_Object_Exception
  293. */
  294. public function testSetLowFontAsNumberForGdImage()
  295. {
  296. $this->_object->setFont(0);
  297. }
  298. /**
  299. * @expectedException Zend_Barcode_Object_Exception
  300. */
  301. public function testSetHighFontAsNumberForGdImage()
  302. {
  303. $this->_object->setFont(6);
  304. }
  305. public function testSetFontAsString()
  306. {
  307. $this->_object->setFont('my_font.ttf');
  308. $this->assertSame('my_font.ttf', $this->_object->getFont());
  309. }
  310. /**
  311. * @expectedException Zend_Barcode_Object_Exception
  312. */
  313. public function testSetFontAsBoolean()
  314. {
  315. $this->_object->setFont(true);
  316. }
  317. public function testFontAsNumberWithoutGd()
  318. {
  319. if (extension_loaded('gd')) {
  320. $this->markTestSkipped(
  321. 'GD extension must not be loaded to run this test');
  322. }
  323. $this->setExpectedException('Zend_Barcode_Object_Exception');
  324. $this->_object->setFont(1);
  325. }
  326. public function testFontSize()
  327. {
  328. $this->_object->setFontSize(22);
  329. $this->assertSame(22, $this->_object->getFontSize());
  330. }
  331. public function testFontSizeWithoutEffectWithGdInternalFont()
  332. {
  333. if (! extension_loaded('gd')) {
  334. $this->markTestSkipped(
  335. 'GD extension is required to run this test');
  336. }
  337. $this->_object->setFont(1);
  338. $this->_object->setFontSize(22);
  339. $this->assertSame(8, $this->_object->getFontSize());
  340. }
  341. /**
  342. * @expectedException Zend_Barcode_Object_Exception
  343. */
  344. public function testStringFontSize()
  345. {
  346. $this->_object->setFontSize('22a');
  347. }
  348. public function testStandardQuietZone()
  349. {
  350. $this->_object->setBarThinWidth(3);
  351. $this->_object->setFactor(4);
  352. $this->assertSame(120.0, $this->_object->getQuietZone());
  353. }
  354. public function testAddInstruction()
  355. {
  356. $object = new Zend_Barcode_Object_Test();
  357. $instructions = array('type' => 'text' , 'text' => 'text' , 'size' => 10 ,
  358. 'position' => array(5 , 5) ,
  359. 'font' => 'my_font.ttf' ,
  360. 'color' => '#123456' ,
  361. 'alignment' => 'center' ,
  362. 'orientation' => 45);
  363. $object->addInstruction($instructions);
  364. $this->assertSame(array($instructions), $object->getInstructions());
  365. }
  366. public function testAddPolygon()
  367. {
  368. $object = new Zend_Barcode_Object_Test();
  369. $points = array();
  370. $color = '#123456';
  371. $filled = false;
  372. $instructions = array('type' => 'polygon' , 'points' => $points ,
  373. 'color' => $color , 'filled' => $filled);
  374. $object->addPolygon($points, $color, $filled);
  375. $this->assertSame(array($instructions), $object->getInstructions());
  376. }
  377. public function testAddPolygonWithDefaultColor()
  378. {
  379. $object = new Zend_Barcode_Object_Test();
  380. $points = array();
  381. $color = 123456;
  382. $object->setForeColor($color);
  383. $filled = false;
  384. $instructions = array('type' => 'polygon' , 'points' => $points ,
  385. 'color' => $color , 'filled' => $filled);
  386. $object->addPolygon($points, null, $filled);
  387. $this->assertSame(array($instructions), $object->getInstructions());
  388. }
  389. public function testAddText()
  390. {
  391. $object = new Zend_Barcode_Object_Test();
  392. $size = 10;
  393. $text = 'foobar';
  394. $position = array();
  395. $font = 'my_font.ttf';
  396. $color = '#123456';
  397. $alignment = 'right';
  398. $orientation = 45;
  399. $instructions = array('type' => 'text' , 'text' => $text , 'size' => $size ,
  400. 'position' => $position ,
  401. 'font' => $font , 'color' => $color ,
  402. 'alignment' => $alignment ,
  403. 'orientation' => $orientation);
  404. $object->addText($text, $size, $position, $font, $color, $alignment,
  405. $orientation);
  406. $this->assertSame(array($instructions), $object->getInstructions());
  407. }
  408. public function testAddTextWithDefaultColor()
  409. {
  410. $object = new Zend_Barcode_Object_Test();
  411. $size = 10;
  412. $text = 'foobar';
  413. $position = array();
  414. $font = 'my_font.ttf';
  415. $color = 123456;
  416. $object->setForeColor($color);
  417. $alignment = 'right';
  418. $orientation = 45;
  419. $instructions = array('type' => 'text' , 'text' => $text , 'size' => $size ,
  420. 'position' => $position ,
  421. 'font' => $font , 'color' => $color ,
  422. 'alignment' => $alignment ,
  423. 'orientation' => $orientation);
  424. $object->addText($text, $size, $position, $font, null, $alignment, $orientation);
  425. $this->assertSame(array($instructions), $object->getInstructions());
  426. }
  427. /**
  428. * @expectedException Zend_Barcode_Object_Exception
  429. */
  430. public function testCheckParamsFontWithOrientation()
  431. {
  432. $this->_object->setText('0');
  433. $this->_object->setFont(1);
  434. $this->_object->setOrientation(45);
  435. $this->_object->checkParams();
  436. }
  437. public function testGetDefaultHeight()
  438. {
  439. $this->assertEquals(62, $this->_object->getHeight());
  440. }
  441. }