BarcodeTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_Validate
  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. /** Zend_Validate_Barcode */
  27. require_once 'Zend/Validate/Barcode.php';
  28. /**
  29. * Zend_Validate_Barcode
  30. *
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @uses Zend_Validate_Barcode
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Validate
  38. */
  39. class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Test if EAN-13 contains only numeric characters
  43. *
  44. * @group ZF-3297
  45. */
  46. public function testEan13ContainsOnlyNumeric()
  47. {
  48. $barcode = new Zend_Validate_Barcode('ean13');
  49. $this->assertFalse($barcode->isValid('3RH1131-1BB40'));
  50. }
  51. public function testNoneExisting()
  52. {
  53. try {
  54. $barcode = new Zend_Validate_Barcode('Zend');
  55. $this->fail("'Zend' is not a valid barcode type'");
  56. } catch (Exception $e) {
  57. $this->assertContains("No such file", $e->getMessage());
  58. }
  59. }
  60. public function testSetAdapter()
  61. {
  62. $barcode = new Zend_Validate_Barcode('upca');
  63. $this->assertTrue($barcode->isValid('065100004327'));
  64. $barcode->setAdapter('ean13');
  65. $this->assertTrue($barcode->isValid('0075678164125'));
  66. }
  67. /**
  68. * @ZF-4352
  69. */
  70. public function testNonStringValidation()
  71. {
  72. $barcode = new Zend_Validate_Barcode('upca');
  73. $this->assertFalse($barcode->isValid(106510000.4327));
  74. $this->assertFalse($barcode->isValid(array('065100004327')));
  75. $barcode = new Zend_Validate_Barcode('ean13');
  76. $this->assertFalse($barcode->isValid(06510000.4327));
  77. $this->assertFalse($barcode->isValid(array('065100004327')));
  78. }
  79. public function testInvalidChecksumAdapter()
  80. {
  81. require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
  82. $barcode = new Zend_Validate_Barcode('MyBarcode1');
  83. $this->assertFalse($barcode->isValid('0000000'));
  84. $this->assertTrue(array_key_exists('barcodeFailed', $barcode->getMessages()));
  85. $this->assertFalse($barcode->getAdapter()->checksum('0000000'));
  86. }
  87. public function testInvalidCharAdapter()
  88. {
  89. require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
  90. $barcode = new Zend_Validate_Barcode('MyBarcode1');
  91. $this->assertFalse($barcode->getAdapter()->checkChars(123));
  92. }
  93. public function testAscii128CharacterAdapter()
  94. {
  95. require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
  96. $barcode = new Zend_Validate_Barcode('MyBarcode2');
  97. $this->assertTrue($barcode->getAdapter()->checkChars('1234QW!"'));
  98. }
  99. public function testInvalidLengthAdapter()
  100. {
  101. require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
  102. $barcode = new Zend_Validate_Barcode('MyBarcode2');
  103. $this->assertFalse($barcode->getAdapter()->checkLength(123));
  104. }
  105. public function testArrayLengthAdapter()
  106. {
  107. require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
  108. $barcode = new Zend_Validate_Barcode('MyBarcode2');
  109. $this->assertTrue($barcode->getAdapter()->checkLength('1'));
  110. $this->assertFalse($barcode->getAdapter()->checkLength('12'));
  111. $this->assertTrue($barcode->getAdapter()->checkLength('123'));
  112. $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
  113. }
  114. public function testArrayLengthAdapter2()
  115. {
  116. require_once dirname(__FILE__) . "/_files/MyBarcode3.php";
  117. $barcode = new Zend_Validate_Barcode('MyBarcode3');
  118. $this->assertTrue($barcode->getAdapter()->checkLength('1'));
  119. $this->assertTrue($barcode->getAdapter()->checkLength('12'));
  120. $this->assertTrue($barcode->getAdapter()->checkLength('123'));
  121. $this->assertTrue($barcode->getAdapter()->checkLength('1234'));
  122. }
  123. public function testOddLengthAdapter()
  124. {
  125. require_once dirname(__FILE__) . "/_files/MyBarcode4.php";
  126. $barcode = new Zend_Validate_Barcode('MyBarcode4');
  127. $this->assertTrue($barcode->getAdapter()->checkLength('1'));
  128. $this->assertFalse($barcode->getAdapter()->checkLength('12'));
  129. $this->assertTrue($barcode->getAdapter()->checkLength('123'));
  130. $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
  131. }
  132. public function testInvalidAdapter()
  133. {
  134. $barcode = new Zend_Validate_Barcode('Ean13');
  135. try {
  136. require_once dirname(__FILE__) . "/_files/MyBarcode5.php";
  137. $barcode->setAdapter('MyBarcode5');
  138. $this->fails('Exception expected');
  139. } catch (Exception $e) {
  140. $this->assertContains('does not implement', $e->getMessage());
  141. }
  142. }
  143. public function testArrayConstructAdapter()
  144. {
  145. $barcode = new Zend_Validate_Barcode(array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false));
  146. $this->assertTrue($barcode->getAdapter() instanceof Zend_Validate_Barcode_Ean13);
  147. $this->assertFalse($barcode->getChecksum());
  148. }
  149. public function testInvalidArrayConstructAdapter()
  150. {
  151. try {
  152. $barcode = new Zend_Validate_Barcode(array('options' => 'unknown', 'checksum' => false));
  153. $this->fails('Exception expected');
  154. } catch (Exception $e) {
  155. $this->assertContains('Missing option', $e->getMessage());
  156. }
  157. }
  158. public function testConfigConstructAdapter()
  159. {
  160. $array = array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false);
  161. require_once 'Zend/Config.php';
  162. $config = new Zend_Config($array);
  163. $barcode = new Zend_Validate_Barcode($config);
  164. $this->assertTrue($barcode->isValid('0075678164125'));
  165. }
  166. public function testxxxxxxxxxxxxCODE25()
  167. {
  168. $barcode = new Zend_Validate_Barcode('code25');
  169. $this->assertTrue($barcode->isValid('00075678164125'));
  170. $this->assertFalse($barcode->isValid('123'));
  171. $barcode->setChecksum(true);
  172. $this->assertFalse($barcode->isValid('00075678164124'));
  173. }
  174. public function testCODE39()
  175. {
  176. $barcode = new Zend_Validate_Barcode('code39');
  177. $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
  178. $this->assertTrue($barcode->isValid('00075678164124'));
  179. $this->assertFalse($barcode->isValid('Test93Test93Test'));
  180. $barcode->setChecksum(true);
  181. $this->assertTrue($barcode->isValid('159AZH'));
  182. $this->assertFalse($barcode->isValid('159AZG'));
  183. }
  184. public function testCODE39EXT()
  185. {
  186. $barcode = new Zend_Validate_Barcode('code39ext');
  187. $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
  188. $this->assertTrue($barcode->isValid('00075678164124'));
  189. $this->assertTrue($barcode->isValid('Test93Test93Test'));
  190. // @TODO: CODE39 EXTENDED CHECKSUM VALIDATION MISSING
  191. // $barcode->setChecksum(true);
  192. // $this->assertTrue($barcode->isValid('159AZH'));
  193. // $this->assertFalse($barcode->isValid('159AZG'));
  194. }
  195. public function testxxxxxxxxxxxxCODE93()
  196. {
  197. $barcode = new Zend_Validate_Barcode('code93');
  198. $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
  199. $this->assertFalse($barcode->isValid('00075678164124'));
  200. }
  201. public function testEAN8()
  202. {
  203. $barcode = new Zend_Validate_Barcode('ean8');
  204. $this->assertTrue($barcode->isValid('12345670'));
  205. $this->assertFalse($barcode->isValid('123'));
  206. $this->assertFalse($barcode->isValid('12345671'));
  207. }
  208. public function testEAN12()
  209. {
  210. $barcode = new Zend_Validate_Barcode('ean12');
  211. $this->assertTrue($barcode->isValid('123456789012'));
  212. $this->assertFalse($barcode->isValid('123'));
  213. $this->assertFalse($barcode->isValid('123456789013'));
  214. }
  215. public function testEAN13()
  216. {
  217. $barcode = new Zend_Validate_Barcode('ean13');
  218. $this->assertTrue($barcode->isValid('1234567890128'));
  219. $this->assertFalse($barcode->isValid('123'));
  220. $this->assertFalse($barcode->isValid('1234567890127'));
  221. }
  222. public function testEAN14()
  223. {
  224. $barcode = new Zend_Validate_Barcode('ean14');
  225. $this->assertTrue($barcode->isValid('12345678901231'));
  226. $this->assertFalse($barcode->isValid('123'));
  227. $this->assertFalse($barcode->isValid('12345678901232'));
  228. }
  229. public function testEAN18()
  230. {
  231. $barcode = new Zend_Validate_Barcode('ean18');
  232. $this->assertTrue($barcode->isValid('123456789012345675'));
  233. $this->assertFalse($barcode->isValid('123'));
  234. $this->assertFalse($barcode->isValid('123456789012345676'));
  235. }
  236. public function testGTIN12()
  237. {
  238. $barcode = new Zend_Validate_Barcode('gtin12');
  239. $this->assertTrue($barcode->isValid('123456789012'));
  240. $this->assertFalse($barcode->isValid('123'));
  241. $this->assertFalse($barcode->isValid('123456789013'));
  242. }
  243. public function testGTIN13()
  244. {
  245. $barcode = new Zend_Validate_Barcode('gtin13');
  246. $this->assertTrue($barcode->isValid('1234567890128'));
  247. $this->assertFalse($barcode->isValid('123'));
  248. $this->assertFalse($barcode->isValid('1234567890127'));
  249. }
  250. public function testGTIN14()
  251. {
  252. $barcode = new Zend_Validate_Barcode('gtin14');
  253. $this->assertTrue($barcode->isValid('12345678901231'));
  254. $this->assertFalse($barcode->isValid('123'));
  255. $this->assertFalse($barcode->isValid('12345678901232'));
  256. }
  257. public function testxxxxxxxxxxxxIDENTCODE()
  258. {
  259. $barcode = new Zend_Validate_Barcode('identcode');
  260. $this->assertTrue($barcode->isValid('564000000050'));
  261. $this->assertFalse($barcode->isValid('123'));
  262. $this->assertFalse($barcode->isValid('0563102430313'));
  263. $this->assertFalse($barcode->isValid('563102430312'));
  264. }
  265. public function testxxxxxxxxxxxxITF14()
  266. {
  267. $barcode = new Zend_Validate_Barcode('itf14');
  268. $this->assertTrue($barcode->isValid('00075678164125'));
  269. $this->assertFalse($barcode->isValid('123'));
  270. $this->assertFalse($barcode->isValid('00075678164124'));
  271. }
  272. public function testxxxxxxxxxxxxLEITCODE()
  273. {
  274. $barcode = new Zend_Validate_Barcode('leitcode');
  275. $this->assertTrue($barcode->isValid('21348075016401'));
  276. $this->assertFalse($barcode->isValid('123'));
  277. $this->assertFalse($barcode->isValid('021348075016401'));
  278. $this->assertFalse($barcode->isValid('21348075016402'));
  279. }
  280. public function testSSCC()
  281. {
  282. $barcode = new Zend_Validate_Barcode('sscc');
  283. $this->assertTrue($barcode->isValid('123456789012345675'));
  284. $this->assertFalse($barcode->isValid('123'));
  285. $this->assertFalse($barcode->isValid('123456789012345676'));
  286. }
  287. public function testUPCA()
  288. {
  289. $barcode = new Zend_Validate_Barcode('upca');
  290. $this->assertTrue($barcode->isValid('123456789012'));
  291. $this->assertFalse($barcode->isValid('123'));
  292. $this->assertFalse($barcode->isValid('123456789013'));
  293. }
  294. public function testxxxxxxxxxxxxUPCE()
  295. {
  296. $barcode = new Zend_Validate_Barcode('upce');
  297. $this->assertTrue($barcode->isValid('123456'));
  298. $this->assertFalse($barcode->isValid('123'));
  299. $this->assertFalse($barcode->isValid('1234567'));
  300. }
  301. }