Skip to content

Commit d1255d2

Browse files
authored
Merge pull request #2 from CubiclDev/circular-refs
Circular refs
2 parents ac31a90 + e1d8d8c commit d1255d2

12 files changed

+157
-19
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
composer.phar
1+
.idea/
22
/vendor/
3-
4-
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock

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.

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
convertErrorsToExceptions = "true"
77
convertNoticesToExceptions = "true"
88
convertWarningsToExceptions = "true">
9+
<testsuites>
10+
<testsuite name="Unit">
11+
<directory suffix="Test.php">./tests/</directory>
12+
</testsuite>
13+
</testsuites>
914
</phpunit>

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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,37 @@
1717

1818
class ObjectGraphGenerator
1919
{
20+
private const DEFAULT_SEED = 1;
2021
private Generator $fakerInstance;
2122

2223
private PropertyInfoExtractor $propertyInfo;
2324

24-
private array $registry = [];
25+
private array $registry;
2526

26-
public function __construct()
27+
public function __construct(array $registry = [])
2728
{
2829
$this->fakerInstance = Factory::create();
2930

3031
$phpDocExtractor = new PhpDocExtractor();
3132
$reflectionExtractor = new ReflectionExtractor();
3233
$typeExtractors = [$phpDocExtractor, $reflectionExtractor];
3334
$this->propertyInfo = new PropertyInfoExtractor([], $typeExtractors, [], [], []);
35+
$this->fakerInstance->seed(self::DEFAULT_SEED);
36+
$this->registry = $registry;
3437
}
3538

36-
public function generate(string $className, int $seed): object
39+
public function generateWithSeed(string $className, int $seed): object
3740
{
3841
$this->fakerInstance->seed($seed);
3942

4043
return $this->generateObject($className);
4144
}
4245

46+
public function generate(string $className): object
47+
{
48+
return $this->generateObject($className);
49+
}
50+
4351
/**
4452
* @param string $className
4553
*
@@ -139,6 +147,6 @@ private function isInRegistry(string $key): bool
139147

140148
private function getFromRegistry(string $key)
141149
{
142-
return $this->registry[$key]();
150+
return $this->registry[$key]($this, $this->fakerInstance);
143151
}
144152
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cubicl\ObjectGraphGenerator\Tests\Fixture;
6+
7+
class CircularReferenceObjectA
8+
{
9+
/** @var CircularReferenceObjectB|null */
10+
private $objectB;
11+
12+
public function getObjectB(): ?CircularReferenceObjectB
13+
{
14+
return $this->objectB;
15+
}
16+
17+
public function setObjectB(?CircularReferenceObjectB $objectB): void
18+
{
19+
$this->objectB = $objectB;
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cubicl\ObjectGraphGenerator\Tests\Fixture;
6+
7+
class CircularReferenceObjectB
8+
{
9+
/** @var CircularReferenceObjectA|null */
10+
private $objectA;
11+
12+
public function getObjectA(): ?CircularReferenceObjectA
13+
{
14+
return $this->objectA;
15+
}
16+
17+
public function setObjectA(CircularReferenceObjectA $objectA): void
18+
{
19+
$this->objectA = $objectA;
20+
}
21+
22+
23+
}
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cubicl\ObjectGraphGenerator\Tests\Fixture;
6+
7+
class SelfReferencingObjects
8+
{
9+
/** @var SelfReferencingObjects|null */
10+
private $reference;
11+
12+
/** @var int */
13+
private $number;
14+
15+
public function __construct(?SelfReferencingObjects $reference, int $number)
16+
{
17+
$this->reference = $reference;
18+
$this->number = $number;
19+
}
20+
21+
public function getReference(): ?SelfReferencingObjects
22+
{
23+
return $this->reference;
24+
}
25+
26+
public function getNumber(): int
27+
{
28+
return $this->number;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)