Skip to content

Commit d8eca8f

Browse files
committed
TASK: Add basic tests for property accessors
1 parent d449dce commit d8eca8f

File tree

5 files changed

+174
-8
lines changed

5 files changed

+174
-8
lines changed

Classes/Domain/NodePropertySpecification.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PackageFactory\NodeTypeObjects\Domain;
66

7+
use Neos\ContentRepository\Domain\Model\NodeType;
78
use Neos\Flow\Annotations as Flow;
89
use Neos\Utility\Unicode\Functions as UnicodeFunctions;
910

@@ -154,4 +155,13 @@ public function $methodName(): $returnType;
154155

155156
return $propertyAccessor;
156157
}
158+
159+
public static function createFromNodeTypeAndPropertyName(NodeType $nodeType, string $propertyName): self
160+
{
161+
return new NodePropertySpecification(
162+
$propertyName,
163+
$nodeType->getPropertyType($propertyName),
164+
$nodeType->getConfiguration('properties.' . $propertyName . '.defaultValue') ?? null
165+
);
166+
}
157167
}

Classes/Domain/NodePropertySpecificationCollection.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public static function createFromNodeType(NodeType $nodeType): NodePropertySpeci
3939
*/
4040
$propertySpecifications = [];
4141
foreach ($nodeType->getProperties() as $propertyName => $propertyConfiguration) {
42-
$propertySpecifications[] = new NodePropertySpecification(
43-
$propertyName,
44-
$nodeType->getPropertyType($propertyName),
45-
$propertyConfiguration['defaultValue'] ?? null
46-
);
42+
$propertySpecifications[] = NodePropertySpecification::createFromNodeTypeAndPropertyName($nodeType, $propertyName);
4743
}
4844
return new NodePropertySpecificationCollection(...$propertySpecifications);
4945
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PackageFactory\NodeTypeObjects\Test;
5+
6+
use Neos\ContentRepository\Domain\Model\NodeType;
7+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecification;
8+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecificationCollection;
9+
use PHPUnit\Framework\TestCase;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\Attributes\Test;
12+
class NodePropertySpecificationCollectionTest extends TestCase
13+
{
14+
#[Test]
15+
public function creationFromNodeTypeWorks(): void
16+
{
17+
$mockNodeType = $this->createMock(NodeType::class);
18+
$mockNodeType->expects(self::once())->method('getProperties')->willReturn(['foo' => [], 'bar' => []]);
19+
$mockNodeType->expects(self::any())->method('getPropertyType')->willReturnCallback(
20+
fn($name) => match ($name) {
21+
'foo' => 'string',
22+
'bar' => 'integer',
23+
default => self::fail('this was unexpected')
24+
}
25+
);
26+
$mockNodeType->expects(self::any())->method('getConfiguration')->willReturnCallback(
27+
fn($name) => match ($name) {
28+
'properties.foo.defaultValue' => 'example',
29+
'properties.bar.defaultValue' => null,
30+
default => self::fail('this was unexpected')
31+
}
32+
);
33+
34+
$this->assertEquals(
35+
new NodePropertySpecificationCollection(
36+
new NodePropertySpecification(
37+
'foo',
38+
'string',
39+
'example'
40+
),
41+
new NodePropertySpecification(
42+
'bar',
43+
'integer',
44+
null
45+
)
46+
),
47+
NodePropertySpecificationCollection::createFromNodeType($mockNodeType)
48+
);
49+
}
50+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PackageFactory\NodeTypeObjects\Test;
5+
6+
use Neos\ContentRepository\Domain\Model\NodeType;
7+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecification;
8+
use PHPUnit\Framework\TestCase;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Attributes\Test;
11+
class NodePropertySpecificationTest extends TestCase
12+
{
13+
public function propertiesMatchExpectationDataProvider(): \Generator
14+
{
15+
yield 'string without default' => [
16+
'title',
17+
'string',
18+
null,
19+
<<<'EOF'
20+
21+
public function getTitle(): ?string;
22+
23+
EOF,
24+
<<<'EOF'
25+
26+
public function getTitle(): ?string
27+
{
28+
$value = $this->node->getProperty('title');
29+
if (is_string($value)) {
30+
return $value;
31+
}
32+
return NULL;
33+
}
34+
35+
EOF
36+
];
37+
38+
yield 'string with default' => [
39+
'title',
40+
'string',
41+
'defaultValue',
42+
<<<'EOF'
43+
44+
public function getTitle(): string;
45+
46+
EOF,
47+
<<<'EOF'
48+
49+
public function getTitle(): string
50+
{
51+
$value = $this->node->getProperty('title');
52+
if (is_string($value)) {
53+
return $value;
54+
}
55+
return 'defaultValue';
56+
}
57+
58+
EOF
59+
];
60+
}
61+
62+
#[Test]
63+
#[DataProvider('propertiesMatchExpectationDataProvider')]
64+
public function propertiesMatchExpectation(string $propertyName, string $propertyType, mixed $defaultValue, string $expectedSignature, string $expectedMethod): void
65+
{
66+
$property = new NodePropertySpecification(
67+
$propertyName,
68+
$propertyType,
69+
$defaultValue
70+
);
71+
72+
$this->assertEquals($expectedSignature, $property->toPhpInterfaceMethodString());
73+
$this->assertEquals($expectedMethod, $property->toPhpClassMethodString());
74+
}
75+
76+
public function creationFromNodeTypeWorksDataProvider (): \Generator
77+
{
78+
yield 'string without default' => [
79+
'title',
80+
'string',
81+
null
82+
];
83+
yield 'string with default' => [
84+
'title',
85+
'string',
86+
'defaultValue'
87+
];
88+
}
89+
90+
#[Test]
91+
#[DataProvider('creationFromNodeTypeWorksDataProvider')]
92+
public function creationFromNodeTypeWorks($propertyName, $propertyType, $defaultValue): void
93+
{
94+
$mockNodeType = $this->createMock(NodeType::class);
95+
$mockNodeType->expects(self::once())->method('getPropertyType')->with($propertyName)->willReturn($propertyType);
96+
$mockNodeType->expects(self::once())->method('getConfiguration')->with('properties.' . $propertyName . '.defaultValue')->willReturn($defaultValue);
97+
98+
$this->assertEquals(
99+
new NodePropertySpecification(
100+
$propertyName,
101+
$propertyType,
102+
$defaultValue,
103+
),
104+
NodePropertySpecification::createFromNodeTypeAndPropertyName($mockNodeType, $propertyName)
105+
);
106+
}
107+
}

Tests/Unit/NodeTypeObjectNameSpecificationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
namespace PackageFactory\NodeTypeObjects\Test;
55

66
use Neos\ContentRepository\Domain\Model\NodeType;
7-
use Neos\Flow\Package\FlowPackageInterface;
87
use PackageFactory\NodeTypeObjects\Domain\NodeTypeObjectNameSpecification;
98
use PHPUnit\Framework\TestCase;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Attributes\Test;
1011

1112
class NodeTypeObjectNameSpecificationTest extends TestCase
1213
{
1314

14-
public function testDetectionOfNamesFromNodeType(): void
15+
#[Test]
16+
public function detectionOfNamesFromNodeType(): void
1517
{
1618
$nodeType = $this->createMock(NodeType::class);
1719
$nodeType->expects(self::any())->method('getName')->willReturn('Vendor.Example:Foo.Bar');
@@ -33,7 +35,8 @@ public function testDetectionOfNamesFromNodeType(): void
3335
);
3436
}
3537

36-
public function testNoClassesForAbstractNodeType(): void
38+
#[Test]
39+
public function noClassesButInterfaceForAbstractNodeType(): void
3740
{
3841
$nodeType = $this->createMock(NodeType::class);
3942
$nodeType->expects(self::any())->method('getName')->willReturn('Vendor.Example:Foo.Bar');

0 commit comments

Comments
 (0)