Skip to content

Commit 81ba326

Browse files
committed
Merge branch 'main' into 1.0
* main: TASK: Add basic tests for property accessors # Conflicts: # Tests/Unit/NodeTypeObjectNameSpecificationTest.php
2 parents 533e265 + d8eca8f commit 81ba326

File tree

8 files changed

+193
-16
lines changed

8 files changed

+193
-16
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ name: build
33
on:
44
push:
55
branches:
6-
- 'main'
6+
- 'neos-8'
77
- 'neos-9'
8+
- '1.0'
89
pull_request:
910
branches:
10-
- 'main'
11+
- 'neos-8'
1112
- 'neos-9'
13+
- '1.0'
1214

1315
jobs:
1416
test:

Classes/Command/NodetypeObjectsCommandController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public function buildCommand(string $packageKeys, string $crId = 'default'): voi
107107
}
108108
$nameSpecificationsCollection = new NodeTypeObjectNameSpecificationCollection(...$nameSpecifications);
109109

110-
// loop 2 build interfaces and objects
110+
// loop 1 build interfaces
111+
// loop 2 build objects
111112
foreach ($packages as $package) {
112113
foreach ($nodeTypes as $nodeType) {
113114
if (!str_starts_with($nodeType->name->value, $package->getPackageKey() . ':')) {

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\Core\NodeType\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\Core\NodeType\NodeType;
7+
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
8+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecification;
9+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecificationCollection;
10+
use PHPUnit\Framework\TestCase;
11+
use PHPUnit\Framework\Attributes\Test;
12+
13+
class NodePropertySpecificationCollectionTest extends TestCase
14+
{
15+
#[Test]
16+
public function creationFromNodeTypeWorks(): void
17+
{
18+
$nodeType = new NodeType(
19+
NodeTypeName::fromString('Vendor.Package:Foo.Bar'),
20+
[],
21+
[
22+
'properties' => [
23+
'foo' => [
24+
'type' => 'string',
25+
'defaultValue' => 'example'
26+
],
27+
'bar' => [
28+
'type' => 'integer'
29+
]
30+
]
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($nodeType)
48+
);
49+
}
50+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PackageFactory\NodeTypeObjects\Test;
5+
6+
use Neos\ContentRepository\Core\NodeType\NodeType;
7+
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
8+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecification;
9+
use PHPUnit\Framework\TestCase;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\Attributes\Test;
12+
class NodePropertySpecificationTest extends TestCase
13+
{
14+
public static function propertiesMatchExpectationDataProvider(): \Generator
15+
{
16+
yield 'string without default' => [
17+
'title',
18+
'string',
19+
null,
20+
<<<'EOF'
21+
22+
public function getTitle(): ?string;
23+
24+
EOF,
25+
<<<'EOF'
26+
27+
public function getTitle(): ?string
28+
{
29+
$value = $this->node->getProperty('title');
30+
if (is_string($value)) {
31+
return $value;
32+
}
33+
return NULL;
34+
}
35+
36+
EOF
37+
];
38+
39+
yield 'string with default' => [
40+
'title',
41+
'string',
42+
'defaultValue',
43+
<<<'EOF'
44+
45+
public function getTitle(): string;
46+
47+
EOF,
48+
<<<'EOF'
49+
50+
public function getTitle(): string
51+
{
52+
$value = $this->node->getProperty('title');
53+
if (is_string($value)) {
54+
return $value;
55+
}
56+
return 'defaultValue';
57+
}
58+
59+
EOF
60+
];
61+
}
62+
63+
#[Test]
64+
#[DataProvider('propertiesMatchExpectationDataProvider')]
65+
public function propertiesMatchExpectation(string $propertyName, string $propertyType, mixed $defaultValue, string $expectedSignature, string $expectedMethod): void
66+
{
67+
$property = new NodePropertySpecification(
68+
$propertyName,
69+
$propertyType,
70+
$defaultValue
71+
);
72+
73+
$this->assertEquals($expectedSignature, $property->toPhpInterfaceMethodString());
74+
$this->assertEquals($expectedMethod, $property->toPhpClassMethodString());
75+
}
76+
77+
public static function creationFromNodeTypeWorksDataProvider (): \Generator
78+
{
79+
yield 'string without default' => [
80+
'title',
81+
'string',
82+
null
83+
];
84+
yield 'string with default' => [
85+
'title',
86+
'string',
87+
'defaultValue'
88+
];
89+
}
90+
91+
#[Test]
92+
#[DataProvider('creationFromNodeTypeWorksDataProvider')]
93+
public function creationFromNodeTypeWorks($propertyName, $propertyType, $defaultValue): void
94+
{
95+
$nodeType = new NodeType(
96+
NodeTypeName::fromString('Vendor.Package:Foo.Bar'),
97+
[],
98+
[
99+
'properties' => [
100+
$propertyName => [
101+
'type' => $propertyType,
102+
'defaultValue' => $defaultValue
103+
]
104+
]
105+
]
106+
);
107+
108+
$this->assertEquals(
109+
new NodePropertySpecification(
110+
$propertyName,
111+
$propertyType,
112+
$defaultValue,
113+
),
114+
NodePropertySpecification::createFromNodeTypeAndPropertyName($nodeType, $propertyName)
115+
);
116+
}
117+
}

Tests/Unit/NodeTypeObjectNameSpecificationTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
88
use PackageFactory\NodeTypeObjects\Domain\NodeTypeObjectNameSpecification;
99
use PHPUnit\Framework\TestCase;
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 = new NodeType(
1719
NodeTypeName::fromString('Vendor.Example:Foo.Bar'),
1820
[],
19-
[
20-
'abstract' => false
21-
]
21+
[]
2222
);
2323

2424
$specification = NodeTypeObjectNameSpecification::createFromNodeType(
@@ -38,7 +38,8 @@ public function testDetectionOfNamesFromNodeType(): void
3838
);
3939
}
4040

41-
public function testNoClassesForAbstractNodeType(): void
41+
#[Test]
42+
public function noClassesButInterfaceForAbstractNodeType(): void
4243
{
4344
$nodeType = new NodeType(
4445
NodeTypeName::fromString('Vendor.Example:Foo.Bar'),

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require-dev": {
1616
"phpunit/phpunit": "^10.0",
1717
"phpstan/phpstan": "^1.10",
18-
"squizlabs/php_codesniffer": "^3.7"
18+
"squizlabs/php_codesniffer": "^3.7",
19+
"neos/contentgraph-doctrinedbaladapter": "~9.0 || dev-master"
1920
},
2021
"scripts": {
2122
"fix:code-style": [
@@ -34,9 +35,8 @@
3435
"@lint:code-style",
3536
"@lint:static-analysis"
3637
],
37-
"test:unit": "vendor/bin/phpunit Tests/Unit",
38+
"test:unit": "vendor/bin/phpunit Tests/Unit --display-phpunit-deprecations",
3839
"test": [
39-
"@install",
4040
"@test:unit"
4141
]
4242
},

0 commit comments

Comments
 (0)