|
13 | 13 | use ReflectionMethod; |
14 | 14 |
|
15 | 15 | #[CoversClass(Node::class)] |
16 | | -class NodeTest extends AbstractTestCase |
| 16 | +final class NodeTest extends AbstractTestCase |
17 | 17 | { |
18 | | - /** |
19 | | - * SetUp for AddNode |
20 | | - */ |
21 | | - public function testAddNode(): void |
| 18 | + public function testNewNode(): void |
22 | 19 | { |
23 | | - $parent = new Node('parent'); |
24 | | - $child = new Node('child'); |
25 | | - $parent->addChild($child); |
26 | | - $this->assertEquals( |
27 | | - $parent->getChild($child->name), |
28 | | - $child, |
29 | | - ); |
30 | | - $this->assertEquals( |
31 | | - $parent->getChild($child->realName, true), |
32 | | - $child, |
33 | | - ); |
| 20 | + $node = new Node('Object Node'); |
| 21 | + $this->assertSame('Object Node', $node->name); |
| 22 | + $this->assertSame('Object Node', $node->realName); |
| 23 | + $this->assertSame(Node::OBJECT, $node->type); |
| 24 | + $this->assertFalse($node->isGroup); |
34 | 25 | } |
35 | 26 |
|
36 | | - /** |
37 | | - * SetUp for getChild |
38 | | - */ |
39 | | - public function testGetChildError(): void |
| 27 | + public function testNewNodeWithEmptyName(): void |
| 28 | + { |
| 29 | + $node = new Node(''); |
| 30 | + $this->assertSame('', $node->name); |
| 31 | + $this->assertSame('', $node->realName); |
| 32 | + $this->assertSame(Node::OBJECT, $node->type); |
| 33 | + $this->assertFalse($node->isGroup); |
| 34 | + } |
| 35 | + |
| 36 | + public function testNewContainerNode(): void |
| 37 | + { |
| 38 | + $node = new Node('Container Node', Node::CONTAINER); |
| 39 | + $this->assertSame('Container Node', $node->name); |
| 40 | + $this->assertSame('Container Node', $node->realName); |
| 41 | + $this->assertSame(Node::CONTAINER, $node->type); |
| 42 | + $this->assertFalse($node->isGroup); |
| 43 | + } |
| 44 | + |
| 45 | + public function testNewGroupNode(): void |
| 46 | + { |
| 47 | + $node = new Node('Group Node', Node::OBJECT, true); |
| 48 | + $this->assertSame('Group Node', $node->name); |
| 49 | + $this->assertSame('Group Node', $node->realName); |
| 50 | + $this->assertSame(Node::OBJECT, $node->type); |
| 51 | + $this->assertTrue($node->isGroup); |
| 52 | + } |
| 53 | + |
| 54 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 55 | + public function testAddChildNode(): void |
40 | 56 | { |
41 | 57 | $parent = new Node('parent'); |
42 | | - $this->assertNull( |
43 | | - $parent->getChild('foo'), |
44 | | - ); |
45 | | - $this->assertNull( |
46 | | - $parent->getChild('foo', true), |
47 | | - ); |
| 58 | + $childOne = new Node('child one'); |
| 59 | + $childTwo = new Node('child two'); |
| 60 | + $this->assertSame([], $parent->children); |
| 61 | + $this->assertNull($childOne->parent); |
| 62 | + $this->assertNull($childTwo->parent); |
| 63 | + $parent->addChild($childOne); |
| 64 | + $this->assertSame([$childOne], $parent->children); |
| 65 | + $this->assertSame($parent, $childOne->parent); |
| 66 | + $this->assertNull($childTwo->parent); |
| 67 | + $parent->addChild($childTwo); |
| 68 | + $this->assertSame([$childOne, $childTwo], $parent->children); |
| 69 | + $this->assertSame($parent, $childOne->parent); |
| 70 | + $this->assertSame($parent, $childTwo->parent); |
48 | 71 | } |
49 | 72 |
|
50 | | - /** |
51 | | - * SetUp for getChild |
52 | | - */ |
53 | | - public function testRemoveNode(): void |
| 73 | + public function testGetChildNode(): void |
54 | 74 | { |
55 | 75 | $parent = new Node('parent'); |
56 | | - $child = new Node('child'); |
| 76 | + $child = new Node('child real name'); |
| 77 | + $child->name = 'child'; |
| 78 | + $this->assertNull($parent->getChild('child')); |
| 79 | + $this->assertNull($parent->getChild('child', true)); |
| 80 | + $this->assertNull($parent->getChild('child real name')); |
| 81 | + $this->assertNull($parent->getChild('child real name', true)); |
57 | 82 | $parent->addChild($child); |
58 | | - $this->assertEquals( |
59 | | - $parent->getChild($child->name), |
60 | | - $child, |
61 | | - ); |
62 | | - $parent->removeChild($child->name); |
63 | | - $this->assertNull( |
64 | | - $parent->getChild($child->name), |
65 | | - ); |
| 83 | + $this->assertSame($child, $parent->getChild('child')); |
| 84 | + $this->assertNull($parent->getChild('child', true)); |
| 85 | + $this->assertNull($parent->getChild('child real name')); |
| 86 | + $this->assertSame($child, $parent->getChild('child real name', true)); |
| 87 | + $child->isNew = true; |
| 88 | + $this->assertNull($parent->getChild('child')); |
| 89 | + $this->assertNull($parent->getChild('child', true)); |
| 90 | + $this->assertNull($parent->getChild('child real name')); |
| 91 | + $this->assertSame($child, $parent->getChild('child real name', true)); |
| 92 | + } |
| 93 | + |
| 94 | + public function testRemoveChildNode(): void |
| 95 | + { |
| 96 | + $parent = new Node('parent'); |
| 97 | + $childOne = new Node('child one'); |
| 98 | + $childTwo = new Node('child two'); |
| 99 | + $childThree = new Node('child three'); |
| 100 | + $parent->addChild($childOne); |
| 101 | + $parent->addChild($childTwo); |
| 102 | + $parent->addChild($childThree); |
| 103 | + $parent->addChild($childTwo); |
| 104 | + $this->assertSame([0 => $childOne, 1 => $childTwo, 2 => $childThree, 3 => $childTwo], $parent->children); |
| 105 | + $parent->removeChild('child two'); |
| 106 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 107 | + $this->assertSame([0 => $childOne, 2 => $childThree, 3 => $childTwo], $parent->children); |
66 | 108 | } |
67 | 109 |
|
68 | 110 | /** |
@@ -132,36 +174,45 @@ public function testNumChildren(): void |
132 | 174 | $this->assertEquals($parent->numChildren(), 3); |
133 | 175 | } |
134 | 176 |
|
135 | | - /** |
136 | | - * SetUp for parents |
137 | | - */ |
138 | 177 | public function testParents(): void |
139 | 178 | { |
140 | | - $parent = new Node('default'); |
141 | | - $this->assertEquals($parent->parents(), []); // exclude self |
142 | | - $this->assertEquals($parent->parents(true), [$parent]); // include self |
143 | | - |
144 | | - $child = new Node('default'); |
145 | | - $parent->addChild($child); |
146 | | - |
147 | | - $this->assertEquals($child->parents(), [$parent]); // exclude self |
148 | | - $this->assertEquals( |
149 | | - $child->parents(true), |
150 | | - [$child, $parent], |
151 | | - ); // include self |
| 179 | + $dbContainer = new Node('root', Node::CONTAINER); |
| 180 | + $dbGroup = new Node('db_group', Node::CONTAINER, true); |
| 181 | + $dbOne = new Node('db_group__one'); |
| 182 | + $dbTwo = new Node('db_group__two'); |
| 183 | + $tableContainer = new Node('tables', Node::CONTAINER); |
| 184 | + $table = new Node('table'); |
| 185 | + $dbContainer->addChild($dbGroup); |
| 186 | + $dbGroup->addChild($dbOne); |
| 187 | + $dbGroup->addChild($dbTwo); |
| 188 | + $dbOne->addChild($tableContainer); |
| 189 | + $tableContainer->addChild($table); |
| 190 | + $this->assertSame([], $dbContainer->parents(false, true)); |
| 191 | + $this->assertSame([$dbContainer], $dbContainer->parents(true, true)); |
| 192 | + $this->assertSame([$dbContainer], $dbGroup->parents(false, true, true)); |
| 193 | + $this->assertSame([$dbGroup, $dbContainer], $dbGroup->parents(true, true, true)); |
| 194 | + $this->assertSame([$dbOne], $table->parents()); |
| 195 | + $this->assertSame([$table, $dbOne], $table->parents(true)); |
| 196 | + $this->assertSame([$tableContainer, $dbOne, $dbContainer], $table->parents(false, true)); |
| 197 | + $this->assertSame([$table, $tableContainer, $dbOne, $dbContainer], $table->parents(true, true)); |
| 198 | + $this->assertSame([$dbOne], $table->parents(false, false, true)); |
| 199 | + $this->assertSame([$tableContainer, $dbOne, $dbGroup, $dbContainer], $table->parents(false, true, true)); |
| 200 | + $this->assertSame( |
| 201 | + [$table, $tableContainer, $dbOne, $dbGroup, $dbContainer], |
| 202 | + $table->parents(true, true, true), |
| 203 | + ); |
152 | 204 | } |
153 | 205 |
|
154 | | - /** |
155 | | - * SetUp for realParent |
156 | | - */ |
157 | 206 | public function testRealParent(): void |
158 | 207 | { |
159 | | - $parent = new Node('default'); |
160 | | - $this->assertFalse($parent->realParent()); |
161 | | - |
162 | | - $child = new Node('default'); |
| 208 | + $parent = new Node('parent'); |
| 209 | + $child = new Node('child'); |
| 210 | + $grandchild = new Node('grandchild'); |
163 | 211 | $parent->addChild($child); |
164 | | - $this->assertEquals($child->realParent(), $parent); |
| 212 | + $child->addChild($grandchild); |
| 213 | + $this->assertFalse($parent->realParent()); |
| 214 | + $this->assertSame($parent, $child->realParent()); |
| 215 | + $this->assertSame($child, $grandchild->realParent()); |
165 | 216 | } |
166 | 217 |
|
167 | 218 | /** |
|
0 commit comments