|
23 | 23 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
|
24 | 24 | use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
|
25 | 25 | use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
|
| 26 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
26 | 27 | use PHPUnit\Framework\TestCase;
|
27 | 28 | use Symfony\Component\PropertyInfo\Type;
|
28 | 29 |
|
@@ -126,4 +127,46 @@ public function testResourceContextWithReverse()
|
126 | 127 |
|
127 | 128 | $this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass));
|
128 | 129 | }
|
| 130 | + |
| 131 | + public function testAnonymousResourceContext() |
| 132 | + { |
| 133 | + $dummy = new Dummy(); |
| 134 | + $this->propertyNameCollectionFactoryProphecy->create(Dummy::class)->willReturn(new PropertyNameCollection(['dummyPropertyA'])); |
| 135 | + $this->propertyMetadataFactoryProphecy->create(Dummy::class, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, [])); |
| 136 | + |
| 137 | + $contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal()); |
| 138 | + |
| 139 | + $iri = '_:'.(\function_exists('spl_object_id') ? spl_object_id($dummy) : spl_object_hash($dummy)); |
| 140 | + $expected = [ |
| 141 | + '@context' => [ |
| 142 | + '@vocab' => '#', |
| 143 | + 'hydra' => 'http://www.w3.org/ns/hydra/core#', |
| 144 | + 'dummyPropertyA' => $iri.'/dummyPropertyA', |
| 145 | + ], |
| 146 | + '@id' => $iri, |
| 147 | + ]; |
| 148 | + |
| 149 | + $this->assertEquals($expected, $contextBuilder->getAnonymousResourceContext($dummy)); |
| 150 | + } |
| 151 | + |
| 152 | + public function testAnonymousResourceContextWithIri() |
| 153 | + { |
| 154 | + $dummy = new Dummy(); |
| 155 | + $this->propertyNameCollectionFactoryProphecy->create(Dummy::class)->willReturn(new PropertyNameCollection(['dummyPropertyA'])); |
| 156 | + $this->propertyMetadataFactoryProphecy->create(Dummy::class, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, [])); |
| 157 | + |
| 158 | + $contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal()); |
| 159 | + |
| 160 | + $expected = [ |
| 161 | + '@context' => [ |
| 162 | + '@vocab' => '#', |
| 163 | + 'hydra' => 'http://www.w3.org/ns/hydra/core#', |
| 164 | + 'dummyPropertyA' => '/dummies/dummyPropertyA', |
| 165 | + ], |
| 166 | + '@id' => '/dummies', |
| 167 | + '@type' => 'Dummy', |
| 168 | + ]; |
| 169 | + |
| 170 | + $this->assertEquals($expected, $contextBuilder->getAnonymousResourceContext($dummy, ['iri' => '/dummies', 'name' => 'Dummy'])); |
| 171 | + } |
129 | 172 | }
|
0 commit comments