// 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()['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;
}
}
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