The international conference on the API Platform Framework
The API Platform Conference 2024 is happening soon, and it's close to selling out.
API Platform 4, Caddy web server, Xdebug, AI... Enjoy two days of inspiring talks with our friendly community and our amazing speakers.
// src/App/Tests.php
namespace App\Tests;
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\ApiResource\Book;
final class BookTest extends ApiTestCase
{
use TestGuideTrait;
public function testBookDoesNotExists(): void
{
$client = static::createClient();
$client->request(method: 'GET', url: '/books/1');
$this->assertResponseStatusCodeSame(404);
$this->assertJsonContains([
'detail' => 'Not Found',
]);
}
public function testGetCollection(): void
{
$response = static::createClient()->request(method: 'GET', url: '/books');
$this->assertMatchesResourceCollectionJsonSchema(Book::class);
$this->assertCount(0, $response->toArray()['hydra:member']);
}
}
// src/App/ApiResource.php
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\CollectionOperationInterface;
#[ApiResource(provider: [Book::class, 'provide'])]
class Book
{
public string $id;
public static function provide($operation)
{
return $operation instanceof CollectionOperationInterface ? [] : null;
}
}
// src/App/Playground.php
namespace App\Playground;
use Symfony\Component\HttpFoundation\Request;
function request(): Request
{
return Request::create(
uri: '/books/1',
method: 'GET',
server: [
'HTTP_ACCEPT' => 'application/ld+json',
]
);
}
Made with love by
Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.
Learn more