Skip to content

Commit e449149

Browse files
committed
Add test fixtures for circular references
1 parent ac31a90 commit e449149

File tree

8 files changed

+100
-13
lines changed

8 files changed

+100
-13
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

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/ObjectGraphGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

2223
private PropertyInfoExtractor $propertyInfo;
@@ -31,15 +32,21 @@ public function __construct()
3132
$reflectionExtractor = new ReflectionExtractor();
3233
$typeExtractors = [$phpDocExtractor, $reflectionExtractor];
3334
$this->propertyInfo = new PropertyInfoExtractor([], $typeExtractors, [], [], []);
35+
$this->fakerInstance->seed(self::DEFAULT_SEED);
3436
}
3537

36-
public function generate(string $className, int $seed): object
38+
public function generateWithSeed(string $className, int $seed): object
3739
{
3840
$this->fakerInstance->seed($seed);
3941

4042
return $this->generateObject($className);
4143
}
4244

45+
public function generate(string $className): object
46+
{
47+
return $this->generateObject($className);
48+
}
49+
4350
/**
4451
* @param string $className
4552
*
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: 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+
}

tests/ObjectGraphGeneratorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Cubicl\ObjectGraphGenerator\Tests;
66

77
use Cubicl\ObjectGraphGenerator\ObjectGraphGenerator;
8+
use Cubicl\ObjectGraphGenerator\Tests\Fixture\CircularReferenceObjectA;
9+
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SelfReferencingObjects;
810
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInConstructor;
911
use Cubicl\ObjectGraphGenerator\Tests\Fixture\SimpleTypesInCreateMethod;
1012
use PHPUnit\Framework\TestCase;
@@ -20,7 +22,7 @@ class ObjectGraphGeneratorTest extends TestCase
2022
public function itShouldGenerateObjectOfGivenType(string $class): void
2123
{
2224
$objectGraphGenerator = $this->getUnitUnderTest();
23-
$actual = $objectGraphGenerator->generate($class, 1);
25+
$actual = $objectGraphGenerator->generate($class);
2426

2527
$this->assertInstanceOf($class, $actual);
2628
}
@@ -30,6 +32,8 @@ public function dataForTest(): array
3032
return [
3133
'simple types in constructor' => [SimpleTypesInConstructor::class],
3234
'simple types in create method' => [SimpleTypesInCreateMethod::class],
35+
'self referencing objects' => [SelfReferencingObjects::class],
36+
'circular referencing objects' => [CircularReferenceObjectA::class],
3337
];
3438
}
3539

0 commit comments

Comments
 (0)