TestCommon.php 15 KB

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