|
7 | 7 | use Liip\MetadataParser\Exception\ParseException; |
8 | 8 | use Liip\MetadataParser\Metadata\ParameterMetadata; |
9 | 9 | use Liip\MetadataParser\Metadata\PropertyType; |
| 10 | +use Liip\MetadataParser\Metadata\PropertyTypeClass; |
| 11 | +use Liip\MetadataParser\Metadata\PropertyTypePrimitive; |
10 | 12 | use Liip\MetadataParser\Metadata\PropertyTypeUnknown; |
11 | 13 | use Liip\MetadataParser\ModelParser\RawMetadata\PropertyCollection; |
12 | 14 | use Liip\MetadataParser\ModelParser\RawMetadata\PropertyVariationMetadata; |
13 | 15 | use Liip\MetadataParser\ModelParser\RawMetadata\RawClassMetadata; |
14 | 16 | use Liip\MetadataParser\ModelParser\ReflectionParser; |
15 | 17 | use PHPUnit\Framework\TestCase; |
| 18 | +use Tests\Liip\MetadataParser\ModelParser\Fixtures\TypeDeclarationModel; |
| 19 | +use Tests\Liip\MetadataParser\ModelParser\Fixtures\UnionTypeDeclarationModel; |
16 | 20 | use Tests\Liip\MetadataParser\ModelParser\Model\ReflectionBaseModel; |
17 | 21 |
|
18 | 22 | /** |
@@ -80,6 +84,57 @@ public function testProperties(): void |
80 | 84 | $this->assertPropertyType($property3->getType(), PropertyTypeUnknown::class, 'mixed', true); |
81 | 85 | } |
82 | 86 |
|
| 87 | + public function testTypedProperties(): void |
| 88 | + { |
| 89 | + if (version_compare(PHP_VERSION, '7.4.0', '<')) { |
| 90 | + $this->markTestSkipped('Primitive property types are only supported in PHP 7.4 or newer'); |
| 91 | + } |
| 92 | + |
| 93 | + $rawClassMetadata = new RawClassMetadata(TypeDeclarationModel::class); |
| 94 | + $this->parser->parse($rawClassMetadata); |
| 95 | + |
| 96 | + $props = $rawClassMetadata->getPropertyCollections(); |
| 97 | + $this->assertCount(3, $props, 'Number of class metadata properties should match'); |
| 98 | + |
| 99 | + $this->assertPropertyCollection('property1', 1, $props[0]); |
| 100 | + $property1 = $props[0]->getVariations()[0]; |
| 101 | + $this->assertProperty('property1', false, false, $property1); |
| 102 | + $this->assertPropertyType($property1->getType(), PropertyTypePrimitive::class, 'string', false); |
| 103 | + |
| 104 | + $this->assertPropertyCollection('property2', 1, $props[1]); |
| 105 | + $property2 = $props[1]->getVariations()[0]; |
| 106 | + $this->assertProperty('property2', true, false, $property2); |
| 107 | + $this->assertPropertyType($property2->getType(), PropertyTypePrimitive::class, 'int|null', true); |
| 108 | + |
| 109 | + $this->assertPropertyCollection('property3', 1, $props[2]); |
| 110 | + $property3 = $props[2]->getVariations()[0]; |
| 111 | + $this->assertProperty('property3', false, false, $property3); |
| 112 | + $this->assertPropertyType($property3->getType(), PropertyTypeClass::class, ReflectionParserTest::class, false); |
| 113 | + } |
| 114 | + |
| 115 | + public function testTypedPropertiesUnion(): void |
| 116 | + { |
| 117 | + if (version_compare(PHP_VERSION, '8.0.0', '<')) { |
| 118 | + $this->markTestSkipped('Union property types are only supported in PHP 8.0 or newer'); |
| 119 | + } |
| 120 | + |
| 121 | + $rawClassMetadata = new RawClassMetadata(UnionTypeDeclarationModel::class); |
| 122 | + $this->parser->parse($rawClassMetadata); |
| 123 | + |
| 124 | + $props = $rawClassMetadata->getPropertyCollections(); |
| 125 | + $this->assertCount(2, $props, 'Number of class metadata properties should match'); |
| 126 | + |
| 127 | + $this->assertPropertyCollection('property1', 1, $props[0]); |
| 128 | + $property1 = $props[0]->getVariations()[0]; |
| 129 | + $this->assertProperty('property1', false, false, $property1); |
| 130 | + $this->assertPropertyType($property1->getType(), PropertyTypeUnknown::class, 'mixed', true); |
| 131 | + |
| 132 | + $this->assertPropertyCollection('property2', 1, $props[1]); |
| 133 | + $property2 = $props[1]->getVariations()[0]; |
| 134 | + $this->assertProperty('property2', true, false, $property2); |
| 135 | + $this->assertPropertyType($property2->getType(), PropertyTypeUnknown::class, 'mixed', true); |
| 136 | + } |
| 137 | + |
83 | 138 | public function testPrefilledClassMetadata(): void |
84 | 139 | { |
85 | 140 | $c = new class() { |
|
0 commit comments