Skip to content

Commit 301b058

Browse files
committed
TASK: Simplify TransientNodeTest
1 parent fd492ed commit 301b058

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

Tests/Unit/Domain/NodeCreation/TransientNodeTest.php

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,27 @@ class TransientNodeTest extends TestCase
6464
type: references
6565
YAML;
6666

67-
/** @var array<string, array<mixed>> */
68-
private array $nodeTypesFixture;
69-
70-
/** @var array<string, NodeType> */
71-
private array $nodeTypes;
67+
private NodeTypeManager $nodeTypeManager;
7268

7369
public function setUp(): void
7470
{
7571
parent::setUp();
76-
$this->nodeTypesFixture = Yaml::parse(self::NODE_TYPE_FIXTURES);
72+
$this->nodeTypeManager = new NodeTypeManager();
73+
$this->nodeTypeManager->overrideNodeTypes(Yaml::parse(self::NODE_TYPE_FIXTURES));
7774
}
7875

7976
/** @test */
8077
public function fromRegularAllowedChildNode(): void
8178
{
82-
$parentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:Content1'));
79+
$parentNode = $this->createFakeRegularTransientNode('A:Content1');
8380
self::assertSame($this->getNodeType('A:Content1'), $parentNode->getNodeType());
8481
$parentNode->requireConstraintsImposedByAncestorsToBeMet($this->getNodeType('A:Content2'));
8582
}
8683

8784
/** @test */
8885
public function forTetheredChildNodeAllowedChildNode(): void
8986
{
90-
$grandParentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:WithContent1AllowedCollectionAsChildNode'));
87+
$grandParentNode = $this->createFakeRegularTransientNode('A:WithContent1AllowedCollectionAsChildNode');
9188

9289
$parentNode = $grandParentNode->forTetheredChildNode(NodeName::fromString('collection'), []);
9390
self::assertSame($this->getNodeType('A:Collection.Allowed'), $parentNode->getNodeType());
@@ -98,7 +95,7 @@ public function forTetheredChildNodeAllowedChildNode(): void
9895
/** @test */
9996
public function forTetheredChildNodeAllowedChildNodeBecauseConstraintOverride(): void
10097
{
101-
$grandParentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:WithContent1AllowedCollectionAsChildNodeViaOverride'));
98+
$grandParentNode = $this->createFakeRegularTransientNode('A:WithContent1AllowedCollectionAsChildNodeViaOverride');
10299

103100
$parentNode = $grandParentNode->forTetheredChildNode(NodeName::fromString('collection'), []);
104101
self::assertSame($this->getNodeType('A:Collection.Disallowed'), $parentNode->getNodeType());
@@ -109,7 +106,7 @@ public function forTetheredChildNodeAllowedChildNodeBecauseConstraintOverride():
109106
/** @test */
110107
public function forRegularChildNodeAllowedChildNode(): void
111108
{
112-
$grandParentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:Content1'));
109+
$grandParentNode = $this->createFakeRegularTransientNode('A:Content1');
113110

114111
$parentNode = $grandParentNode->forRegularChildNode($this->getNodeType('A:Content2'), []);
115112
self::assertSame($this->getNodeType('A:Content2'), $parentNode->getNodeType());
@@ -123,7 +120,7 @@ public function fromRegularDisallowedChildNode(): void
123120
$this->expectException(NodeConstraintException::class);
124121
$this->expectExceptionMessage('Node type "A:Content1" is not allowed for child nodes of type A:Collection.Disallowed');
125122

126-
$parentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:Collection.Disallowed'));
123+
$parentNode = $this->createFakeRegularTransientNode('A:Collection.Disallowed');
127124
self::assertSame($this->getNodeType('A:Collection.Disallowed'), $parentNode->getNodeType());
128125

129126
$parentNode->requireConstraintsImposedByAncestorsToBeMet($this->getNodeType('A:Content1'));
@@ -135,7 +132,7 @@ public function forTetheredChildNodeDisallowedChildNode(): void
135132
$this->expectException(NodeConstraintException::class);
136133
$this->expectExceptionMessage('Node type "A:Content1" is not allowed below tethered child nodes "collection" of nodes of type "A:WithDisallowedCollectionAsChildNode"');
137134

138-
$grandParentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:WithDisallowedCollectionAsChildNode'));
135+
$grandParentNode = $this->createFakeRegularTransientNode('A:WithDisallowedCollectionAsChildNode');
139136

140137
$parentNode = $grandParentNode->forTetheredChildNode(NodeName::fromString('collection'), []);
141138
self::assertSame($this->getNodeType('A:Collection.Disallowed'), $parentNode->getNodeType());
@@ -149,7 +146,7 @@ public function forRegularChildNodeDisallowedChildNode(): void
149146
$this->expectException(NodeConstraintException::class);
150147
$this->expectExceptionMessage('Node type "A:Content1" is not allowed for child nodes of type A:Collection.Disallowed');
151148

152-
$grandParentNode = $this->createFakeRegularTransientNode($this->getNodeType('A:Content2'));
149+
$grandParentNode = $this->createFakeRegularTransientNode('A:Content2');
153150

154151
$parentNode = $grandParentNode->forRegularChildNode($this->getNodeType('A:Collection.Disallowed'), []);
155152
self::assertSame($this->getNodeType('A:Collection.Disallowed'), $parentNode->getNodeType());
@@ -162,7 +159,7 @@ public function splitPropertiesAndReferencesByTypeDeclaration(): void
162159
{
163160
$node = TransientNode::forRegular(
164161
$this->getNodeType('A:ContentWithProperties'),
165-
$this->getMockBuilder(NodeTypeManager::class)->disableOriginalConstructor()->getMock(),
162+
$this->nodeTypeManager,
166163
$this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(),
167164
[
168165
'property-string' => '',
@@ -191,11 +188,13 @@ public function splitPropertiesAndReferencesByTypeDeclaration(): void
191188
);
192189
}
193190

194-
private function createFakeRegularTransientNode(NodeType $nodeType): TransientNode
191+
private function createFakeRegularTransientNode(string $nodeTypeName): TransientNode
195192
{
193+
$nodeType = $this->getNodeType($nodeTypeName);
194+
196195
return TransientNode::forRegular(
197196
$nodeType,
198-
$this->getMockBuilder(NodeTypeManager::class)->disableOriginalConstructor()->getMock(),
197+
$this->nodeTypeManager,
199198
$this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(),
200199
[]
201200
);
@@ -206,34 +205,9 @@ private function createFakeRegularTransientNode(NodeType $nodeType): TransientNo
206205
*/
207206
private function getNodeType(string $nodeTypeName): ?NodeType
208207
{
209-
if (!isset($this->nodeTypesFixture[$nodeTypeName])) {
210-
return null;
211-
}
212-
213-
if (isset($this->nodeTypes[$nodeTypeName])) {
214-
return $this->nodeTypes[$nodeTypeName];
215-
}
216-
217-
$configuration = $this->nodeTypesFixture[$nodeTypeName];
218-
$declaredSuperTypes = [];
219-
if (isset($configuration['superTypes']) && is_array($configuration['superTypes'])) {
220-
foreach ($configuration['superTypes'] as $superTypeName => $enabled) {
221-
$declaredSuperTypes[$superTypeName] = $enabled === true ? $this->getNodeType($superTypeName) : null;
222-
}
223-
}
224-
225-
$nodeType = new NodeType(
226-
$nodeTypeName,
227-
$declaredSuperTypes,
228-
$configuration
229-
);
230-
231-
$fakeNodeTypeManager = $this->getMockBuilder(NodeTypeManager::class)->onlyMethods(['getNodeType'])->getMock();
232-
233-
$fakeNodeTypeManager->expects(self::any())->method('getNodeType')->willReturnCallback(fn ($nodeType) => $this->getNodeType($nodeType));
234-
235-
ObjectAccess::setProperty($nodeType, 'nodeTypeManager', $fakeNodeTypeManager, true);
236-
237-
return $this->nodeTypes[$nodeTypeName] = $nodeType;
208+
$nodeType = $this->nodeTypeManager->getNodeType($nodeTypeName);
209+
// no di here
210+
ObjectAccess::setProperty($nodeType, 'nodeTypeManager', $this->nodeTypeManager, true);
211+
return $nodeType;
238212
}
239213
}

0 commit comments

Comments
 (0)