Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnykvsky committed Dec 22, 2024
1 parent c31469d commit 6fabbea
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 47 deletions.
6 changes: 0 additions & 6 deletions src/Container/DefinitionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/Core/Randomizer/CoreRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,13 @@ public function randomLetter(): string

public function randomElement(array $array): mixed
{
if ($array === []) {
return null;
}

$array = $this->shuffleElements($array);

return reset($array);
}

public function randomKey(array $array = []): int|string|null
{
if (!$array) {
return null;
}

return $this->randomElement(array_keys($array));
}

Expand Down
12 changes: 6 additions & 6 deletions src/Core/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
10 changes: 3 additions & 7 deletions src/Strategy/ValidStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
* </code>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(...);
}

Expand Down
30 changes: 30 additions & 0 deletions test/Calculator/EanCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Calculator;

use DummyGenerator\Core\Calculator\EanCalculator;
use PHPUnit\Framework\TestCase;

class EanCalculatorTest extends TestCase
{
private const string EAN8 = '96385074';
private const string EAN13 = '5901234123457';

public function testChecksum(): void
{
$calculator = new EanCalculator;

self::assertEquals(4, $calculator->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));
}
}
27 changes: 27 additions & 0 deletions test/Calculator/IbanCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Calculator;

use DummyGenerator\Core\Calculator\IbanCalculator;
use PHPUnit\Framework\TestCase;

class IbanCalculatorTest extends TestCase
{
private const string IBAN = 'IE64IRCE92050112345678';

public function testChecksum(): void
{
$calculator = new IbanCalculator();

self::assertEquals(64, $calculator->checksum('IE64IRCE92050112345678'));
}

public function testIsValid(): void
{
$calculator = new IbanCalculator();

self::assertTrue($calculator->isValid(self::IBAN));
}
}
28 changes: 28 additions & 0 deletions test/Calculator/IsbnCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Calculator;

use DummyGenerator\Core\Calculator\IsbnCalculator;
use PHPUnit\Framework\TestCase;

class IsbnCalculatorTest extends TestCase
{
private const string ISBN10 = '2123456802';
// private const string ISBN13 = '9782123456803';

public function testChecksum(): void
{
$calculator = new IsbnCalculator();

self::assertEquals(2, $calculator->checksum('212345680'));
}

public function testIsValid(): void
{
$calculator = new IsbnCalculator();

self::assertTrue($calculator->isValid(self::ISBN10));
}
}
34 changes: 34 additions & 0 deletions test/Calculator/LuhnCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Calculator;

use DummyGenerator\Core\Calculator\LuhnCalculator;
use PHPUnit\Framework\TestCase;

class LuhnCalculatorTest extends TestCase
{
private const string LUHN = '17893729974';

public function testIsValid(): void
{
$calculator = new LuhnCalculator();

self::assertTrue($calculator->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'));
}
}
12 changes: 12 additions & 0 deletions test/Container/DefinitionContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down
25 changes: 25 additions & 0 deletions test/Extension/CoordinatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,13 +34,37 @@ 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);

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();
Expand Down
2 changes: 1 addition & 1 deletion test/Extension/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions test/Extension/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,28 @@ 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);

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));
Expand Down
Loading

0 comments on commit 6fabbea

Please sign in to comment.