Skip to content

Commit

Permalink
Use collection-oriented repository pattern and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Dec 27, 2023
1 parent 044a453 commit 04990c2
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 61 deletions.
1 change: 1 addition & 0 deletions config/packages/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[
'dbal' => [
'url' => '%env(resolve:DATABASE_URL)%',
'use_savepoints' => true,
],
'orm' => [
'report_fields_where_declared' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(CreateBookCommand $command): Book
$command->price,
);

$this->bookRepository->save($book);
$this->bookRepository->add($book);

return $book;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
interface BookRepositoryInterface extends RepositoryInterface
{
public function save(Book $book): void;
public function add(Book $book): void;

public function remove(Book $book): void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(EntityManagerInterface $em)
parent::__construct($em, self::ENTITY_CLASS, self::ALIAS);
}

public function save(Book $book): void
public function add(Book $book): void
{
$this->em->persist($book);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
final class InMemoryBookRepository extends InMemoryRepository implements BookRepositoryInterface
{
public function save(Book $book): void
public function add(Book $book): void
{
$this->entities[(string) $book->id()] = $book;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Acceptance/AnonymizeBooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testAnonymizeAuthorOfBooks(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

for ($i = 0; $i < 10; ++$i) {
$bookRepository->save(DummyBookFactory::createBook(author: sprintf('author_%d', $i)));
$bookRepository->add(DummyBookFactory::createBook(author: sprintf('author_%d', $i)));
}

$response = $client->request('POST', '/api/books/anonymize', [
Expand Down
16 changes: 8 additions & 8 deletions tests/BookStore/Acceptance/BookCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testReturnPaginatedBooks(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

for ($i = 0; $i < 100; ++$i) {
$bookRepository->save(DummyBookFactory::createBook());
$bookRepository->add(DummyBookFactory::createBook());
}

$client->request('GET', '/api/books');
Expand All @@ -51,9 +51,9 @@ public function testFilterBooksByAuthor(): void
/** @var BookRepositoryInterface $bookRepository */
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$bookRepository->save(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->save(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->save(DummyBookFactory::createBook(author: 'authorTwo'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorTwo'));

$client->request('GET', '/api/books?author=authorOne');

Expand Down Expand Up @@ -83,7 +83,7 @@ public function testReturnBook(): void
content: 'content',
price: 1000,
);
$bookRepository->save($book);
$bookRepository->add($book);

$client->request('GET', sprintf('/api/books/%s', (string) $book->id()));

Expand Down Expand Up @@ -186,7 +186,7 @@ public function testUpdateBook(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$book = DummyBookFactory::createBook();
$bookRepository->save($book);
$bookRepository->add($book);

$client->request('PUT', sprintf('/api/books/%s', $book->id()), [
'json' => [
Expand Down Expand Up @@ -227,7 +227,7 @@ public function testPartiallyUpdateBook(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$book = DummyBookFactory::createBook(name: 'name', description: 'description');
$bookRepository->save($book);
$bookRepository->add($book);

$client->request('PATCH', sprintf('/api/books/%s', $book->id()), [
'headers' => [
Expand Down Expand Up @@ -260,7 +260,7 @@ public function testDeleteBook(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$book = DummyBookFactory::createBook();
$bookRepository->save($book);
$bookRepository->add($book);

$response = $client->request('DELETE', sprintf('/api/books/%s', $book->id()));

Expand Down
4 changes: 2 additions & 2 deletions tests/BookStore/Acceptance/CheapestBooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testReturnOnlyTheTenCheapestBooks(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

for ($i = 0; $i < 20; ++$i) {
$bookRepository->save(DummyBookFactory::createBook(price: $i));
$bookRepository->add(DummyBookFactory::createBook(price: $i));
}

$response = $client->request('GET', '/api/books/cheapest');
Expand All @@ -46,7 +46,7 @@ public function testReturnBooksSortedByPrice(): void

$prices = [2000, 1000, 3000];
foreach ($prices as $price) {
$bookRepository->save(DummyBookFactory::createBook(price: $price));
$bookRepository->add(DummyBookFactory::createBook(price: $price));
}

$response = $client->request('GET', '/api/books/cheapest');
Expand Down
4 changes: 2 additions & 2 deletions tests/BookStore/Acceptance/DiscountBookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testApplyADiscountOnBook(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$book = DummyBookFactory::createBook(price: 1000);
$bookRepository->save($book);
$bookRepository->add($book);

$client->request('POST', sprintf('/api/books/%s/discount', $book->id()), [
'json' => [
Expand All @@ -43,7 +43,7 @@ public function testValidateDiscountAmount(): void
$bookRepository = static::getContainer()->get(BookRepositoryInterface::class);

$book = DummyBookFactory::createBook(price: 1000);
$bookRepository->save($book);
$bookRepository->add($book);

$client->request('POST', sprintf('/api/books/%s/discount', $book->id()), [
'json' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Functional/AnonymizeBooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testAnonymizeAuthorOfBooks(): void
$commandBus = static::getContainer()->get(CommandBusInterface::class);

for ($i = 0; $i < 10; ++$i) {
$bookRepository->save(DummyBookFactory::createBook(author: sprintf('author_%d', $i)));
$bookRepository->add(DummyBookFactory::createBook(author: sprintf('author_%d', $i)));
}

$commandBus->dispatch(new AnonymizeBooksCommand('anon.'));
Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Functional/DeleteBookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testDeleteBook(): void
$commandBus = static::getContainer()->get(CommandBusInterface::class);

$book = DummyBookFactory::createBook();
$bookRepository->save($book);
$bookRepository->add($book);

static::assertCount(1, $bookRepository);

Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Functional/DiscountBookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testApplyADiscountOnBook(int $initialAmount, int $discount, int
$commandBus = static::getContainer()->get(CommandBusInterface::class);

$book = DummyBookFactory::createBook(price: $initialAmount);
$bookRepository->save($book);
$bookRepository->add($book);

$commandBus->dispatch(new DiscountBookCommand($book->id(), new Discount($discount)));

Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Functional/FindBookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testFindBook(): void
$queryBus = static::getContainer()->get(QueryBusInterface::class);

$book = DummyBookFactory::createBook();
$bookRepository->save($book);
$bookRepository->add($book);

static::assertSame($book, $queryBus->ask(new FindBookQuery($book->id())));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/BookStore/Functional/FindBooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testFindBooks(): void
];

foreach ($initialBooks as $book) {
$bookRepository->save($book);
$bookRepository->add($book);
}

$books = $queryBus->ask(new FindBooksQuery());
Expand All @@ -49,9 +49,9 @@ public function testFilterBooksByAuthor(): void
/** @var QueryBusInterface $queryBus */
$queryBus = static::getContainer()->get(QueryBusInterface::class);

$bookRepository->save(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->save(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->save(DummyBookFactory::createBook(author: 'authorTwo'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorOne'));
$bookRepository->add(DummyBookFactory::createBook(author: 'authorTwo'));

static::assertCount(3, $bookRepository);

Expand Down Expand Up @@ -80,7 +80,7 @@ public function testReturnPaginatedBooks(): void
];

foreach ($initialBooks as $book) {
$bookRepository->save($book);
$bookRepository->add($book);
}

$books = $queryBus->ask(new FindBooksQuery(page: 2, itemsPerPage: 2));
Expand Down
4 changes: 2 additions & 2 deletions tests/BookStore/Functional/FindCheapestBooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testReturnOnlyTheCheapestBooks(): void
$queryBus = static::getContainer()->get(QueryBusInterface::class);

for ($i = 0; $i < 5; ++$i) {
$bookRepository->save(DummyBookFactory::createBook());
$bookRepository->add(DummyBookFactory::createBook());
}

$cheapestBooks = $queryBus->ask(new FindCheapestBooksQuery(3));
Expand All @@ -40,7 +40,7 @@ public function testReturnBooksSortedByPrice(): void

$prices = [2000, 1000, 3000];
foreach ($prices as $price) {
$bookRepository->save(DummyBookFactory::createBook(price: $price));
$bookRepository->add(DummyBookFactory::createBook(price: $price));
}

$cheapestBooks = $queryBus->ask(new FindCheapestBooksQuery(3));
Expand Down
2 changes: 1 addition & 1 deletion tests/BookStore/Functional/UpdateBookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testUpdateBook(): void
price: 1000,
);

$bookRepository->save($initialBook);
$bookRepository->add($initialBook);

$commandBus->dispatch(new UpdateBookCommand(
$initialBook->id(),
Expand Down
47 changes: 27 additions & 20 deletions tests/BookStore/Integration/Doctrine/DoctrineBookRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\BookStore\Infrastructure\Doctrine\DoctrineBookRepository;
use App\Shared\Infrastructure\Doctrine\DoctrinePaginator;
use App\Tests\BookStore\DummyFactory\DummyBookFactory;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand All @@ -17,13 +16,11 @@

final class DoctrineBookRepositoryTest extends KernelTestCase
{
private static Connection $connection;
private static EntityManagerInterface $em;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

static::$connection = static::getContainer()->get(Connection::class);
static::bootKernel();

(new Application(static::$kernel))
->find('doctrine:database:create')
Expand All @@ -36,7 +33,8 @@ public static function setUpBeforeClass(): void

protected function setUp(): void
{
static::$connection->executeStatement('TRUNCATE book');
static::$em = static::getContainer()->get(EntityManagerInterface::class);
static::$em->getConnection()->executeStatement('TRUNCATE book');
}

public function testSave(): void
Expand All @@ -47,7 +45,8 @@ public function testSave(): void
static::assertEmpty($repository);

$book = DummyBookFactory::createBook();
$repository->save($book);
$repository->add($book);
self::$em->flush();

static::assertCount(1, $repository);
}
Expand All @@ -58,11 +57,14 @@ public function testRemove(): void
$repository = static::getContainer()->get(DoctrineBookRepository::class);

$book = DummyBookFactory::createBook();
$repository->save($book);
$repository->add($book);
self::$em->flush();

static::assertCount(1, $repository);

$repository->remove($book);
self::$em->flush();

static::assertEmpty($repository);
}

Expand All @@ -74,9 +76,9 @@ public function testOfId(): void
static::assertEmpty($repository);

$book = DummyBookFactory::createBook();
$repository->save($book);

static::getContainer()->get(EntityManagerInterface::class)->clear();
$repository->add($book);
self::$em->flush();
self::$em->clear();

static::assertEquals($book, $repository->ofId($book->id()));
}
Expand All @@ -86,9 +88,10 @@ public function testWithAuthor(): void
/** @var DoctrineBookRepository $repository */
$repository = static::getContainer()->get(DoctrineBookRepository::class);

$repository->save(DummyBookFactory::createBook(author: 'authorOne'));
$repository->save(DummyBookFactory::createBook(author: 'authorOne'));
$repository->save(DummyBookFactory::createBook(author: 'authorTwo'));
$repository->add(DummyBookFactory::createBook(author: 'authorOne'));
$repository->add(DummyBookFactory::createBook(author: 'authorOne'));
$repository->add(DummyBookFactory::createBook(author: 'authorTwo'));
self::$em->flush();

static::assertCount(2, $repository->withAuthor(new Author('authorOne')));
static::assertCount(1, $repository->withAuthor(new Author('authorTwo')));
Expand All @@ -99,9 +102,10 @@ public function testWithCheapestsFirst(): void
/** @var DoctrineBookRepository $repository */
$repository = static::getContainer()->get(DoctrineBookRepository::class);

$repository->save(DummyBookFactory::createBook(price: 1));
$repository->save(DummyBookFactory::createBook(price: 3));
$repository->save(DummyBookFactory::createBook(price: 2));
$repository->add(DummyBookFactory::createBook(price: 1));
$repository->add(DummyBookFactory::createBook(price: 3));
$repository->add(DummyBookFactory::createBook(price: 2));
self::$em->flush();

$prices = [];
foreach ($repository->withCheapestsFirst() as $book) {
Expand Down Expand Up @@ -144,8 +148,9 @@ public function testIteratorWithoutPagination(): void
DummyBookFactory::createBook(),
];
foreach ($books as $book) {
$repository->save($book);
$repository->add($book);
}
self::$em->flush();

$i = 0;
foreach ($repository as $book) {
Expand All @@ -167,8 +172,9 @@ public function testIteratorWithPagination(): void
];

foreach ($books as $book) {
$repository->save($book);
$repository->add($book);
}
self::$em->flush();

$repository = $repository->withPagination(1, 2);

Expand Down Expand Up @@ -202,8 +208,9 @@ public function testCount(): void
DummyBookFactory::createBook(),
];
foreach ($books as $book) {
$repository->save($book);
$repository->add($book);
}
self::$em->flush();

static::assertCount(count($books), $repository);
static::assertCount(2, $repository->withPagination(1, 2));
Expand Down
Loading

0 comments on commit 04990c2

Please sign in to comment.