Skip to content

Commit

Permalink
allow changing implementations on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
johnykvsky committed Dec 23, 2024
1 parent cd2b039 commit 3f4a729
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ I needed simple dummy data generator for PHP 8.3 and with modern architecture in
* all `mt_rand` / `array_rand` replaced with `\Random\Randomizer`
* no static methods, only one magic method (`__call()` in generator)
* interfaces and dependency injection for everything (all core implementations can be replaced with different ones)
* implementations can be changed on the fly with `addDefinition()`
* language providers removed from core (that makes it ~9.8Mb smaller)
* removed database providers (core is only for dummy data generation)
* removed `HmlLorem`, `Uuid` (you can use any uuid generator, Symfony, Ramsey...)
Expand Down
8 changes: 8 additions & 0 deletions docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ No problem, just try this:
echo $generator->boo('School'); // gives 'School is a crap!'
```

But what if I want to add/replace some definition "on the fly"? It can be done like this:

```php
$generator->firstName(); // will generate i.e. "Harry"
$generator->addDefinition(PersonExtensionInterface::class, ElvesPerson::class);
$generator->firstName(); // will generate i.e. "Fingolfin"
```

## Text

Text extension is a bit different for one reason - it uses external `txt` file as source to large test. By default it's in `resources/en_US.txt` but you can either:
Expand Down
2 changes: 1 addition & 1 deletion src/Container/DefinitionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function has(string $id): bool
/**
* Add new definition
*
* @param callable(): DefinitionInterface|DefinitionInterface|class-string<DefinitionInterface> $value
* @param DefinitionInterface|class-string<DefinitionInterface>|callable(): DefinitionInterface $value
* @param string $name
*/
public function add(string $name, callable|DefinitionInterface|string $value): void
Expand Down
3 changes: 1 addition & 2 deletions src/Container/DefinitionContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace DummyGenerator\Container;

use DummyGenerator\Definitions\DefinitionInterface;
use DummyGenerator\Definitions\Extension\ExtensionInterface;

interface DefinitionContainerInterface
{
public function get(string $id): DefinitionInterface;
public function has(string $id): bool;
public function add(string $name, callable|ExtensionInterface|string $value): void;
public function add(string $name, callable|DefinitionInterface|string $value): void;
public function findProcessor(string $name): null|DefinitionInterface;
}
11 changes: 11 additions & 0 deletions src/DummyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public function ext(string $id): DefinitionInterface
return $this->handleAwareness($extension);
}

/**
* Add new definition
*
* @param DefinitionInterface|class-string<DefinitionInterface>|callable():DefinitionInterface $value
* @param string $name
*/
public function addDefinition(string $name, callable|DefinitionInterface|string $value): void
{
$this->container->add($name, $value);
}

/**
* Replaces tokens ('{{ tokenName }}') in given string with the result from the token method call
*/
Expand Down
37 changes: 14 additions & 23 deletions test/Extension/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ public function setUp(): void
{
parent::setUp();

$this->generator = $this->generator();
$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);
}

public function testCreditCardNumber(): void
Expand Down Expand Up @@ -83,8 +94,8 @@ public function testIbanC(): void

public function testIbanA(): void
{
$generator = $this->generator(8);
$iban = $generator->iban(alpha2: 'AZ', prefix: 'RR');
$this->generator->addDefinition(RandomizerInterface::class, new XoshiroRandomizer(seed: 8));
$iban = $this->generator->iban(alpha2: 'AZ', prefix: 'RR');

self::assertTrue(str_contains($iban, 'RR'));
self::assertTrue(strlen($iban) > 10);
Expand All @@ -97,24 +108,4 @@ public function testDefaultFormat(): void
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);
}
}
7 changes: 1 addition & 6 deletions test/Extension/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ public function testRealTextConstructor(): void
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOT;

$container = new DefinitionContainer([]);
$container->add(RandomizerInterface::class, Randomizer::class);
$container->add(TransliteratorInterface::class, Transliterator::class);
$container->add(ReplacerInterface::class, Replacer::class);
$container->add(TextExtensionInterface::class, new Text($text));
$this->generator = new DummyGenerator($container);
$this->generator->addDefinition(TextExtensionInterface::class, new Text($text));

$realText = $this->generator->realText(min: 5, max: 50, indexSize: 3);

Expand Down
28 changes: 9 additions & 19 deletions test/Extension/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public function setUp(): void
{
parent::setUp();

$this->generator = $this->generator();
$container = new DefinitionContainer([]);
$container->add(RandomizerInterface::class, Randomizer::class);

$container->add(VersionExtensionInterface::class, Version::class);
$this->generator = new DummyGenerator($container);
}

public function testSemver(): void
Expand All @@ -31,29 +35,15 @@ public function testSemver(): void

public function testSemverPreReleaseAndBuildShortSyntax(): void
{
$generator = $this->generator(8);
$this->generator->addDefinition(RandomizerInterface::class, new XoshiroRandomizer(seed: 8));

self::assertNotEmpty($generator->semver(preRelease: true, build: true));
self::assertNotEmpty($this->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
{
$container = new DefinitionContainer([]);
$this->generator->addDefinition(RandomizerInterface::class, new XoshiroRandomizer(seed: 9));

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);
self::assertNotEmpty($this->generator->semver(preRelease: true, build: true));
}
}
11 changes: 11 additions & 0 deletions test/Generator/DummyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,15 @@ public function testCanOverwriteExtension(): void
$this->expectException(\InvalidArgumentException::class);
self::assertEquals('foobar', $generator->foo());
}

public function testAddDefinition(): void
{
$container = new DefinitionContainer([]);
$container->add(FooProvider::class, new FooProvider());

$generator = new DummyGenerator($container);
$generator->addDefinition(FooProvider::class, new BazProvider());

self::assertEquals('baz', $generator->baz());
}
}

0 comments on commit 3f4a729

Please sign in to comment.