BarcodeTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Validate_BarcodeTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testUpcA()
  41. {
  42. $barcode = new Zend_Validate_Barcode('upc-a');
  43. $this->assertTrue($barcode->isValid('065100004327'));
  44. $this->assertFalse($barcode->isValid('123'));
  45. $this->assertFalse($barcode->isValid('065100004328'));
  46. }
  47. public function testEan13()
  48. {
  49. $barcode = new Zend_Validate_Barcode('ean-13');
  50. $this->assertTrue($barcode->isValid('0075678164125'));
  51. $this->assertFalse($barcode->isValid('123'));
  52. $this->assertFalse($barcode->isValid('0075678164124'));
  53. }
  54. /**
  55. * Test if EAN-13 contains only numeric characters
  56. *
  57. * @group ZF-3297
  58. */
  59. public function testEan13ContainsOnlyNumeric()
  60. {
  61. $barcode = new Zend_Validate_Barcode('ean-13');
  62. $this->assertFalse($barcode->isValid('3RH1131-1BB40'));
  63. }
  64. public function testNoneExisting()
  65. {
  66. try {
  67. $barcode = new Zend_Validate_Barcode('Zend');
  68. $this->fail("'Zend' is not a valid barcode type'");
  69. } catch (Exception $e) {
  70. $this->assertContains("'Zend' is not supported", $e->getMessage());
  71. }
  72. }
  73. public function testSetType()
  74. {
  75. $barcode = new Zend_Validate_Barcode('upc-a');
  76. $this->assertTrue($barcode->isValid('065100004327'));
  77. $barcode->setType('ean-13');
  78. $this->assertTrue($barcode->isValid('0075678164125'));
  79. }
  80. /**
  81. * @ZF-4352
  82. */
  83. public function testNonStringValidation()
  84. {
  85. $barcode = new Zend_Validate_Barcode('upc-a');
  86. $this->assertFalse($barcode->isValid(106510000.4327));
  87. $this->assertFalse($barcode->isValid(array('065100004327')));
  88. $barcode = new Zend_Validate_Barcode('ean-13');
  89. $this->assertFalse($barcode->isValid(06510000.4327));
  90. $this->assertFalse($barcode->isValid(array('065100004327')));
  91. }
  92. }