BarcodeTest.php 1.1 KB

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