From 6fabbea91eb788c82915833ba8a8e2d5b3088e7d Mon Sep 17 00:00:00 2001 From: johnykvsky Date: Sun, 22 Dec 2024 17:00:25 +0100 Subject: [PATCH] more tests --- src/Container/DefinitionContainer.php | 6 --- src/Core/Randomizer/CoreRandomizer.php | 8 --- src/Core/Version.php | 12 ++--- src/Strategy/ValidStrategy.php | 10 ++-- test/Calculator/EanCalculatorTest.php | 30 +++++++++++ test/Calculator/IbanCalculatorTest.php | 27 ++++++++++ test/Calculator/IsbnCalculatorTest.php | 28 ++++++++++ test/Calculator/LuhnCalculatorTest.php | 34 +++++++++++++ test/Container/DefinitionContainerTest.php | 12 +++++ test/Extension/CoordinatesTest.php | 25 +++++++++ test/Extension/DateTimeTest.php | 2 +- test/Extension/NumberTest.php | 15 ++++++ test/Extension/PaymentTest.php | 59 ++++++++++++++++++---- test/Extension/VersionTest.php | 36 ++++++++++--- 14 files changed, 257 insertions(+), 47 deletions(-) create mode 100644 test/Calculator/EanCalculatorTest.php create mode 100644 test/Calculator/IbanCalculatorTest.php create mode 100644 test/Calculator/IsbnCalculatorTest.php create mode 100644 test/Calculator/LuhnCalculatorTest.php diff --git a/src/Container/DefinitionContainer.php b/src/Container/DefinitionContainer.php index 585871e..ef3a752 100644 --- a/src/Container/DefinitionContainer.php +++ b/src/Container/DefinitionContainer.php @@ -145,12 +145,6 @@ private function getService(string $id, callable|object|string $definition): Def $id )); } - - // @phpstan-ignore-next-line - throw new ContainerException(sprintf( - 'Invalid type for definition for "%s"', - $id - )); } private function handleAwareness(DefinitionInterface $extension): DefinitionInterface diff --git a/src/Core/Randomizer/CoreRandomizer.php b/src/Core/Randomizer/CoreRandomizer.php index ca3fd2d..2e2a0f4 100644 --- a/src/Core/Randomizer/CoreRandomizer.php +++ b/src/Core/Randomizer/CoreRandomizer.php @@ -40,10 +40,6 @@ public function randomLetter(): string public function randomElement(array $array): mixed { - if ($array === []) { - return null; - } - $array = $this->shuffleElements($array); return reset($array); @@ -51,10 +47,6 @@ public function randomElement(array $array): mixed public function randomKey(array $array = []): int|string|null { - if (!$array) { - return null; - } - return $this->randomElement(array_keys($array)); } diff --git a/src/Core/Version.php b/src/Core/Version.php index 020e38e..3d60b47 100644 --- a/src/Core/Version.php +++ b/src/Core/Version.php @@ -24,19 +24,19 @@ public function semver(bool $preRelease = false, bool $build = false): string $this->randomizer->getInt(0, 9), $this->randomizer->getInt(0, 99), $this->randomizer->getInt(0, 99), - $preRelease && $this->randomizer->getBool() ? '-' . $this->semverPreReleaseIdentifier() : '', - $build && $this->randomizer->getBool() ? '+' . $this->semverBuildIdentifier() : '' + $preRelease ? '-' . $this->semverPreReleaseIdentifier($this->randomizer->getBool()) : '', + $build ? '+' . $this->semverBuildIdentifier($this->randomizer->getBool()) : '' ); } /** * Common pre-release identifier */ - private function semverPreReleaseIdentifier(): string + private function semverPreReleaseIdentifier(bool $short = true): string { $ident = $this->randomizer->randomElement($this->semverCommonPreReleaseIdentifiers); - if ($this->randomizer->getBool()) { + if ($short) { return $ident; } @@ -46,9 +46,9 @@ private function semverPreReleaseIdentifier(): string /** * Common random build identifier */ - private function semverBuildIdentifier(): string + private function semverBuildIdentifier(bool $shortSyntax = true): string { - if ($this->randomizer->getBool()) { + if ($shortSyntax) { // short git revision syntax: https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection return substr(sha1(uniqid('', true)), 0, 7); } diff --git a/src/Strategy/ValidStrategy.php b/src/Strategy/ValidStrategy.php index 3a26e48..78f2ab4 100644 --- a/src/Strategy/ValidStrategy.php +++ b/src/Strategy/ValidStrategy.php @@ -22,21 +22,17 @@ class ValidStrategy implements StrategyInterface * return $digit % 2 === 0; * }; * for ($i=0; $i < 10; $i++) { - * $values []= $generator->valid($evenValidator)->randomDigit; + * $values []= $generator->withStrategy(new ValidStrategy($evenValidator))->randomDigit(); * } * print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6] * a * - * @param null|callable(?mixed $value):bool $validator A function returning true for valid values + * @param callable(?mixed $value):bool $validator A function returning true for valid values * @param int $retries Maximum number of retries to find a valid value, * After which an OverflowException is thrown. */ - public function __construct(?callable $validator, private readonly int $retries = 10000) + public function __construct(callable $validator, private readonly int $retries = 10000) { - if ($validator === null) { - $validator = (static fn() => true)(...); - } - $this->validator = $validator(...); } diff --git a/test/Calculator/EanCalculatorTest.php b/test/Calculator/EanCalculatorTest.php new file mode 100644 index 0000000..d607f19 --- /dev/null +++ b/test/Calculator/EanCalculatorTest.php @@ -0,0 +1,30 @@ +checksum('9638507')); + self::assertEquals(7, $calculator->checksum('590123412345')); + } + + public function testIsValid(): void + { + $calculator = new EanCalculator; + + self::assertTrue($calculator->isValid(self::EAN8)); + self::assertTrue($calculator->isValid(self::EAN13)); + } +} diff --git a/test/Calculator/IbanCalculatorTest.php b/test/Calculator/IbanCalculatorTest.php new file mode 100644 index 0000000..27d8c62 --- /dev/null +++ b/test/Calculator/IbanCalculatorTest.php @@ -0,0 +1,27 @@ +checksum('IE64IRCE92050112345678')); + } + + public function testIsValid(): void + { + $calculator = new IbanCalculator(); + + self::assertTrue($calculator->isValid(self::IBAN)); + } +} diff --git a/test/Calculator/IsbnCalculatorTest.php b/test/Calculator/IsbnCalculatorTest.php new file mode 100644 index 0000000..1ab2a6d --- /dev/null +++ b/test/Calculator/IsbnCalculatorTest.php @@ -0,0 +1,28 @@ +checksum('212345680')); + } + + public function testIsValid(): void + { + $calculator = new IsbnCalculator(); + + self::assertTrue($calculator->isValid(self::ISBN10)); + } +} diff --git a/test/Calculator/LuhnCalculatorTest.php b/test/Calculator/LuhnCalculatorTest.php new file mode 100644 index 0000000..47a76eb --- /dev/null +++ b/test/Calculator/LuhnCalculatorTest.php @@ -0,0 +1,34 @@ +isValid(self::LUHN)); + } + + public function testComputeCheckDigit(): void + { + $calculator = new LuhnCalculator(); + + self::assertEquals('4', $calculator->computeCheckDigit('1789372997')); + } + + public function testGenerateLuhnNumber(): void + { + $calculator = new LuhnCalculator(); + + self::assertEquals(self::LUHN, $calculator->generateLuhnNumber('1789372997')); + } +} diff --git a/test/Container/DefinitionContainerTest.php b/test/Container/DefinitionContainerTest.php index 65a9ae0..8184b4e 100644 --- a/test/Container/DefinitionContainerTest.php +++ b/test/Container/DefinitionContainerTest.php @@ -44,6 +44,18 @@ public function testThrowsExceptionIfDefinitionNotInContainer(): void $container->get('some_name'); } + public function testContainerOnlyHoldsDefinitions(): void + { + $testClass = new \stdClass(); + $definitions = ['some_name' => $testClass::class]; + + // @phpstan-ignore-next-line + $container = new DefinitionContainer($definitions); + + $this->expectException(ContainerException::class); + $container->get('some_name'); + } + public function testCanAddDefinitionToContainer(): void { $container = new DefinitionContainer([]); diff --git a/test/Extension/CoordinatesTest.php b/test/Extension/CoordinatesTest.php index 7bddfae..a572f83 100644 --- a/test/Extension/CoordinatesTest.php +++ b/test/Extension/CoordinatesTest.php @@ -6,6 +6,7 @@ use DummyGenerator\Container\DefinitionContainer; use DummyGenerator\Definitions\Extension\CoordinatesExtensionInterface; +use DummyGenerator\Definitions\Extension\Exception\ExtensionLogicException; use DummyGenerator\Definitions\Randomizer\RandomizerInterface; use DummyGenerator\DummyGenerator; use DummyGenerator\Core\Coordinates; @@ -33,6 +34,18 @@ public function testLatitude(): void self::assertTrue($latitude >= $min && $latitude <= $max); } + public function testLatitudeMinimumException(): void + { + $this->expectException(ExtensionLogicException::class); + $this->generator->latitude(min: -100.0, max: -100.0); + } + + public function testLatitudeMaximumException(): void + { + $this->expectException(ExtensionLogicException::class); + $this->generator->latitude(min: 100.0, max: 100.0); + } + public function testLongitude(): void { $longitude = $this->generator->longitude(min: $min = -10.0, max: $max = 10.0); @@ -40,6 +53,18 @@ public function testLongitude(): void self::assertTrue($longitude >= $min && $longitude <= $max); } + public function testLongitudeMinimumException(): void + { + $this->expectException(ExtensionLogicException::class); + $this->generator->longitude(min: -200.0, max: -200.0); + } + + public function testLongitudeMaximumException(): void + { + $this->expectException(ExtensionLogicException::class); + $this->generator->longitude(min: 200.0, max: 200.0); + } + public function testCoordinates(): void { $coordinates = $this->generator->coordinates(); diff --git a/test/Extension/DateTimeTest.php b/test/Extension/DateTimeTest.php index fcbe734..586c0eb 100644 --- a/test/Extension/DateTimeTest.php +++ b/test/Extension/DateTimeTest.php @@ -79,7 +79,7 @@ public function testDateTimeThisMonth(): void $date1 = new \DateTimeImmutable($this->now->format('Y-m') . '-01', new \DateTimeZone('UTC')); $date2 = new \DateTimeImmutable($this->now->format('Y-m-t'), new \DateTimeZone('UTC')); - $date = $this->generator->dateTimeThisWeek(until: $date2, timezone: 'UTC'); + $date = $this->generator->dateTimeThisMonth(until: $date2, timezone: 'UTC'); self::assertTrue($date >= $date1 && $date <= $date2); } diff --git a/test/Extension/NumberTest.php b/test/Extension/NumberTest.php index c3062ad..a7d0c10 100644 --- a/test/Extension/NumberTest.php +++ b/test/Extension/NumberTest.php @@ -61,6 +61,14 @@ public function testRandomFloat(): void self::assertTrue($number >= 12.83 && $number <= 26.45); } + public function testRandomFloatRandomDecimals(): void + { + $number = $this->generator->randomFloat(nbMaxDecimals: null, min: 12.83, max: 26.45); + + // TODO check for decimals number + self::assertTrue($number >= 12.83 && $number <= 26.45); + } + public function testRandomNumber(): void { $number = $this->generator->randomNumber(nbDigits: 3, strict: true); @@ -68,6 +76,13 @@ public function testRandomNumber(): void self::assertTrue($number >= 100 && $number <= 999); } + public function testRandomNumberRandomDigits(): void + { + $number = $this->generator->randomNumber(nbDigits: null, strict: false); + + self::assertTrue($number >= 0); + } + public function testBoolean(): void { self::assertTrue($this->generator->boolean(chanceOfGettingTrue: 100)); diff --git a/test/Extension/PaymentTest.php b/test/Extension/PaymentTest.php index 7790e44..5e243b3 100644 --- a/test/Extension/PaymentTest.php +++ b/test/Extension/PaymentTest.php @@ -10,6 +10,7 @@ use DummyGenerator\Core\DateTime; use DummyGenerator\Core\Payment; use DummyGenerator\Core\Person; +use DummyGenerator\Core\Randomizer\XoshiroRandomizer; use DummyGenerator\Definitions\Calculator\IbanCalculatorInterface; use DummyGenerator\Definitions\Calculator\LuhnCalculatorInterface; use DummyGenerator\Definitions\Extension\DateTimeExtensionInterface; @@ -32,16 +33,7 @@ public function setUp(): void { parent::setUp(); - $container = new DefinitionContainer([]); - $container->add(RandomizerInterface::class, Randomizer::class); - $container->add(TransliteratorInterface::class, Transliterator::class); - $container->add(ReplacerInterface::class, Replacer::class); - $container->add(IbanCalculatorInterface::class, IbanCalculator::class); - $container->add(LuhnCalculatorInterface::class, LuhnCalculator::class); - $container->add(DateTimeExtensionInterface::class, DateTime::class); - $container->add(PersonExtensionInterface::class, Person::class); - $container->add(PaymentExtensionInterface::class, Payment::class); - $this->generator = new DummyGenerator($container); + $this->generator = $this->generator(); } public function testCreditCardNumber(): void @@ -68,7 +60,7 @@ public function testCreditCardDetails(): void self::assertCount(4, $this->generator->creditCardDetails(valid: false)); } - public function testIban(): void + public function testIbanN(): void { $iban = $this->generator->iban(alpha2: 'PL', prefix: 'RR'); @@ -80,4 +72,49 @@ public function testSwiftBicNumber(): void { self::assertEquals(11, strlen($this->generator->swiftBicNumber())); } + + public function testIbanC(): void + { + $iban = $this->generator->iban(alpha2: 'MD', prefix: 'RR'); + + self::assertTrue(str_contains($iban, 'RR')); + self::assertTrue(strlen($iban) > 10); + } + + public function testIbanA(): void + { + $generator = $this->generator(8); + $iban = $generator->iban(alpha2: 'AZ', prefix: 'RR'); + + self::assertTrue(str_contains($iban, 'RR')); + self::assertTrue(strlen($iban) > 10); + } + + public function testDefaultFormat(): void + { + $iban = $this->generator->iban(alpha2: 'XX', prefix: 'RR'); + + self::assertTrue(str_contains($iban, 'RR')); + self::assertTrue(strlen($iban) > 24); + } + + private function generator(?int $seed = null): DummyGenerator + { + $container = new DefinitionContainer([]); + + if ($seed !== null) { + $container->add(RandomizerInterface::class, new XoshiroRandomizer(seed: $seed)); + } else { + $container->add(RandomizerInterface::class, Randomizer::class); + } + + $container->add(TransliteratorInterface::class, Transliterator::class); + $container->add(ReplacerInterface::class, Replacer::class); + $container->add(IbanCalculatorInterface::class, IbanCalculator::class); + $container->add(LuhnCalculatorInterface::class, LuhnCalculator::class); + $container->add(DateTimeExtensionInterface::class, DateTime::class); + $container->add(PersonExtensionInterface::class, Person::class); + $container->add(PaymentExtensionInterface::class, Payment::class); + return new DummyGenerator($container); + } } diff --git a/test/Extension/VersionTest.php b/test/Extension/VersionTest.php index 47c3e15..55e4ea1 100644 --- a/test/Extension/VersionTest.php +++ b/test/Extension/VersionTest.php @@ -5,6 +5,7 @@ namespace DummyGenerator\Test\Extension; use DummyGenerator\Container\DefinitionContainer; +use DummyGenerator\Core\Randomizer\XoshiroRandomizer; use DummyGenerator\Definitions\Extension\VersionExtensionInterface; use DummyGenerator\Definitions\Randomizer\RandomizerInterface; use DummyGenerator\DummyGenerator; @@ -20,10 +21,7 @@ public function setUp(): void { parent::setUp(); - $container = new DefinitionContainer([]); - $container->add(RandomizerInterface::class, Randomizer::class); - $container->add(VersionExtensionInterface::class, Version::class); - $this->generator = new DummyGenerator($container); + $this->generator = $this->generator(); } public function testSemver(): void @@ -31,9 +29,31 @@ public function testSemver(): void self::assertCount(3, explode('.', $this->generator->semver())); } - public function testSemverPreReleaseAndBuild(): void + public function testSemverPreReleaseAndBuildShortSyntax(): void + { + $generator = $this->generator(8); + + self::assertNotEmpty($generator->semver(preRelease: true, build: true)); + } + + public function testSemverPreReleaseAndBuildLongSyntax(): void + { + $generator = $this->generator(9); + + self::assertNotEmpty($generator->semver(preRelease: true, build: true)); + } + + private function generator(?int $seed = null): DummyGenerator { - // since preRelease and build are randomly added, not much to verify - self::assertNotEmpty($this->generator->semver(preRelease: true, build: true)); + $container = new DefinitionContainer([]); + + if ($seed !== null) { + $container->add(RandomizerInterface::class, new XoshiroRandomizer(seed: $seed)); + } else { + $container->add(RandomizerInterface::class, Randomizer::class); + } + + $container->add(VersionExtensionInterface::class, Version::class); + return new DummyGenerator($container); } -} \ No newline at end of file +}