Skip to content

Commit 7e68139

Browse files
authored
Merge pull request #2554 from soyuka/fix-2551
Test ContextBuilder with anonymous objects
2 parents cdb70b7 + 9a74e35 commit 7e68139

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

src/JsonLd/AnonymousContextBuilderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ interface AnonymousContextBuilderInterface extends ContextBuilderInterface
2626
* Creates a JSON-LD context based on the given object.
2727
* Usually this is used with an Input or Output DTO object.
2828
*/
29-
public function getAnonymousResourceContext($object, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): array;
29+
public function getAnonymousResourceContext($object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array;
3030
}

src/JsonLd/ContextBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getResourceContextUri(string $resourceClass, int $referenceType
108108
/**
109109
* {@inheritdoc}
110110
*/
111-
public function getAnonymousResourceContext($object, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
111+
public function getAnonymousResourceContext($object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array
112112
{
113113
$id = $context['iri'] ?? '_:'.(\function_exists('spl_object_id') ? spl_object_id($object) : spl_object_hash($object));
114114
$jsonLdContext = [

src/JsonLd/Serializer/JsonLdContextTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ private function createJsonLdContext(AnonymousContextBuilderInterface $contextBu
5555

5656
$context['jsonld_has_context'] = true;
5757

58-
return $contextBuilder->getAnonymousResourceContext($object, $context['output']);
58+
return $contextBuilder->getAnonymousResourceContext($object, $context['output'] ?? []);
5959
}
6060
}

tests/JsonLd/ContextBuilderTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
2424
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2525
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
26+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2627
use PHPUnit\Framework\TestCase;
2728
use Symfony\Component\PropertyInfo\Type;
2829

@@ -126,4 +127,46 @@ public function testResourceContextWithReverse()
126127

127128
$this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass));
128129
}
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+
}
129172
}

0 commit comments

Comments
 (0)