Skip to content

Commit 8071370

Browse files
committed
Add temporary registry
1 parent 47828a3 commit 8071370

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/ObjectGraphGenerator.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class ObjectGraphGenerator
2828
/** @var array */
2929
private $registry;
3030

31+
/** @var array */
32+
private $temporaryRegistry = [];
33+
3134
public function __construct(array $registry = [])
3235
{
3336
$this->fakerInstance = Factory::create();
@@ -40,16 +43,18 @@ public function __construct(array $registry = [])
4043
$this->registry = $registry;
4144
}
4245

43-
public function generateWithSeed(string $className, int $seed): object
46+
public function generate(string $className): object
4447
{
45-
$this->fakerInstance->seed($seed);
46-
4748
return $this->generateObject($className);
4849
}
4950

50-
public function generate(string $className): object
51+
public function generateWithTemporaryConfig(string $className, array $config): object
5152
{
52-
return $this->generateObject($className);
53+
$this->temporaryRegistry = $config;
54+
$object = $this->generateObject($className);
55+
$this->temporaryRegistry = [];
56+
57+
return $object;
5358
}
5459

5560
/**
@@ -146,11 +151,14 @@ function () use ($argumentName, $className, $type) {
146151

147152
private function isInRegistry(string $key): bool
148153
{
149-
return array_key_exists($key, $this->registry);
154+
return array_key_exists($key, $this->temporaryRegistry) || array_key_exists($key, $this->registry);
150155
}
151156

152157
private function getFromRegistry(string $key)
153158
{
159+
if (isset($this->temporaryRegistry[$key])) {
160+
return $this->temporaryRegistry[$key]($this, $this->fakerInstance);
161+
}
154162
return $this->registry[$key]($this, $this->fakerInstance);
155163
}
156164
}

tests/ObjectGraphGeneratorTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public function itShouldGenerateObjectOfGivenType(string $class): void
2828
$this->assertInstanceOf($class, $actual);
2929
}
3030

31+
/**
32+
* @test
33+
*/
34+
public function itShouldUseTemporaryConfigBeforeRegistry(): void
35+
{
36+
$objectGraphGenerator = $this->getUnitUnderTest();
37+
/** @var SimpleTypesInConstructor $actual */
38+
$actual = $objectGraphGenerator->generateWithTemporaryConfig(SimpleTypesInConstructor::class, [
39+
'Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInConstructor:bar' => function () {return true;},
40+
]);
41+
42+
$this->assertTrue($actual->isBar());
43+
}
44+
3145
public function dataForTest(): array
3246
{
3347
return [

0 commit comments

Comments
 (0)