Skip to content

Commit

Permalink
TASK: Introduce ContentStreamAwareNodeBuilder to simplify creating …
Browse files Browse the repository at this point in the history
…`ContentStreamAwareNode`
  • Loading branch information
mhsdesign committed Jun 16, 2024
1 parent 313656c commit 0d3bc77
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\FakeClock;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\FakeUserIdProvider;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\NodeWithContentStreamId;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\ContentStreamAwareNode;
use PHPUnit\Framework\Assert;

/**
Expand All @@ -55,7 +55,7 @@ trait CRTestSuiteRuntimeVariables
* The Node's content stream id doesn't necessarily have to match with {@see $currentContentStreamId} if the node was initialized via the content graph from another workspace
* thus we have to track it as well.
*/
protected ?NodeWithContentStreamId $currentNode = null;
protected ?ContentStreamAwareNode $currentNode = null;

protected ?NodeAggregate $currentNodeAggregate = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function theCommandCopyNodesRecursivelyIsExecutedCopyingTheCurrentNodeAgg
$command = CopyNodesRecursively::createFromSubgraphAndStartNode(
$subgraph,
$workspaceName,
$this->currentNode->instance,
$this->currentNode->nodeInstance,
$targetDimensionSpacePoint,
NodeAggregateId::fromString($commandArguments['targetParentNodeAggregateId']),
$targetSucceedingSiblingNodeAggregateId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;

final readonly class NodeWithContentStreamId
final readonly class ContentStreamAwareNode
{
private function __construct(
public NodeAggregateId $aggregateId,
public Node $instance,
public ContentStreamId $contentStreamId,
public Node $nodeInstance,
/** Alias for $currentNode->instance->aggregateId */
public NodeAggregateId $aggregateId,
) {
}

public static function create(Node $node, ContentStreamId $contentStreamId): self
public static function create(ContentStreamId $contentStreamId, Node $node): self
{
return new self($node->aggregateId, $node, $contentStreamId);
return new self($contentStreamId, $node, $node->aggregateId);
}

public function withNode(Node $node): self
public function builder(): ContentStreamAwareNodeBuilder
{
return new self($node->aggregateId, $node, $this->contentStreamId);
return ContentStreamAwareNodeBuilder::create($this->contentStreamId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers;

use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;

final readonly class ContentStreamAwareNodeBuilder
{
private function __construct(
private ContentStreamId $contentStreamId
) {
}

public static function create(ContentStreamId $contentStreamId): self
{
return new self($contentStreamId);
}

public function buildNode(Node $node): ContentStreamAwareNode
{
return ContentStreamAwareNode::create($this->contentStreamId, $node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ private function __construct(
) {
}

public static function create(ContentStreamId $contentStreamId, OriginDimensionSpacePoint $originDimensionSpacePoint, NodeAggregateId $aggregateId): self
{
return new self(
$contentStreamId,
$aggregateId,
$originDimensionSpacePoint
);
}

public static function fromShorthand(string $shorthand): self
{
list($contentStreamId, $nodeAggregateId, $originDimensionSpacePoint) = explode(';', $shorthand);
Expand All @@ -57,12 +48,12 @@ public static function fromShorthand(string $shorthand): self
);
}

public static function fromNode(NodeWithContentStreamId $decorated): self
public static function fromNode(ContentStreamAwareNode $contentStreamAwareNode): self
{
return new self(
$decorated->contentStreamId,
$decorated->instance->aggregateId,
$decorated->instance->originDimensionSpacePoint
$contentStreamAwareNode->contentStreamId,
$contentStreamAwareNode->nodeInstance->aggregateId,
$contentStreamAwareNode->nodeInstance->originDimensionSpacePoint
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
use Neos\ContentRepository\Core\Tests\Behavior\Fixtures\PostalAddress;
use Neos\ContentRepository\Core\Tests\Behavior\Fixtures\PriceSpecification;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\NodeDiscriminator;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\NodeWithContentStreamId;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\ContentStreamAwareNode;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\Helpers\ContentStreamAwareNodeBuilder;
use PHPUnit\Framework\Assert;

/**
Expand Down Expand Up @@ -99,7 +100,7 @@ public function iExpectANodeIdentifiedByXToExistInTheContentGraph(string $serial
. '" was not found in content stream "' . $nodeDiscriminator->contentStreamId->value . '"'
);
$node = $currentNodeAggregate->getNodeByOccupiedDimensionSpacePoint($nodeDiscriminator->originDimensionSpacePoint);
$this->currentNode = NodeWithContentStreamId::create($node, $nodeDiscriminator->contentStreamId);
$this->currentNode = ContentStreamAwareNode::create($nodeDiscriminator->contentStreamId, $node);
}

/**
Expand All @@ -111,10 +112,10 @@ public function iExpectNodeAggregateIdToLeadToNode(
): void {
$nodeAggregateId = NodeAggregateId::fromString($serializedNodeAggregateId);
$expectedDiscriminator = NodeDiscriminator::fromShorthand($serializedNodeDiscriminator);
$this->initializeCurrentNodeFromContentSubgraph(function (ContentSubgraphInterface $subgraph, ContentStreamId $currentContentStreamId) use ($nodeAggregateId, $expectedDiscriminator) {
$this->initializeCurrentNodeFromContentSubgraph(function (ContentSubgraphInterface $subgraph, ContentStreamAwareNodeBuilder $contentStreamAwareNodeBuilder) use ($nodeAggregateId, $expectedDiscriminator) {
$currentNode = $subgraph->findNodeById($nodeAggregateId);
Assert::assertNotNull($currentNode, 'No node could be found by node aggregate id "' . $nodeAggregateId->value . '" in content subgraph "' . $this->currentDimensionSpacePoint->toJson() . '@' . $this->currentWorkspaceName->value . '"');
$actualDiscriminator = NodeDiscriminator::create($currentContentStreamId, $currentNode->originDimensionSpacePoint, $currentNode->aggregateId);
$actualDiscriminator = NodeDiscriminator::fromNode($contentStreamAwareNodeBuilder->buildNode($currentNode));
Assert::assertTrue($expectedDiscriminator->equals($actualDiscriminator), 'Node discriminators do not match. Expected was ' . json_encode($expectedDiscriminator) . ' , given was ' . json_encode($actualDiscriminator));
return $currentNode;
});
Expand Down Expand Up @@ -148,10 +149,10 @@ public function iExpectPathToLeadToNode(string $serializedNodePath, string $seri
}
$nodePath = NodePath::fromString($serializedNodePath);
$expectedDiscriminator = NodeDiscriminator::fromShorthand($serializedNodeDiscriminator);
$this->initializeCurrentNodeFromContentSubgraph(function (ContentSubgraphInterface $subgraph, ContentStreamId $currentContentStreamId) use ($nodePath, $expectedDiscriminator) {
$this->initializeCurrentNodeFromContentSubgraph(function (ContentSubgraphInterface $subgraph, ContentStreamAwareNodeBuilder $contentStreamAwareNodeBuilder) use ($nodePath, $expectedDiscriminator) {
$currentNode = $subgraph->findNodeByPath($nodePath, $this->getRootNodeAggregateId());
Assert::assertNotNull($currentNode, 'No node could be found by node path "' . $nodePath->serializeToString() . '" in content subgraph "' . $this->currentDimensionSpacePoint->toJson() . '@' . $this->currentWorkspaceName->value . '"');
$actualDiscriminator = NodeDiscriminator::create($currentContentStreamId, $currentNode->originDimensionSpacePoint, $currentNode->aggregateId);
$actualDiscriminator = NodeDiscriminator::fromNode($contentStreamAwareNodeBuilder->buildNode($currentNode));
Assert::assertTrue($expectedDiscriminator->equals($actualDiscriminator), 'Node discriminators do not match. Expected was ' . json_encode($expectedDiscriminator) . ' , given was ' . json_encode($actualDiscriminator));
return $currentNode;
});
Expand Down Expand Up @@ -279,15 +280,14 @@ public function iExpectThisNodeToExactlyInheritTheTags(string $expectedTagList):
}

/**
* @param callable(ContentSubgraphInterface $subgraph, ContentStreamId $currentContentStreamId): Node $query
* @param callable(ContentSubgraphInterface $subgraph, ContentStreamAwareNodeBuilder $contentStreamAwareNodeBuilder): Node $query
*/
protected function initializeCurrentNodeFromContentSubgraph(callable $query): void
{
$contentGraph = $this->getCurrentContentGraph();
$node = $query($this->getCurrentSubgraph($contentGraph), $contentGraph->getContentStreamId());
$this->currentNode = NodeWithContentStreamId::create(
$node, $contentGraph->getContentStreamId()
);
$contentStreamAwareNodeBuilder = ContentStreamAwareNodeBuilder::create($contentGraph->getContentStreamId());
$node = $query($this->getCurrentSubgraph($contentGraph), $contentStreamAwareNodeBuilder);
$this->currentNode = $contentStreamAwareNodeBuilder->buildNode($node);
}

/**
Expand Down Expand Up @@ -467,15 +467,15 @@ public function iExpectThisNodeToHaveNoReferences(): void
public function iExpectThisNodeToBeReferencedBy(TableNode $expectedReferences): void
{
$contentGraph = $this->getCurrentContentGraph();
$this->assertOnCurrentNodeWithContentStreamId(function (NodeWithContentStreamId $currentNode) use ($expectedReferences, $contentGraph) {
$this->assertOnCurrentContentStreamAwareNode(function (ContentStreamAwareNode $currentNode) use ($expectedReferences, $contentGraph) {
$actualReferences = $this->getCurrentSubgraph($contentGraph)
->findBackReferences($currentNode->aggregateId, FindBackReferencesFilter::create());

$this->assertReferencesMatch($expectedReferences, $actualReferences, $currentNode->contentStreamId);
});
}

private function assertReferencesMatch(TableNode $expectedReferencesTable, References $actualReferences, ContentStreamId $actualContentStreamId): void
private function assertReferencesMatch(TableNode $expectedReferencesTable, References $actualReferences, ContentStreamAwareNodeBuilder $contentStreamAwareNodeBuilder): void
{
$expectedReferences = $expectedReferencesTable->getHash();
Assert::assertSame(
Expand All @@ -491,7 +491,7 @@ private function assertReferencesMatch(TableNode $expectedReferencesTable, Refer
$actualReferences[$index]->name->value
);
$expectedReferenceDiscriminator = NodeDiscriminator::fromShorthand($row['Node']);
$actualReferenceDiscriminator = NodeDiscriminator::create($actualContentStreamId, $actualReferences[$index]->node->originDimensionSpacePoint, $actualReferences[$index]->node->aggregateId);
$actualReferenceDiscriminator = NodeDiscriminator::fromNode($contentStreamAwareNodeBuilder->buildNode($actualReferences[$index]->node));
Assert::assertTrue(
$expectedReferenceDiscriminator->equals($actualReferenceDiscriminator),
'Reference discriminator does not match.'
Expand Down Expand Up @@ -560,17 +560,17 @@ public function iExpectThisNodeToNotBeReferenced(): void
public function iExpectThisNodeToBeTheChildOfNode(string $serializedParentNodeDiscriminator): void
{
$expectedParentDiscriminator = NodeDiscriminator::fromShorthand($serializedParentNodeDiscriminator);
$this->assertOnCurrentNodeWithContentStreamId(function (NodeWithContentStreamId $currentNode) use ($expectedParentDiscriminator) {
$this->assertOnCurrentContentStreamAwareNode(function (ContentStreamAwareNode $currentNode) use ($expectedParentDiscriminator) {
$subgraph = $this->getCurrentSubgraph();

$parent = $subgraph->findParentNode($currentNode->aggregateId);
Assert::assertInstanceOf(Node::class, $parent, 'Parent not found.');
$actualParentDiscriminator = NodeDiscriminator::fromNode($currentNode->withNode($parent));
$actualParentDiscriminator = NodeDiscriminator::fromNode($currentNode->builder()->buildNode($parent));
Assert::assertTrue($expectedParentDiscriminator->equals($actualParentDiscriminator), 'Parent discriminator does not match. Expected was ' . json_encode($expectedParentDiscriminator) . ', given was ' . json_encode($actualParentDiscriminator));

$expectedChildDiscriminator = NodeDiscriminator::fromNode($currentNode);
$child = $subgraph->findNodeByPath($currentNode->instance->name, $parent->aggregateId);
$actualChildDiscriminator = NodeDiscriminator::fromNode($currentNode->withNode($child));
$child = $subgraph->findNodeByPath($currentNode->nodeInstance->name, $parent->aggregateId);
$actualChildDiscriminator = NodeDiscriminator::fromNode($currentNode->builder()->buildNode($child));
Assert::assertTrue($expectedChildDiscriminator->equals($actualChildDiscriminator), 'Child discriminator does not match. Expected was ' . json_encode($expectedChildDiscriminator) . ', given was ' . json_encode($actualChildDiscriminator));
});
}
Expand All @@ -593,7 +593,7 @@ public function iExpectThisNodeToHaveNoParentNode(): void
*/
public function iExpectThisNodeToHaveTheFollowingChildNodes(TableNode $expectedChildNodesTable): void
{
$this->assertOnCurrentNodeWithContentStreamId(function (NodeWithContentStreamId $currentNode) use ($expectedChildNodesTable) {
$this->assertOnCurrentContentStreamAwareNode(function (ContentStreamAwareNode $currentNode) use ($expectedChildNodesTable) {
$subgraph = $this->getCurrentSubgraph();
$actualChildNodes = [];
foreach ($subgraph->findChildNodes($currentNode->aggregateId, FindChildNodesFilter::create()) as $actualChildNode) {
Expand All @@ -608,7 +608,7 @@ public function iExpectThisNodeToHaveTheFollowingChildNodes(TableNode $expectedC
Assert::assertTrue($expectedNodeName->equals($actualNodeName), 'ContentSubgraph::findChildNodes: Node name in index ' . $index . ' does not match. Expected: "' . $expectedNodeName->value . '" Actual: "' . $actualNodeName->value . '"');
if (isset($row['NodeDiscriminator'])) {
$expectedNodeDiscriminator = NodeDiscriminator::fromShorthand($row['NodeDiscriminator']);
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->withNode($actualChildNodes[$index]));
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->builder()->buildNode($actualChildNodes[$index]));
Assert::assertTrue($expectedNodeDiscriminator->equals($actualNodeDiscriminator), 'ContentSubgraph::findChildNodes: Node discriminator in index ' . $index . ' does not match. Expected: ' . json_encode($expectedNodeDiscriminator->jsonSerialize()) . ' Actual: ' . json_encode($actualNodeDiscriminator));
}
}
Expand All @@ -634,7 +634,7 @@ public function iExpectThisNodeToHaveNoChildNodes(): void
*/
public function iExpectThisNodeToHaveTheFollowingPrecedingSiblings(TableNode $expectedPrecedingSiblingsTable): void
{
$this->assertOnCurrentNodeWithContentStreamId(function (NodeWithContentStreamId $currentNode) use ($expectedPrecedingSiblingsTable) {
$this->assertOnCurrentContentStreamAwareNode(function (ContentStreamAwareNode $currentNode) use ($expectedPrecedingSiblingsTable) {
$actualSiblings = [];
foreach (
$this->getCurrentSubgraph()->findPrecedingSiblingNodes(
Expand All @@ -647,7 +647,7 @@ public function iExpectThisNodeToHaveTheFollowingPrecedingSiblings(TableNode $ex
Assert::assertCount(count($expectedPrecedingSiblingsTable->getHash()), $actualSiblings, 'ContentSubgraph::findPrecedingSiblingNodes: Sibling count does not match');
foreach ($expectedPrecedingSiblingsTable->getHash() as $index => $row) {
$expectedNodeDiscriminator = NodeDiscriminator::fromShorthand($row['NodeDiscriminator']);
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->withNode($actualSiblings[$index]));
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->builder()->buildNode($actualSiblings[$index]));
Assert::assertTrue($expectedNodeDiscriminator->equals($actualNodeDiscriminator), 'ContentSubgraph::findPrecedingSiblingNodes: Node discriminator in index ' . $index . ' does not match. Expected: ' . json_encode($expectedNodeDiscriminator) . ' Actual: ' . json_encode($actualNodeDiscriminator));
}
});
Expand All @@ -671,7 +671,7 @@ public function iExpectThisNodeToHaveNoPrecedingSiblings(): void
*/
public function iExpectThisNodeToHaveTheFollowingSucceedingSiblings(TableNode $expectedSucceedingSiblingsTable): void
{
$this->assertOnCurrentNodeWithContentStreamId(function (NodeWithContentStreamId $currentNode) use ($expectedSucceedingSiblingsTable) {
$this->assertOnCurrentContentStreamAwareNode(function (ContentStreamAwareNode $currentNode) use ($expectedSucceedingSiblingsTable) {
$actualSiblings = [];
foreach (
$this->getCurrentSubgraph()->findSucceedingSiblingNodes(
Expand All @@ -684,7 +684,7 @@ public function iExpectThisNodeToHaveTheFollowingSucceedingSiblings(TableNode $e
Assert::assertCount(count($expectedSucceedingSiblingsTable->getHash()), $actualSiblings, 'ContentSubgraph::findSucceedingSiblingNodes: Sibling count does not match');
foreach ($expectedSucceedingSiblingsTable->getHash() as $index => $row) {
$expectedNodeDiscriminator = NodeDiscriminator::fromShorthand($row['NodeDiscriminator']);
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->withNode($actualSiblings[$index]));
$actualNodeDiscriminator = NodeDiscriminator::fromNode($currentNode->builder()->buildNode($actualSiblings[$index]));
Assert::assertTrue($expectedNodeDiscriminator->equals($actualNodeDiscriminator), 'ContentSubgraph::findSucceedingSiblingNodes: Node discriminator in index ' . $index . ' does not match. Expected: ' . json_encode($expectedNodeDiscriminator) . ' Actual: ' . json_encode($actualNodeDiscriminator));
}
});
Expand All @@ -702,7 +702,7 @@ public function iExpectThisNodeToHaveNoSucceedingSiblings(): void
});
}

protected function assertOnCurrentNodeWithContentStreamId(callable $assertions): void
protected function assertOnCurrentContentStreamAwareNode(callable $assertions): void
{
$this->expectCurrentNode();
$assertions($this->currentNode);
Expand All @@ -711,6 +711,6 @@ protected function assertOnCurrentNodeWithContentStreamId(callable $assertions):
protected function assertOnCurrentNode(callable $assertions): void
{
$this->expectCurrentNode();
$assertions($this->currentNode->instance);
$assertions($this->currentNode->nodeInstance);
}
}

0 comments on commit 0d3bc77

Please sign in to comment.