2
0

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-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. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/_files/BarcodeTest.php';
  30. /**
  31. * @see Zend_Config
  32. */
  33. require_once 'Zend/Config.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Barcode
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. abstract class Zend_Barcode_Object_TestCommon extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * @var Zend_Barcode_Object
  45. */
  46. protected $_object = null;
  47. abstract protected function _getBarcodeObject($options = null);
  48. protected function loadInstructionsFile($fileName)
  49. {
  50. return include_once (dirname(__FILE__) . "/_files/$fileName.php");
  51. }
  52. public function setUp()
  53. {
  54. $this->_object = $this->_getBarcodeObject();
  55. }
  56. public function tearDown()
  57. {
  58. $this->_object = null;
  59. }
  60. public function testStaticFontAsString()
  61. {
  62. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('my_static_font.ttf');
  63. $this->assertEquals('', $this->_object->getFont());
  64. $object = $this->_getBarcodeObject();
  65. $this->assertEquals('my_static_font.ttf', $object->getFont());
  66. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('');
  67. }
  68. public function testStaticFontAsNumber()
  69. {
  70. for ($i = 1; $i < 5; $i++) {
  71. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont($i);
  72. $this->assertEquals('', $this->_object->getFont());
  73. $object = $this->_getBarcodeObject();
  74. $this->assertEquals($i, $object->getFont());
  75. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont('');
  76. }
  77. }
  78. public function testConstructorWithArray()
  79. {
  80. $object = $this->_getBarcodeObject(
  81. array('barHeight' => 150 ,
  82. 'unkownProperty' => 'aValue'));
  83. $this->assertEquals(150, $object->getBarHeight());
  84. }
  85. public function testConstructorWithZendConfig()
  86. {
  87. $config = new Zend_Config(
  88. array('barHeight' => 150 ,
  89. 'unkownProperty' => 'aValue'));
  90. $object = $this->_getBarcodeObject($config);
  91. $this->assertEquals(150, $object->getBarHeight());
  92. }
  93. public function testSetOptions()
  94. {
  95. $this->_object->setOptions(
  96. array('barHeight' => 150 ,
  97. 'unkownProperty' => 'aValue'));
  98. $this->assertEquals(150, $this->_object->getBarHeight());
  99. }
  100. public function testSetConfig()
  101. {
  102. $config = new Zend_Config(
  103. array('barHeight' => 150 ,
  104. 'unkownProperty' => 'aValue'));
  105. $this->_object->setConfig($config);
  106. $this->assertEquals(150, $this->_object->getBarHeight());
  107. }
  108. public function testBarcodeNamespace()
  109. {
  110. $this->_object->setBarcodeNamespace('My_Namespace');
  111. $this->assertEquals('My_Namespace', $this->_object->getBarcodeNamespace());
  112. }
  113. public function testBarHeight()
  114. {
  115. $this->_object->setBarHeight(1);
  116. $this->assertSame(1, $this->_object->getBarHeight());
  117. $this->_object->setBarHeight(true);
  118. $this->assertSame(1, $this->_object->getBarHeight());
  119. $this->_object->setBarHeight('200a');
  120. $this->assertSame(200, $this->_object->getBarHeight());
  121. }
  122. /**
  123. * @expectedException Zend_Barcode_Object_Exception
  124. */
  125. public function testNegativeBarHeight()
  126. {
  127. $this->_object->setBarHeight(- 1);
  128. }
  129. public function testBarThinWidth()
  130. {
  131. $this->_object->setBarThinWidth(1);
  132. $this->assertSame(1, $this->_object->getBarThinWidth());
  133. $this->_object->setBarThinWidth(true);
  134. $this->assertSame(1, $this->_object->getBarThinWidth());
  135. $this->_object->setBarThinWidth('200a');
  136. $this->assertSame(200, $this->_object->getBarThinWidth());
  137. }
  138. /**
  139. * @expectedException Zend_Barcode_Object_Exception
  140. */
  141. public function testNegativeBarThinWidth()
  142. {
  143. $this->_object->setBarThinWidth(- 1);
  144. }
  145. public function testBarThickWidth()
  146. {
  147. $this->_object->setBarThickWidth(1);
  148. $this->assertSame(1, $this->_object->getBarThickWidth());
  149. $this->_object->setBarThickWidth(true);
  150. $this->assertSame(1, $this->_object->getBarThickWidth());
  151. $this->_object->setBarThickWidth('200a');
  152. $this->assertSame(200, $this->_object->getBarThickWidth());
  153. }
  154. /**
  155. * @expectedException Zend_Barcode_Object_Exception
  156. */
  157. public function testNegativeBarThickWidth()
  158. {
  159. $this->_object->setBarThickWidth(- 1);
  160. }
  161. public function testFactor()
  162. {
  163. $this->_object->setFactor(1);
  164. $this->assertSame(1.0, $this->_object->getFactor());
  165. $this->_object->setFactor(1.25);
  166. $this->assertSame(1.25, $this->_object->getFactor());
  167. $this->_object->setFactor(true);
  168. $this->assertSame(1.0, $this->_object->getFactor());
  169. $this->_object->setFactor('200a');
  170. $this->assertSame(200.0, $this->_object->getFactor());
  171. }
  172. /**
  173. * @expectedException Zend_Barcode_Object_Exception
  174. */
  175. public function testNegativeFactor()
  176. {
  177. $this->_object->setFactor(- 1);
  178. }
  179. public function testForeColor()
  180. {
  181. $this->_object->setForeColor('#333333');
  182. $this->assertSame(3355443, $this->_object->getForeColor());
  183. $this->_object->setForeColor(1000);
  184. $this->assertSame(1000, $this->_object->getForeColor());
  185. }
  186. /**
  187. * @expectedException Zend_Barcode_Object_Exception
  188. */
  189. public function testNegativeForeColor()
  190. {
  191. $this->_object->setForeColor(- 1);
  192. }
  193. /**
  194. * @expectedException Zend_Barcode_Object_Exception
  195. */
  196. public function testTooHighForeColor()
  197. {
  198. $this->_object->setForeColor(16777126);
  199. }
  200. public function testBackgroundColor()
  201. {
  202. $this->_object->setBackgroundColor('#333333');
  203. $this->assertSame(3355443, $this->_object->getBackgroundColor());
  204. $this->_object->setBackgroundColor(1000);
  205. $this->assertSame(1000, $this->_object->getBackgroundColor());
  206. }
  207. /**
  208. * @expectedException Zend_Barcode_Object_Exception
  209. */
  210. public function testNegativeBackgroundColor()
  211. {
  212. $this->_object->setBackgroundColor(- 1);
  213. }
  214. /**
  215. * @expectedException Zend_Barcode_Object_Exception
  216. */
  217. public function testTooHighBackgroundColor()
  218. {
  219. $this->_object->setBackgroundColor(16777126);
  220. }
  221. public function testWithBorder()
  222. {
  223. $this->_object->setWithBorder(1);
  224. $this->assertSame(true, $this->_object->getWithBorder());
  225. $this->_object->setWithBorder(true);
  226. $this->assertSame(true, $this->_object->getWithBorder());
  227. }
  228. public function testReverseColor()
  229. {
  230. $this->_object->setForeColor(11);
  231. $this->_object->setBackgroundColor(111);
  232. $this->_object->setReverseColor();
  233. $this->assertSame(111, $this->_object->getForeColor());
  234. $this->assertSame(11, $this->_object->getBackgroundColor());
  235. }
  236. public function testOrientation()
  237. {
  238. $this->_object->setOrientation(1);
  239. $this->assertSame(1.0, $this->_object->getOrientation());
  240. $this->_object->setOrientation(1.25);
  241. $this->assertSame(1.25, $this->_object->getOrientation());
  242. $this->_object->setOrientation(true);
  243. $this->assertSame(1.0, $this->_object->getOrientation());
  244. $this->_object->setOrientation('200a');
  245. $this->assertSame(200.0, $this->_object->getOrientation());
  246. $this->_object->setOrientation(360);
  247. $this->assertSame(0.0, $this->_object->getOrientation());
  248. }
  249. public function testDrawText()
  250. {
  251. $this->_object->setDrawText(1);
  252. $this->assertSame(true, $this->_object->getDrawText());
  253. $this->_object->setDrawText(true);
  254. $this->assertSame(true, $this->_object->getDrawText());
  255. }
  256. public function testStretchText()
  257. {
  258. $this->_object->setStretchText(1);
  259. $this->assertSame(true, $this->_object->getStretchText());
  260. $this->_object->setStretchText(true);
  261. $this->assertSame(true, $this->_object->getStretchText());
  262. }
  263. public function testWithChecksum()
  264. {
  265. $this->_object->setWithChecksum(1);
  266. $this->assertSame(true, $this->_object->getWithChecksum());
  267. $this->_object->setWithChecksum(true);
  268. $this->assertSame(true, $this->_object->getWithChecksum());
  269. }
  270. public function testWithChecksumInText()
  271. {
  272. $this->_object->setWithChecksumInText(1);
  273. $this->assertSame(true, $this->_object->getWithChecksumInText());
  274. $this->_object->setWithChecksumInText(true);
  275. $this->assertSame(true, $this->_object->getWithChecksumInText());
  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. }