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