Skip to content

Commit

Permalink
Move separate test, remove doc block
Browse files Browse the repository at this point in the history
Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode committed Nov 23, 2022
1 parent 33d908e commit aef62a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
26 changes: 0 additions & 26 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,12 @@ class Generator
private int $value;
private int $checkDigit;

/**
* Generator constructor.
*
* @param int $numbers
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
public function __construct(string $numbers, string $format = self::GTIN_13)
{
$this->checkDigit = self::execute($numbers, $format);
$this->value = ($numbers * 10) + $this->checkDigit;
}

/**
* @param int $number
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function validateLength(string $number, string $format): void
{
$actualLength = strlen((string)$number);
Expand All @@ -63,11 +49,6 @@ private static function validateLength(string $number, string $format): void
}
}

/**
* @param string $format
*
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function validateFormat(string $format): void
{
if (! in_array($format, self::ID_KEY_FORMATS)) {
Expand Down Expand Up @@ -95,13 +76,6 @@ private static function nearestEqualOrHigherThenMultiplyByTen(int $number): int
return (int)ceil($number / 10) * 10;
}

/**
* @param int $numbers
* @param string $format
*
* @return int
* @throws \Lloricode\CheckDigit\Exceptions\ValidationException
*/
private static function execute(string $numbers, string $format = self::GTIN_13): int
{
self::validateFormat($format);
Expand Down
16 changes: 10 additions & 6 deletions tests/Feature/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,30 @@ public function generator()

$this->assertEquals(2, (new Generator(9638527, Generator::GTIN_8))->getCheckDigit());
$this->assertEquals(4, (new Generator(3216549, Generator::GTIN_8))->getCheckDigit());
$this->assertEquals(7, (new Generator("0012345", Generator::GTIN_8))->getCheckDigit());


$this->assertEquals(2, (new Generator(91739456321, Generator::GTIN_12))->getCheckDigit());
$this->assertEquals(8, (new Generator(74185245963, Generator::GTIN_12))->getCheckDigit());
$this->assertEquals(5, (new Generator("00123456789", Generator::GTIN_12))->getCheckDigit());

$this->assertEquals(3, (new Generator(629104150021, Generator::GTIN_13))->getCheckDigit());
$this->assertEquals(6, (new Generator(123456789876, Generator::GTIN_13))->getCheckDigit());
$this->assertEquals(5, (new Generator("001234567890", Generator::GTIN_13))->getCheckDigit());

$this->assertEquals(6, (new Generator(7539514528423, Generator::GTIN_14))->getCheckDigit());
$this->assertEquals(5, (new Generator(8563251459762, Generator::GTIN_14))->getCheckDigit());
$this->assertEquals(2, (new Generator("0012345678901", Generator::GTIN_14))->getCheckDigit());

$this->assertEquals(0, (new Generator(7896541230123456, Generator::GSIN))->getCheckDigit());
$this->assertEquals(3, (new Generator(7658485040650456, Generator::GSIN))->getCheckDigit());
$this->assertEquals(3, (new Generator("0012345678901234", Generator::GSIN))->getCheckDigit());

$this->assertEquals(6, (new Generator(95135623050123698, Generator::SSCC))->getCheckDigit());
$this->assertEquals(7, (new Generator(87643802105978513, Generator::SSCC))->getCheckDigit());
}

public function testWithStringAndStartWithZero()
{
$this->assertEquals(7, (new Generator("0012345", Generator::GTIN_8))->getCheckDigit());
$this->assertEquals(5, (new Generator("00123456789", Generator::GTIN_12))->getCheckDigit());
$this->assertEquals(5, (new Generator("001234567890", Generator::GTIN_13))->getCheckDigit());
$this->assertEquals(2, (new Generator("0012345678901", Generator::GTIN_14))->getCheckDigit());
$this->assertEquals(3, (new Generator("0012345678901234", Generator::GSIN))->getCheckDigit());
$this->assertEquals(2, (new Generator("00123456789012345", Generator::SSCC))->getCheckDigit());
}

Expand Down

0 comments on commit aef62a8

Please sign in to comment.