Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/hydra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.metadata.property.name_collection_factory" />
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
<argument type="service" id="api_platform.jsonld.context_builder" />
<argument type="service" id="api_platform.resource_class_resolver" />
<argument type="service" id="api_platform.operation_method_resolver" />
<argument type="service" id="api_platform.router" />
Expand Down
12 changes: 5 additions & 7 deletions src/Hydra/ApiDocumentationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ final class ApiDocumentationBuilder implements ApiDocumentationBuilderInterface
private $resourceMetadataFactory;
private $propertyNameCollectionFactory;
private $propertyMetadataFactory;
private $contextBuilder;
private $resourceClassResolver;
private $operationMethodResolver;
private $urlGenerator;
private $title;
private $description;

public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '')
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '')
{
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
$this->propertyMetadataFactory = $propertyMetadataFactory;
$this->contextBuilder = $contextBuilder;
$this->resourceClassResolver = $resourceClassResolver;
$this->operationMethodResolver = $operationMethodResolver;
$this->urlGenerator = $urlGenerator;
Expand Down Expand Up @@ -404,9 +402,10 @@ private function getRange(PropertyMetadata $propertyMetadata)
*/
private function getContext() : array
{
return array_merge(
$this->contextBuilder->getBaseContext(UrlGeneratorInterface::ABS_URL),
return
[
'@vocab' => $this->urlGenerator->generate('api_hydra_doc', [], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => ContextBuilderInterface::HYDRA_NS,
'rdf' => ContextBuilderInterface::RDF_NS,
'rdfs' => ContextBuilderInterface::RDFS_NS,
'xmls' => ContextBuilderInterface::XML_NS,
Expand All @@ -416,7 +415,6 @@ private function getContext() : array
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
'expects' => ['@id' => 'hydra:expects', '@type' => '@id'],
'returns' => ['@id' => 'hydra:returns', '@type' => '@id'],
]
);
];
}
}
252 changes: 252 additions & 0 deletions tests/Hydra/ApiDocumentationBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?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\Hydra;

use ApiPlatform\Core\Api\OperationMethodResolverInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\Hydra\ApiDocumentationBuilder;
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 ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use Prophecy\Argument;
use Symfony\Component\PropertyInfo\Type;

/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
class ApiDocumentationBuilderTest extends \PHPUnit_Framework_TestCase /**/
{
public function testGetApiDocumention()
{
$title = 'Test Api';
$desc = 'test ApiGerard';
$formats = ['jsonld' => ['application/ld+json']];

$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
$resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection(['dummy' => 'dummy']))->shouldBeCalled();

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create('dummy', [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['name']));

$dummyMetadata = new ResourceMetadata('dummy', 'dummy', '#dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST']], []);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn($dummyMetadata);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create('dummy', 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name', true, true, true, true, false, false, null, []));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true);

$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
$operationMethodResolverProphecy->getItemOperationMethod('dummy', 'get')->shouldBeCalled()->willReturn('GET');
$operationMethodResolverProphecy->getItemOperationMethod('dummy', 'put')->shouldBeCalled()->willReturn('PUT');
$operationMethodResolverProphecy->getCollectionOperationMethod('dummy', 'get')->shouldBeCalled()->willReturn('GET');
$operationMethodResolverProphecy->getCollectionOperationMethod('dummy', 'post')->shouldBeCalled()->willReturn('POST');

$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('api_hydra_doc')->willReturn('/doc')->shouldBeCalled(1);
$urlGenerator->generate('api_hydra_entrypoint')->willReturn('/')->shouldBeCalled(1);

$urlGenerator->generate('api_hydra_doc', [], UrlGeneratorInterface::ABS_URL)->willReturn('/doc')->shouldBeCalled(1);

$apiDocumentationBuilder = new ApiDocumentationBuilder(
$resourceNameCollectionFactoryProphecy->reveal(),
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$operationMethodResolverProphecy->reveal(),
$urlGenerator->reveal(),
$title,
$desc);

$expected = [
'@context' => [
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'hydra:expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'hydra:returns',
'@type' => '@id',
],
],
'@id' => '/doc',
'hydra:title' => 'Test Api',
'hydra:description' => 'test ApiGerard',
'hydra:supportedClass' => [
0 => [
'@id' => '#dummy',
'@type' => 'hydra:Class',
'rdfs:label' => 'dummy',
'hydra:title' => 'dummy',
'hydra:description' => 'dummy',
'hydra:supportedProperty' => [0 => [
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#dummy/name',
'@type' => 'rdf:Property',
'rdfs:label' => 'name',
'domain' => '#dummy',
'range' => 'xmls:string',
],
'hydra:title' => 'name',
'hydra:required' => false,
'hydra:readable' => true,
'hydra:writable' => true,
'hydra:description' => 'name',
]],
'hydra:supportedOperation' => [
0 => [
'@type' => 'hydra:Operation',
'hydra:method' => 'GET',
'hydra:title' => 'Retrieves dummy resource.',
'rdfs:label' => 'Retrieves dummy resource.',
'returns' => '#dummy',
],
1 => [
'@type' => 'hydra:ReplaceResourceOperation',
'expects' => '#dummy',
'hydra:method' => 'PUT',
'hydra:title' => 'Replaces the dummy resource.',
'rdfs:label' => 'Replaces the dummy resource.',
'returns' => '#dummy',
],
],
],
1 => [
'@id' => '#Entrypoint',
'@type' => 'hydra:Class',
'hydra:title' => 'The API entrypoint',
'hydra:supportedProperty' => [0 => [
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#Entrypoint/dummy',
'@type' => 'hydra:Link',
'rdfs:label' => 'The collection of dummy resources',
'domain' => '#Entrypoint',
'range' => 'hydra:PagedCollection',
'hydra:supportedOperation' => [
0 => [
'@type' => 'hydra:Operation',
'hydra:method' => 'GET',
'hydra:title' => 'Retrieves the collection of dummy resources.',
'rdfs:label' => 'Retrieves the collection of dummy resources.',
'returns' => 'hydra:PagedCollection',
],
1 => [
'@type' => 'hydra:CreateResourceOperation',
'expects' => '#dummy',
'hydra:method' => 'POST',
'hydra:title' => 'Creates a dummy resource.',
'rdfs:label' => 'Creates a dummy resource.',
'returns' => '#dummy',
],
],
],
'hydra:title' => 'The collection of dummy resources',
'hydra:readable' => true,
'hydra:writable' => false,
]],
'hydra:supportedOperation' => [
'@type' => 'hydra:Operation',
'hydra:method' => 'GET',
'rdfs:label' => 'The API entrypoint.',
'returns' => '#EntryPoint',
],
],
2 => [
'@id' => '#ConstraintViolation',
'@type' => 'hydra:Class',
'hydra:title' => 'A constraint violation',
'hydra:supportedProperty' => [
0 => ['@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#ConstraintViolation/propertyPath',
'@type' => 'rdf:Property',
'rdfs:label' => 'propertyPath',
'domain' => '#ConstraintViolation',
'range' => 'xmls:string',
],
'hydra:title' => 'propertyPath',
'hydra:description' => 'The property path of the violation',
'hydra:readable' => true,
'hydra:writable' => false,
],
1 => [
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#ConstraintViolation/message',
'@type' => 'rdf:Property',
'rdfs:label' => 'message',
'domain' => '#ConstraintViolation',
'range' => 'xmls:string',
],
'hydra:title' => 'message',
'hydra:description' => 'The message associated with the violation',
'hydra:readable' => true,
'hydra:writable' => false,
],
],
],
3 => [
'@id' => '#ConstraintViolationList',
'@type' => 'hydra:Class',
'subClassOf' => 'hydra:Error',
'hydra:title' => 'A constraint violation list',
'hydra:supportedProperty' => [0 => [
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => '#ConstraintViolationList/violation',
'@type' => 'rdf:Property',
'rdfs:label' => 'violation',
'domain' => '#ConstraintViolationList',
'range' => '#ConstraintViolation',
],
'hydra:title' => 'violation',
'hydra:description' => 'The violations',
'hydra:readable' => true,
'hydra:writable' => false,
]],
],
],
'hydra:entrypoint' => '/',

];
$this->assertEquals($expected, $apiDocumentationBuilder->getApiDocumentation());
}
}
Loading