Skip to content

Commit e1d8d8c

Browse files
committed
Add concept of factory
1 parent e449149 commit e1d8d8c

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# php-object-graph-generator
1+
# PHP Object Graph Generator
2+
3+
This library helps you creating randomly populated object graphs inside your domain.
4+
5+
## Installation
6+
7+
```
8+
composer install cubicl/php-object-graph-generator
9+
```
10+
11+
## Usage
12+
13+
Support for factories, factory methods and constructor calls.

src/FactoryInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Cubicl\ObjectGraphGenerator;
4+
5+
use Cubicl\ObjectGraphGenerator\ObjectGraphGenerator;
6+
use Faker\Generator;
7+
8+
interface FactoryInterface
9+
{
10+
public function __invoke(ObjectGraphGenerator $objectGraphGenerator, Generator $faker): object;
11+
}

src/ObjectGraphGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class ObjectGraphGenerator
2222

2323
private PropertyInfoExtractor $propertyInfo;
2424

25-
private array $registry = [];
25+
private array $registry;
2626

27-
public function __construct()
27+
public function __construct(array $registry = [])
2828
{
2929
$this->fakerInstance = Factory::create();
3030

@@ -33,6 +33,7 @@ public function __construct()
3333
$typeExtractors = [$phpDocExtractor, $reflectionExtractor];
3434
$this->propertyInfo = new PropertyInfoExtractor([], $typeExtractors, [], [], []);
3535
$this->fakerInstance->seed(self::DEFAULT_SEED);
36+
$this->registry = $registry;
3637
}
3738

3839
public function generateWithSeed(string $className, int $seed): object
@@ -146,6 +147,6 @@ private function isInRegistry(string $key): bool
146147

147148
private function getFromRegistry(string $key)
148149
{
149-
return $this->registry[$key]();
150+
return $this->registry[$key]($this, $this->fakerInstance);
150151
}
151152
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Cubicl\ObjectGraphGenerator\Tests\Fixture\Factory;
4+
5+
use Cubicl\ObjectGraphGenerator\FactoryInterface;
6+
use Cubicl\ObjectGraphGenerator\ObjectGraphGenerator;
7+
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SelfReferencingObjects;
8+
use Faker\Generator;
9+
10+
class SelfReferencingObjectsFactory implements FactoryInterface
11+
{
12+
public function __invoke(ObjectGraphGenerator $objectGraphGenerator, Generator $faker): SelfReferencingObjects
13+
{
14+
return new SelfReferencingObjects(null, 1);
15+
}
16+
}

tests/Fixture/SimpleTypesInConstructor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77
class SimpleTypesInConstructor
88
{
99
private int $foo;
10+
private bool $bar;
1011

11-
public function __construct(int $foo)
12+
public function __construct(int $foo, bool $bar)
1213
{
1314
$this->foo = $foo;
15+
$this->bar = $bar;
1416
}
1517

1618
public function getFoo(): int
1719
{
1820
return $this->foo;
1921
}
22+
23+
public function isBar(): bool
24+
{
25+
return $this->bar;
26+
}
2027
}

tests/ObjectGraphGeneratorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Cubicl\ObjectGraphGenerator\ObjectGraphGenerator;
88
use Cubicl\ObjectGraphGenerator\Tests\Fixture\CircularReferenceObjectA;
9+
use Cubicl\ObjectGraphGenerator\Tests\Fixture\Factory\SelfReferencingObjectsFactory;
910
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SelfReferencingObjects;
1011
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInConstructor;
1112
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInCreateMethod;
@@ -39,6 +40,9 @@ public function dataForTest(): array
3940

4041
private function getUnitUnderTest(): ObjectGraphGenerator
4142
{
42-
return new ObjectGraphGenerator();
43+
return new ObjectGraphGenerator([
44+
SelfReferencingObjects::class => new SelfReferencingObjectsFactory(),
45+
'Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInConstructor:bar' => function () {return false;},
46+
]);
4347
}
4448
}

0 commit comments

Comments
 (0)