Skip to content
Open
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
8 changes: 8 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
14 changes: 14 additions & 0 deletions tests/Bundle/ApiPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '>=')) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Bundle/Resources/App/Api/Entity/Shelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Resources\App\Api\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use AutoMapper\Attribute\Mapper;
use AutoMapper\Tests\Bundle\Resources\App\Api\Provider\ShelfProvider;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/shelf',
normalizationContext: ['groups' => ['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,
)
{
}
}
27 changes: 27 additions & 0 deletions tests/Bundle/Resources/App/Api/Provider/ShelfProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Bundle\Resources\App\Api\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use AutoMapper\Tests\Bundle\Resources\App\Api\Entity\Book;
use AutoMapper\Tests\Bundle\Resources\App\Api\Entity\Shelf;
use Doctrine\Common\Collections\ArrayCollection;

class ShelfProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Shelf
{
$book = new Book();
$book->title = 'The Book';
$book->id = 1;

$secondBook = new Book();
$secondBook->title = 'Another Book';
$secondBook->id = 2;

return new Shelf(1, new ArrayCollection([$book, $secondBook]));
}
}
3 changes: 1 addition & 2 deletions tests/Bundle/Resources/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ api_platform:
- '%kernel.project_dir%/App/Api/Entity'
formats:
jsonld: [ 'application/ld+json' ]
json: [ 'application/json' ]
html: [ 'text/html' ]
json: [ 'application/json' ]
4 changes: 2 additions & 2 deletions tests/Bundle/Resources/config/routes/dev/profiler.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading