1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Model; |
6
|
|
|
|
7
|
|
|
use Application\Model\Collection; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class CollectionTest extends \PHPUnit\Framework\TestCase |
11
|
|
|
{ |
12
|
|
|
public function testChildCollectionsRelation(): void |
13
|
|
|
{ |
14
|
|
|
$parent = new Collection(); |
15
|
|
|
$child = new Collection(); |
16
|
|
|
self::assertCount(0, $parent->getChildren()); |
17
|
|
|
|
18
|
|
|
$child->setParent($parent); |
19
|
|
|
self::assertCount(1, $parent->getChildren()); |
20
|
|
|
self::assertSame($child, $parent->getChildren()[0]); |
21
|
|
|
|
22
|
|
|
$otherParent = new Collection(); |
23
|
|
|
self::assertCount(0, $otherParent->getChildren()); |
24
|
|
|
|
25
|
|
|
$child->setParent($otherParent); |
26
|
|
|
self::assertCount(0, $parent->getChildren()); |
27
|
|
|
self::assertCount(1, $otherParent->getChildren()); |
28
|
|
|
self::assertSame($child, $otherParent->getChildren()[0]); |
29
|
|
|
|
30
|
|
|
$child->setParent(null); |
31
|
|
|
self::assertCount(0, $parent->getChildren()); |
32
|
|
|
self::assertCount(0, $otherParent->getChildren()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testCannotCreateCyclicHierarchy(): void |
36
|
|
|
{ |
37
|
|
|
$parent = new Collection(); |
38
|
|
|
$child = new Collection(); |
39
|
|
|
$grandChild = new Collection(); |
40
|
|
|
|
41
|
|
|
$child->setParent($parent); |
42
|
|
|
$grandChild->setParent($child); |
43
|
|
|
|
44
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
45
|
|
|
self::expectExceptionMessage('Parent object is invalid because it would create a cyclic hierarchy'); |
|
|
|
|
46
|
|
|
$parent->setParent($grandChild); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testCannotCreateCyclicHierarchyAsMyOwnParent(): void |
50
|
|
|
{ |
51
|
|
|
$collection = new Collection(); |
52
|
|
|
|
53
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
54
|
|
|
self::expectExceptionMessage('An object cannot be his own parent'); |
|
|
|
|
55
|
|
|
$collection->setParent($collection); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|