diff --git a/castor.php b/castor.php index 37d9542c..d347b605 100644 --- a/castor.php +++ b/castor.php @@ -154,3 +154,11 @@ function doc_build_github_pages() run('git reset --hard gh-pages', context: $context); } + +#[AsTask('serve', namespace: 'test', description: 'Serve the symfony app in tests')] +function test_serve($prod = false): void +{ + $file = $prod ? 'index.php' : 'dev.php'; + + run('php -S localhost:8000 tests/Bundle/Resources/public/' . $file, context: context()->withAllowFailure()); +} \ No newline at end of file diff --git a/tests/Bundle/ApiPlatformTest.php b/tests/Bundle/ApiPlatformTest.php index 78c2e93f..d12106a8 100644 --- a/tests/Bundle/ApiPlatformTest.php +++ b/tests/Bundle/ApiPlatformTest.php @@ -21,6 +21,20 @@ protected function setUp(): void self::bootKernel(); } + public function testGetShelf(): void + { + $response = static::createClient()->request('GET', '/shelf'); + + $this->assertResponseIsSuccessful(); + + $this->assertJsonContains([ + '@context' => '/contexts/Shelf', + '@id' => '/shelf', + '@type' => 'Shelf', + 'books' => ["/books/1", "/books/2"], + ]); + } + public function testGetBookCollectionOnApip3(): void { if (version_compare(\Composer\InstalledVersions::getVersion('api-platform/core'), '4', '>=')) { diff --git a/tests/Bundle/Resources/App/Api/Entity/Shelf.php b/tests/Bundle/Resources/App/Api/Entity/Shelf.php new file mode 100644 index 00000000..b93a8dce --- /dev/null +++ b/tests/Bundle/Resources/App/Api/Entity/Shelf.php @@ -0,0 +1,35 @@ + ['shelf']], + provider: ShelfProvider::class, + ), + ], +)] +#[Mapper(source: 'array', target: 'array')] +class Shelf +{ + public function __construct( + #[Groups('shelf')] + public int $id, + #[Groups('shelf')] + /** @var Book[] $books */ + public Collection $books, + ) + { + } +} diff --git a/tests/Bundle/Resources/App/Api/Provider/ShelfProvider.php b/tests/Bundle/Resources/App/Api/Provider/ShelfProvider.php new file mode 100644 index 00000000..6025bbbb --- /dev/null +++ b/tests/Bundle/Resources/App/Api/Provider/ShelfProvider.php @@ -0,0 +1,27 @@ +title = 'The Book'; + $book->id = 1; + + $secondBook = new Book(); + $secondBook->title = 'Another Book'; + $secondBook->id = 2; + + return new Shelf(1, new ArrayCollection([$book, $secondBook])); + } +} diff --git a/tests/Bundle/Resources/config/packages/api_platform.yaml b/tests/Bundle/Resources/config/packages/api_platform.yaml index 5b6a6714..cd71c643 100644 --- a/tests/Bundle/Resources/config/packages/api_platform.yaml +++ b/tests/Bundle/Resources/config/packages/api_platform.yaml @@ -8,5 +8,4 @@ api_platform: - '%kernel.project_dir%/App/Api/Entity' formats: jsonld: [ 'application/ld+json' ] - json: [ 'application/json' ] - html: [ 'text/html' ] \ No newline at end of file + json: [ 'application/json' ] \ No newline at end of file diff --git a/tests/Bundle/Resources/config/routes/dev/profiler.yaml b/tests/Bundle/Resources/config/routes/dev/profiler.yaml index cab9dc00..5e851c69 100644 --- a/tests/Bundle/Resources/config/routes/dev/profiler.yaml +++ b/tests/Bundle/Resources/config/routes/dev/profiler.yaml @@ -1,7 +1,7 @@ web_profiler_wdt: - resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml' + resource: '@WebProfilerBundle/Resources/config/routing/wdt.php' prefix: /_wdt web_profiler_profiler: - resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml' + resource: '@WebProfilerBundle/Resources/config/routing/profiler.php' prefix: /_profiler \ No newline at end of file