|
|
@@ -41,24 +41,6 @@ require_once 'Zend/Validate/Barcode.php';
|
|
|
*/
|
|
|
class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
- public function testUpcA()
|
|
|
- {
|
|
|
- $barcode = new Zend_Validate_Barcode('upc-a');
|
|
|
-
|
|
|
- $this->assertTrue($barcode->isValid('065100004327'));
|
|
|
- $this->assertFalse($barcode->isValid('123'));
|
|
|
- $this->assertFalse($barcode->isValid('065100004328'));
|
|
|
- }
|
|
|
-
|
|
|
- public function testEan13()
|
|
|
- {
|
|
|
- $barcode = new Zend_Validate_Barcode('ean-13');
|
|
|
-
|
|
|
- $this->assertTrue($barcode->isValid('0075678164125'));
|
|
|
- $this->assertFalse($barcode->isValid('123'));
|
|
|
- $this->assertFalse($barcode->isValid('0075678164124'));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Test if EAN-13 contains only numeric characters
|
|
|
*
|
|
|
@@ -66,7 +48,7 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
public function testEan13ContainsOnlyNumeric()
|
|
|
{
|
|
|
- $barcode = new Zend_Validate_Barcode('ean-13');
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean13');
|
|
|
$this->assertFalse($barcode->isValid('3RH1131-1BB40'));
|
|
|
}
|
|
|
|
|
|
@@ -76,16 +58,16 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
|
|
|
$barcode = new Zend_Validate_Barcode('Zend');
|
|
|
$this->fail("'Zend' is not a valid barcode type'");
|
|
|
} catch (Exception $e) {
|
|
|
- $this->assertContains("'Zend' is not supported", $e->getMessage());
|
|
|
+ $this->assertContains("No such file", $e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function testSetType()
|
|
|
+ public function testSetAdapter()
|
|
|
{
|
|
|
- $barcode = new Zend_Validate_Barcode('upc-a');
|
|
|
+ $barcode = new Zend_Validate_Barcode('upca');
|
|
|
$this->assertTrue($barcode->isValid('065100004327'));
|
|
|
|
|
|
- $barcode->setType('ean-13');
|
|
|
+ $barcode->setAdapter('ean13');
|
|
|
$this->assertTrue($barcode->isValid('0075678164125'));
|
|
|
}
|
|
|
|
|
|
@@ -94,12 +76,216 @@ class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
public function testNonStringValidation()
|
|
|
{
|
|
|
- $barcode = new Zend_Validate_Barcode('upc-a');
|
|
|
+ $barcode = new Zend_Validate_Barcode('upca');
|
|
|
$this->assertFalse($barcode->isValid(106510000.4327));
|
|
|
$this->assertFalse($barcode->isValid(array('065100004327')));
|
|
|
|
|
|
- $barcode = new Zend_Validate_Barcode('ean-13');
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean13');
|
|
|
$this->assertFalse($barcode->isValid(06510000.4327));
|
|
|
$this->assertFalse($barcode->isValid(array('065100004327')));
|
|
|
}
|
|
|
+
|
|
|
+ public function testInvalidChecksumAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode1');
|
|
|
+ $this->assertFalse($barcode->isValid('0000000'));
|
|
|
+ $this->assertTrue(array_key_exists('barcodeFailed', $barcode->getMessages()));
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checksum('0000000'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvalidCharAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode1.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode1');
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkChars(123));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testAscii128CharacterAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode2');
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkChars('1234QW!"'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvalidLengthAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode2');
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkLength(123));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testArrayLengthAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode2.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode2');
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('1'));
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkLength('12'));
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('123'));
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testArrayLengthAdapter2()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode3.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode3');
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('1'));
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('12'));
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('123'));
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('1234'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testOddLengthAdapter()
|
|
|
+ {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode4.php";
|
|
|
+ $barcode = new Zend_Validate_Barcode('MyBarcode4');
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('1'));
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkLength('12'));
|
|
|
+ $this->assertTrue($barcode->getAdapter()->checkLength('123'));
|
|
|
+ $this->assertFalse($barcode->getAdapter()->checkLength('1234'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvalidAdapter()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('Ean13');
|
|
|
+ try {
|
|
|
+ require_once dirname(__FILE__) . "/_files/MyBarcode5.php";
|
|
|
+ $barcode->setAdapter('MyBarcode5');
|
|
|
+ $this->fails('Exception expected');
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->assertContains('does not implement', $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testArrayConstructAdapter()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode(array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false));
|
|
|
+ $this->assertTrue($barcode->getAdapter() instanceof Zend_Validate_Barcode_Ean13);
|
|
|
+ $this->assertFalse($barcode->getChecksum());
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testInvalidArrayConstructAdapter()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $barcode = new Zend_Validate_Barcode(array('options' => 'unknown', 'checksum' => false));
|
|
|
+ $this->fails('Exception expected');
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->assertContains('Missing option', $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testConfigConstructAdapter()
|
|
|
+ {
|
|
|
+ $array = array('adapter' => 'Ean13', 'options' => 'unknown', 'checksum' => false);
|
|
|
+ require_once 'Zend/Config.php';
|
|
|
+ $config = new Zend_Config($array);
|
|
|
+
|
|
|
+ $barcode = new Zend_Validate_Barcode($config);
|
|
|
+ $this->assertTrue($barcode->isValid('0075678164125'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testEAN13()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean13');
|
|
|
+
|
|
|
+ $this->assertTrue($barcode->isValid('0075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('0075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testEAN12()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean12');
|
|
|
+ $this->assertTrue($barcode->isValid('075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testEAN14()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean14');
|
|
|
+ $this->assertTrue($barcode->isValid('00075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testEAN8()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('ean8');
|
|
|
+ $this->assertTrue($barcode->isValid('67816413'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('67816412'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGTIN12()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('gtin12');
|
|
|
+ $this->assertTrue($barcode->isValid('075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGTIN13()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('gtin13');
|
|
|
+ $this->assertTrue($barcode->isValid('0075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('0075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGTIN14()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('gtin14');
|
|
|
+ $this->assertTrue($barcode->isValid('00075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('00075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testCODE25()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('code25');
|
|
|
+ $this->assertTrue($barcode->isValid('00075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $barcode->setChecksum(true);
|
|
|
+ $this->assertFalse($barcode->isValid('00075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testCODE93()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('code93');
|
|
|
+ $this->assertTrue($barcode->isValid('TEST93TEST93TEST93TEST93Y+'));
|
|
|
+ $this->assertFalse($barcode->isValid('00075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testITF14()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('itf14');
|
|
|
+ $this->assertTrue($barcode->isValid('00075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('00075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testSSCC()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('sscc');
|
|
|
+ $this->assertTrue($barcode->isValid('000000075678164125'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('000000075678164124'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testUPCA()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('upca');
|
|
|
+ $this->assertTrue($barcode->isValid('065100004327'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('065100004328'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testUPCE()
|
|
|
+ {
|
|
|
+ $barcode = new Zend_Validate_Barcode('upce');
|
|
|
+ $this->assertTrue($barcode->isValid('123456'));
|
|
|
+ $this->assertFalse($barcode->isValid('123'));
|
|
|
+ $this->assertFalse($barcode->isValid('1234567'));
|
|
|
+ }
|
|
|
}
|