Skip to content

Commit

Permalink
TASK: Add NodeType unit tests for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Feb 15, 2024
1 parent ee7cea6 commit 95a447c
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Neos.ContentRepository.Core/Classes/NodeType/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function getPropertyType(string $propertyName): string
if (!$this->hasProperty($propertyName)) {
throw new \InvalidArgumentException(
sprintf('NodeType schema has no property "%s" configured for the NodeType "%s". Cannot read its type.', $propertyName, $this->name->value),
1695062252040
1708025421
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function allInheritedNodeTypePropertiesCannotBeUnset(): void
/**
* @test
*/
public function anInheritedNodeTypePropertyCannotBeSetToEmptyArray(): void
public function anInheritedNodeTypePropertyCanBeOverruledWithEmptyArray(): void
{
$nodeTypesFixture = [
'Neos.ContentRepository.Testing:Base' => [
Expand Down
179 changes: 177 additions & 2 deletions Neos.ContentRepository.Core/Tests/Unit/NodeType/NodeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ public function setDeclaredSuperTypesExpectsAnArrayOfNodeTypesAsKeys()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1291300950);
new NodeType(NodeTypeName::fromString('ContentRepository:Folder'), ['foo' => true], [], new DefaultNodeLabelGeneratorFactory()
);
new NodeType(NodeTypeName::fromString('ContentRepository:Folder'), ['foo' => true], [], new DefaultNodeLabelGeneratorFactory());
}

/**
Expand Down Expand Up @@ -412,6 +411,171 @@ public function superTypesRemovedByInheritanceCanBeAddedAgain()
self::assertSame($expectedProperties, $nodeType->getProperties());
}

/**
* @test
*/
public function propertyDeclaration()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'properties' => [
'someProperty' => [
'type' => 'bool',
'defaultValue' => false
]
]
], new DefaultNodeLabelGeneratorFactory());
self::assertTrue($nodeType->hasProperty('someProperty'));
self::assertFalse($nodeType->hasReference('someProperty'));
self::assertSame('bool', $nodeType->getPropertyType('someProperty'));
self::assertEmpty($nodeType->getReferences());
self::assertNull($nodeType->getConfiguration('references.someProperty'));
self::assertNotNull($nodeType->getConfiguration('properties.someProperty'));
self::assertSame(['someProperty' => false], $nodeType->getDefaultValuesForProperties());
self::assertSame(
[
'someProperty' => [
'type' => 'bool',
'defaultValue' => false
]
],
$nodeType->getProperties()
);
}

/**
* @test
*/
public function getPropertyTypeThrowsOnInvalidProperty()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1708025421);
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [], new DefaultNodeLabelGeneratorFactory());
$nodeType->getPropertyType('nonExistent');
self::assertSame('string', $nodeType->getPropertyType('nonExistent'));
}

/**
* @test
*/
public function getPropertyTypeFallback()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'properties' => [
'someProperty' => []
]
], new DefaultNodeLabelGeneratorFactory());
self::assertSame('string', $nodeType->getPropertyType('someProperty'));
}

/**
* @test
*/
public function getDefaultValuesForPropertiesIgnoresNullAndUnset()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'properties' => [
'someProperty' => [
'type' => 'string',
'defaultValue' => 'lol'
],
'otherProperty' => [
'type' => 'string',
'defaultValue' => null
],
'thirdProperty' => [
'type' => 'string'
]
]
], new DefaultNodeLabelGeneratorFactory());
self::assertSame(['someProperty' => 'lol'], $nodeType->getDefaultValuesForProperties());
}

/**
* @test
*/
public function referencesDeclaration()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'references' => [
'someReferences' => []
]
], new DefaultNodeLabelGeneratorFactory());
self::assertFalse($nodeType->hasProperty('someReferences'));
self::assertTrue($nodeType->hasReference('someReferences'));
self::assertThrows(fn() => $nodeType->getPropertyType('someReferences'), \InvalidArgumentException::class);
self::assertEmpty($nodeType->getProperties());
self::assertEmpty($nodeType->getDefaultValuesForProperties());
self::assertNull($nodeType->getConfiguration('properties.someReferences'));
self::assertNotNull($nodeType->getConfiguration('references.someReferences'));
self::assertSame(
[
'someReferences' => []
],
$nodeType->getReferences()
);
}

/**
* @test
*/
public function legacyPropertyReferenceDeclaration()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'properties' => [
'referenceProperty' => [
'type' => 'reference',
]
]
], new DefaultNodeLabelGeneratorFactory());
// will be available as _real_ reference
self::assertFalse($nodeType->hasProperty('referenceProperty'));
self::assertTrue($nodeType->hasReference('referenceProperty'));
self::assertThrows(fn() => $nodeType->getPropertyType('referenceProperty'), \InvalidArgumentException::class);
self::assertEmpty($nodeType->getProperties());
self::assertEmpty($nodeType->getDefaultValuesForProperties());
self::assertNull($nodeType->getConfiguration('properties.referenceProperty'));
self::assertNotNull($nodeType->getConfiguration('references.referenceProperty'));
self::assertSame(
[
'referenceProperty' => [
'constraints' => [
'maxItems' => 1
],
'__legacyPropertyType' => 'reference'
]
],
$nodeType->getReferences()
);
}

/**
* @test
*/
public function legacyPropertyReferencesDeclaration()
{
$nodeType = new NodeType(NodeTypeName::fromString('ContentRepository:Node'), [], [
'properties' => [
'referencesProperty' => [
'type' => 'references',
]
]
], new DefaultNodeLabelGeneratorFactory());
// will be available as _real_ reference
self::assertFalse($nodeType->hasProperty('referencesProperty'));
self::assertTrue($nodeType->hasReference('referencesProperty'));
self::assertThrows(fn() => $nodeType->getPropertyType('referencesProperty'), \InvalidArgumentException::class);
self::assertEmpty($nodeType->getProperties());
self::assertEmpty($nodeType->getDefaultValuesForProperties());
self::assertNull($nodeType->getConfiguration('properties.referencesProperty'));
self::assertNotNull($nodeType->getConfiguration('references.referencesProperty'));
self::assertSame(
[
'referencesProperty' => []
],
$nodeType->getReferences()
);
}

/**
* Return a nodetype built from the nodeTypesFixture
*/
Expand All @@ -437,4 +601,15 @@ protected function getNodeType(string $nodeTypeName): ?NodeType
new DefaultNodeLabelGeneratorFactory()
);
}

private static function assertThrows(callable $fn, string $exceptionClassName): void
{
try {
$fn();
} catch (\Throwable $e) {
self::assertInstanceOf($exceptionClassName, $e);
return;
}
self::fail('$fn should throw.');
}
}

0 comments on commit 95a447c

Please sign in to comment.