Skip to content

Commit

Permalink
[DX][Internal] use Foundry for comic books
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jan 15, 2025
1 parent eb8c5c4 commit 3ad249d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ COPY . /app

WORKDIR /app

ENV PHP_MEMORY_LIMIT=512M

RUN composer global config --no-plugins allow-plugins.symfony/flex true
RUN composer global require --no-progress --no-scripts --no-plugins "symfony/flex:^1.10"
RUN composer update --with-all-dependencies --no-interaction --no-progress
Expand Down
46 changes: 46 additions & 0 deletions tests/Application/src/Foundry/Factory/AuthorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Factory;

use App\Entity\Author;
use Zenstruck\Foundry\ObjectFactory;

/**
* @extends ObjectFactory<Author>
*/
final class AuthorFactory extends ObjectFactory
{
public static function class(): string
{
return Author::class;
}

public function withFirstName(string $firstName): self
{
return $this->with(['firstName' => $firstName]);
}

public function withLastName(string $lastName): self
{
return $this->with(['lastName' => $lastName]);
}

protected function defaults(): array
{
return [
'firstName' => self::faker()->firstName(),
'lastName' => self::faker()->lastName(),
];
}
}
54 changes: 54 additions & 0 deletions tests/Application/src/Foundry/Factory/ComicBookFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Factory;

use App\Entity\Author;
use App\Entity\ComicBook;
use Doctrine\Persistence\Proxy;
use Zenstruck\Foundry\LazyValue;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
* @extends PersistentProxyObjectFactory<ComicBook>
*/
final class ComicBookFactory extends PersistentProxyObjectFactory
{
public static function class(): string
{
return ComicBook::class;
}

public function withTitle(string $title): self
{
return $this->with(['title' => $title]);
}

/**
* @param AuthorFactory|Proxy<Author> $author
*/
public function withAuthor(AuthorFactory|Proxy $author): self
{
return $this->with(['author' => $author]);
}

protected function defaults(): array
{
$author = LazyValue::memoize(fn () => AuthorFactory::createOne());

return [
'title' => ucfirst(self::faker()->words(2, true)),
'author' => $author,
];
}
}
44 changes: 44 additions & 0 deletions tests/Application/src/Foundry/Story/DefaultComicBooksStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Story;

use App\Foundry\Factory\AuthorFactory;
use App\Foundry\Factory\ComicBookFactory;
use Zenstruck\Foundry\Story;

final class DefaultComicBooksStory extends Story
{
public function build(): void
{
ComicBookFactory::new()
->withTitle('Old Man Logan')
->withAuthor(
AuthorFactory::new()
->withFirstName('Andrea')
->withLastName('Sorrentino'),
)
->create()
;

ComicBookFactory::new()
->withTitle('Civil War II')
->withAuthor(
AuthorFactory::new()
->withFirstName('Brian Michael')
->withLastName('Bendis'),
)
->create()
;
}
}
16 changes: 7 additions & 9 deletions tests/Application/src/Foundry/Story/MoreBooksStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@
namespace App\Foundry\Story;

use App\Foundry\Factory\BookFactory;
use function Zenstruck\Foundry\Persistence\flush_after;
use Zenstruck\Foundry\Story;

final class MoreBooksStory extends Story
{
public function build(): void
{
flush_after(function () {
foreach (range(1, 22) as $number) {
BookFactory::new()
->withTitle('Book ' . $number)
->create()
;
}
});
BookFactory::createSequence(
function () {
foreach (range(1, 22) as $number) {
yield ['title' => 'Book ' . $number];
}
},
);
}
}
66 changes: 42 additions & 24 deletions tests/Application/src/Tests/Controller/ComicBookApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@
namespace App\Tests\Controller;

use ApiTestCase\JsonApiTestCase;
use App\Foundry\Factory\AuthorFactory;
use App\Foundry\Factory\ComicBookFactory;
use App\Foundry\Story\DefaultComicBooksStory;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

final class ComicBookApiTest extends JsonApiTestCase
{
use Factories;
use ResetDatabase;

/**
* @test
*/
public function it_allows_creating_a_comic_book()
public function it_allows_creating_a_comic_book(): void
{
$data =
<<<EOT
Expand All @@ -42,7 +50,7 @@ public function it_allows_creating_a_comic_book()
/**
* @test
*/
public function it_allows_versioned_creating_a_comic_book()
public function it_allows_versioned_creating_a_comic_book(): void
{
$this->markAsSkippedIfNecessary();

Expand All @@ -65,9 +73,9 @@ public function it_allows_versioned_creating_a_comic_book()
/**
* @test
*/
public function it_allows_updating_a_comic_book()
public function it_allows_updating_a_comic_book(): void
{
$objects = $this->loadFixturesFromFile('comic_books.yml');
$comicBook = self::someComicBook()->create();

$data =
<<<EOT
Expand All @@ -80,17 +88,17 @@ public function it_allows_updating_a_comic_book()
}
EOT;

$this->client->request('PUT', '/v1/comic-books/' . $objects['comic-book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$this->client->request('PUT', '/v1/comic-books/' . $comicBook->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}

/**
* @test
*/
public function it_allows_updating_partial_information_about_a_comic_book()
public function it_allows_updating_partial_information_about_a_comic_book(): void
{
$objects = $this->loadFixturesFromFile('comic_books.yml');
$comicBook = self::someComicBook()->create();

$data =
<<<EOT
Expand All @@ -102,57 +110,57 @@ public function it_allows_updating_partial_information_about_a_comic_book()
}
EOT;

$this->client->request('PATCH', '/v1/comic-books/' . $objects['comic-book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$this->client->request('PATCH', '/v1/comic-books/' . $comicBook->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}

/**
* @test
*/
public function it_allows_removing_a_comic_book()
public function it_allows_removing_a_comic_book(): void
{
$objects = $this->loadFixturesFromFile('comic_books.yml');
$comicBook = self::someComicBook()->create();

$this->client->request('DELETE', '/v1/comic-books/' . $objects['comic-book1']->getId());
$this->client->request('DELETE', '/v1/comic-books/' . $comicBook->getId());
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}

/**
* @test
*/
public function it_allows_showing_a_comic_book()
public function it_allows_showing_a_comic_book(): void
{
$objects = $this->loadFixturesFromFile('comic_books.yml');
$comicBook = self::someComicBook()->create();

$this->client->request('GET', '/v1/comic-books/' . $objects['comic-book1']->getId());
$this->client->request('GET', '/v1/comic-books/' . $comicBook->getId());
$response = $this->client->getResponse();
$this->assertResponse($response, 'comic-books/show_response');
}

/**
* @test
*/
public function it_allows_versioning_of_a_showing_comic_book_serialization()
public function it_allows_versioning_of_a_showing_comic_book_serialization(): void
{
$this->markAsSkippedIfNecessary();

$objects = $this->loadFixturesFromFile('comic_books.yml');
$comicBook = self::someComicBook()->create();

$this->client->request('GET', '/v1.2/comic-books/' . $objects['comic-book1']->getId());
$this->client->request('GET', '/v1.2/comic-books/' . $comicBook->getId());
$response = $this->client->getResponse();
$this->assertResponse($response, 'comic-books/versioned_show_response');
}

/**
* @test
*/
public function it_allows_indexing_of_comic_books()
public function it_allows_indexing_of_comic_books(): void
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('comic_books.yml');
DefaultComicBooksStory::load();

$this->client->request('GET', '/v1/comic-books/');
$response = $this->client->getResponse();
Expand All @@ -162,11 +170,11 @@ public function it_allows_indexing_of_comic_books()
/**
* @test
*/
public function it_allows_versioned_indexing_of_comic_books()
public function it_allows_versioned_indexing_of_comic_books(): void
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('comic_books.yml');
DefaultComicBooksStory::load();

$this->client->request('GET', '/v1.2/comic-books/');
$response = $this->client->getResponse();
Expand All @@ -176,10 +184,8 @@ public function it_allows_versioned_indexing_of_comic_books()
/**
* @test
*/
public function it_does_not_allow_showing_resource_if_it_does_not_exist()
public function it_does_not_allow_showing_resource_if_it_does_not_exist(): void
{
$this->loadFixturesFromFile('comic_books.yml');

$this->client->request('GET', '/v1/comic-books/3');
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
Expand All @@ -191,4 +197,16 @@ private function markAsSkippedIfNecessary(): void
$this->markTestSkipped();
}
}

private static function someComicBook(): ComicBookFactory
{
return ComicBookFactory::new()
->withTitle('Old Man Logan')
->withAuthor(
AuthorFactory::new()
->withFirstName('Andrea')
->withLastName('Sorrentino'),
)
;
}
}
15 changes: 0 additions & 15 deletions tests/Application/src/Tests/DataFixtures/ORM/comic_books.yml

This file was deleted.

0 comments on commit 3ad249d

Please sign in to comment.