Skip to content

Commit

Permalink
fix: update for guide
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoireHebert authored and soyuka committed Sep 11, 2023
1 parent b8ec98d commit 76c6942
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
25 changes: 10 additions & 15 deletions docs/guides/return-the-iri-of-your-resources-relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(
operations: [
new Get(normalizationContext: ['groups' => ['read']], provider: Brand::class),
new Get(provider: Brand::class.'::provide'),
],
)]
class Brand implements ProviderInterface
class Brand
{
public function __construct(
#[ApiProperty(identifier: true)]
public readonly int $id = 1,

#[Groups('read')]
public readonly string $name = 'Anon',

// Setting uriTemplate on a relation with a resource collection will try to find the related operation.
Expand All @@ -38,13 +35,11 @@ public function __construct(
* @var array<int, Car> $cars
*/
#[ApiProperty(uriTemplate: '/brands/{brandId}/cars')]
#[Groups('read')]
private array $cars = [],

// Setting uriTemplate on a relation with a resource item will try to find the related operation.
// It is based on the uriTemplate set on the operation defined on the Address resource (see below).
#[ApiProperty(uriTemplate: '/brands/{brandId}/addresses/{id}')]
#[Groups('read')]
private ?Address $headQuarters = null
)
{
Expand Down Expand Up @@ -79,9 +74,9 @@ public function setHeadQuarters(?Address $headQuarters): self
return $this;
}

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
return (new self(1, 'Ford'))
return (new Brand(1, 'Ford'))
->setHeadQuarters(new Address(1, 'One American Road near Michigan Avenue, Dearborn, Michigan'))
->addCar(new Car(1, 'Torpedo Roadster'));
}
Expand Down Expand Up @@ -142,7 +137,6 @@ class Address
public function __construct(
#[ApiProperty(identifier: true)]
public readonly int $id = 1,
#[Groups('read')]
public readonly string $name = 'Anon',
private ?Brand $brand = null
)
Expand Down Expand Up @@ -176,30 +170,31 @@ public function setBrand(Brand $brand): void

function request(): Request
{
return Request::create('/brands/1.jsonld', 'GET');
return Request::create(uri: '/brands/1', method: 'GET', server: ['HTTP_ACCEPT' => 'application/ld+json']);
}
}


namespace App\Tests {
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\ApiResource\Brand;

final class BrandTest extends ApiTestCase
{
use TestGuideTrait;

public function testResourceExposeIRI(): void
{
static::createClient()->request('GET', '/brands/1.jsonld');
static::createClient()->request('GET', '/brands/1', ['headers' => [
'Accept: application/ld+json'
]]);

$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Brand::class, '_api_/brand{._format}_get_item');
$this->assertMatchesResourceCollectionJsonSchema(Brand::class, '_api_/brands/{id}{._format}_get');
$this->assertJsonContains([
"@context" => "/contexts/Brand",
"@id" => "/brands/1",
"@type" => "Brand",
"name"=> "Ford",
"cars" => "/brands/1/cars",
"headQuarters" => "/brands/1/addresses/1"
]);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function configureContainer(ContainerConfigurator $container, LoaderInte
sprintf('sqlite:///%s/%s', $this->getCacheDir(), 'data.db')
);

$services->set('doctrine.orm.default_metadata_driver', StaticMappingDriver::class)->args(['$classes' => $resources]);
$services->set('doctrine.orm.default_metadata_driver', StaticMappingDriver::class)->args(['$classes' => $entities]);

if (\function_exists('App\DependencyInjection\configure')) {
configure($container);
Expand Down

0 comments on commit 76c6942

Please sign in to comment.