|
| 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 | +} |
0 commit comments