BarcodeTest.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Generator;
  4. use Faker\Provider\Barcode;
  5. class BarcodeTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $faker;
  8. public function setUp()
  9. {
  10. $faker = new Generator();
  11. $faker->addProvider(new Barcode($faker));
  12. $faker->seed(0);
  13. $this->faker = $faker;
  14. }
  15. public function testEan8()
  16. {
  17. $code = $this->faker->ean8();
  18. $this->assertRegExp('/^\d{8}$/i', $code);
  19. $codeWithoutChecksum = substr($code, 0, -1);
  20. $checksum = substr($code, -1);
  21. $this->assertEquals(TestableBarcode::eanChecksum($codeWithoutChecksum), $checksum);
  22. }
  23. public function testEan13()
  24. {
  25. $code = $this->faker->ean13();
  26. $this->assertRegExp('/^\d{13}$/i', $code);
  27. $codeWithoutChecksum = substr($code, 0, -1);
  28. $checksum = substr($code, -1);
  29. $this->assertEquals(TestableBarcode::eanChecksum($codeWithoutChecksum), $checksum);
  30. }
  31. }
  32. class TestableBarcode extends Barcode
  33. {
  34. public static function eanChecksum($input)
  35. {
  36. return parent::eanChecksum($input);
  37. }
  38. }