-
-
Notifications
You must be signed in to change notification settings - Fork 857
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #752 from maechler/feature/typedValues
Adds jsonld context, Adds support for jsonld typed values
- Loading branch information
Showing
6 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiProperty; | ||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* Jsonld Context Dummy. | ||
* | ||
* | ||
* @ApiResource | ||
* @ORM\Entity | ||
*/ | ||
class JsonldContextDummy | ||
{ | ||
/** | ||
* @var int The id. | ||
* | ||
* @ApiProperty(identifier=true) | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var string The dummy person. | ||
* | ||
* @ApiProperty( | ||
* attributes={ | ||
* "jsonld_context"= { | ||
* "@id"="http://example.com/id", | ||
* "@type"="@id", | ||
* "foo"="bar" | ||
* } | ||
* }, | ||
* ) | ||
*/ | ||
private $person; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setPerson($person) | ||
{ | ||
$this->person = $person; | ||
} | ||
|
||
public function getPerson() | ||
{ | ||
return $this->person; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Tests\JsonLd; | ||
|
||
use ApiPlatform\Core\Api\UrlGeneratorInterface; | ||
use ApiPlatform\Core\JsonLd\ContextBuilder; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* @author Markus Mächler <markus.maechler@bithost.ch> | ||
*/ | ||
class ContextBuilderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $entityClass; | ||
private $resourceNameCollectionFactoryProphecy; | ||
private $resourceMetadataFactoryProphecy; | ||
private $propertyNameCollectionFactoryProphecy; | ||
private $propertyMetadataFactoryProphecy; | ||
private $urlGeneratorProphecy; | ||
|
||
public function setUp() | ||
{ | ||
$this->entityClass = '\Dummy\DummyEntity'; | ||
$this->resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class); | ||
$this->resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); | ||
$this->propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); | ||
$this->propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); | ||
$this->urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class); | ||
} | ||
|
||
public function testResourceContext() | ||
{ | ||
$this->resourceMetadataFactoryProphecy->create($this->entityClass)->willReturn(new ResourceMetadata('DummyEntity')); | ||
$this->propertyNameCollectionFactoryProphecy->create($this->entityClass)->willReturn(new PropertyNameCollection(['dummyPropertyA'])); | ||
$this->propertyMetadataFactoryProphecy->create($this->entityClass, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, [])); | ||
|
||
$contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal()); | ||
|
||
$expected = [ | ||
'@vocab' => '#', | ||
'hydra' => 'http://www.w3.org/ns/hydra/core#', | ||
'dummyPropertyA' => '#DummyEntity/dummyPropertyA', | ||
]; | ||
|
||
$this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass)); | ||
} | ||
|
||
public function testResourceContextWithJsonldContext() | ||
{ | ||
$this->resourceMetadataFactoryProphecy->create($this->entityClass)->willReturn(new ResourceMetadata('DummyEntity')); | ||
$this->propertyNameCollectionFactoryProphecy->create($this->entityClass)->willReturn(new PropertyNameCollection(['dummyPropertyA'])); | ||
$this->propertyMetadataFactoryProphecy->create($this->entityClass, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, ['jsonld_context' => ['@type' => '@id', '@id' => 'customId', 'foo' => 'bar']])); | ||
|
||
$contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal()); | ||
|
||
$expected = [ | ||
'@vocab' => '#', | ||
'hydra' => 'http://www.w3.org/ns/hydra/core#', | ||
'dummyPropertyA' => [ | ||
'@type' => '@id', | ||
'@id' => 'customId', | ||
'foo' => 'bar', | ||
], | ||
]; | ||
|
||
$this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass)); | ||
} | ||
|
||
public function testResourceContextWithReverse() | ||
{ | ||
$this->resourceMetadataFactoryProphecy->create($this->entityClass)->willReturn(new ResourceMetadata('DummyEntity')); | ||
$this->propertyNameCollectionFactoryProphecy->create($this->entityClass)->willReturn(new PropertyNameCollection(['dummyPropertyA'])); | ||
$this->propertyMetadataFactoryProphecy->create($this->entityClass, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, ['jsonld_context' => ['@reverse' => 'parent']])); | ||
|
||
$contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal()); | ||
|
||
$expected = [ | ||
'@vocab' => '#', | ||
'hydra' => 'http://www.w3.org/ns/hydra/core#', | ||
'dummyPropertyA' => [ | ||
'@id' => '#DummyEntity/dummyPropertyA', | ||
'@reverse' => 'parent' | ||
], | ||
]; | ||
|
||
$this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass)); | ||
} | ||
} |