From 044a453098d448a452bc886fa192fdc95bb19def Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Wed, 27 Dec 2023 06:53:33 -0500 Subject: [PATCH 1/3] Implementing ACID Transactions --- config/packages/messenger.php | 6 +++++- .../Application/Command/AnonymizeBooksCommandHandler.php | 2 -- .../Application/Command/CreateBookCommandHandler.php | 2 +- .../Application/Command/DiscountBookCommandHandler.php | 2 -- .../Application/Command/UpdateBookCommandHandler.php | 2 -- .../Infrastructure/Doctrine/DoctrineBookRepository.php | 2 -- 6 files changed, 6 insertions(+), 10 deletions(-) diff --git a/config/packages/messenger.php b/config/packages/messenger.php index 13d1930..d32f87f 100644 --- a/config/packages/messenger.php +++ b/config/packages/messenger.php @@ -11,7 +11,11 @@ 'messenger' => [ 'default_bus' => 'command.bus', 'buses' => [ - 'command.bus' => [], + 'command.bus' => [ + 'middleware' => [ + 'messenger.middleware.doctrine_transaction', + ], + ], 'query.bus' => [], ], 'transports' => [ diff --git a/src/BookStore/Application/Command/AnonymizeBooksCommandHandler.php b/src/BookStore/Application/Command/AnonymizeBooksCommandHandler.php index 96b66ee..a96cb79 100644 --- a/src/BookStore/Application/Command/AnonymizeBooksCommandHandler.php +++ b/src/BookStore/Application/Command/AnonymizeBooksCommandHandler.php @@ -23,8 +23,6 @@ public function __invoke(AnonymizeBooksCommand $command): void $book->update( author: new Author($command->anonymizedName), ); - - $this->bookRepository->save($book); } } } diff --git a/src/BookStore/Application/Command/CreateBookCommandHandler.php b/src/BookStore/Application/Command/CreateBookCommandHandler.php index 90d6245..a49b4a5 100644 --- a/src/BookStore/Application/Command/CreateBookCommandHandler.php +++ b/src/BookStore/Application/Command/CreateBookCommandHandler.php @@ -11,7 +11,7 @@ #[AsCommandHandler] final readonly class CreateBookCommandHandler { - public function __construct(private readonly BookRepositoryInterface $bookRepository) + public function __construct(private BookRepositoryInterface $bookRepository) { } diff --git a/src/BookStore/Application/Command/DiscountBookCommandHandler.php b/src/BookStore/Application/Command/DiscountBookCommandHandler.php index 5bcfe2e..6ee4241 100644 --- a/src/BookStore/Application/Command/DiscountBookCommandHandler.php +++ b/src/BookStore/Application/Command/DiscountBookCommandHandler.php @@ -23,7 +23,5 @@ public function __invoke(DiscountBookCommand $command): void } $book->applyDiscount($command->discount); - - $this->bookRepository->save($book); } } diff --git a/src/BookStore/Application/Command/UpdateBookCommandHandler.php b/src/BookStore/Application/Command/UpdateBookCommandHandler.php index 6b6c9c2..f64134d 100644 --- a/src/BookStore/Application/Command/UpdateBookCommandHandler.php +++ b/src/BookStore/Application/Command/UpdateBookCommandHandler.php @@ -31,8 +31,6 @@ public function __invoke(UpdateBookCommand $command): Book price: $command->price, ); - $this->bookRepository->save($book); - return $book; } } diff --git a/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php b/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php index b239aed..f139cf1 100644 --- a/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php +++ b/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php @@ -28,13 +28,11 @@ public function __construct(EntityManagerInterface $em) public function save(Book $book): void { $this->em->persist($book); - $this->em->flush(); } public function remove(Book $book): void { $this->em->remove($book); - $this->em->flush(); } public function ofId(BookId $id): ?Book From 4a3ab51b2d166bd25504b2b830a0e9d032aaef5a Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Wed, 27 Dec 2023 11:32:42 -0500 Subject: [PATCH 2/3] Use collection-oriented repository pattern and fixing tests --- config/packages/doctrine.php | 1 + config/services/test/book_store.php | 4 ++ .../Command/CreateBookCommandHandler.php | 2 +- .../Repository/BookRepositoryInterface.php | 2 +- .../Doctrine/DoctrineBookRepository.php | 2 +- .../InMemory/InMemoryBookRepository.php | 2 +- .../Symfony/Messenger/NullMiddleware.php | 17 +++++++ .../Acceptance/AnonymizeBooksTest.php | 2 +- tests/BookStore/Acceptance/BookCrudTest.php | 16 +++---- .../Acceptance/CheapestBooksTest.php | 4 +- .../BookStore/Acceptance/DiscountBookTest.php | 4 +- .../Functional/AnonymizeBooksTest.php | 2 +- tests/BookStore/Functional/DeleteBookTest.php | 2 +- .../BookStore/Functional/DiscountBookTest.php | 2 +- tests/BookStore/Functional/FindBookTest.php | 2 +- tests/BookStore/Functional/FindBooksTest.php | 10 ++-- .../Functional/FindCheapestBooksTest.php | 4 +- tests/BookStore/Functional/UpdateBookTest.php | 2 +- .../Doctrine/DoctrineBookRepositoryTest.php | 47 +++++++++++-------- .../InMemory/InMemoryBookRepositoryTest.php | 24 +++++----- 20 files changed, 90 insertions(+), 61 deletions(-) create mode 100644 src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php diff --git a/config/packages/doctrine.php b/config/packages/doctrine.php index dd89d43..d31c353 100644 --- a/config/packages/doctrine.php +++ b/config/packages/doctrine.php @@ -10,6 +10,7 @@ [ 'dbal' => [ 'url' => '%env(resolve:DATABASE_URL)%', + 'use_savepoints' => true, ], 'orm' => [ 'report_fields_where_declared' => true, diff --git a/config/services/test/book_store.php b/config/services/test/book_store.php index 278f181..776d2bf 100644 --- a/config/services/test/book_store.php +++ b/config/services/test/book_store.php @@ -5,6 +5,7 @@ use App\BookStore\Domain\Repository\BookRepositoryInterface; use App\BookStore\Infrastructure\Doctrine\DoctrineBookRepository; use App\BookStore\Infrastructure\InMemory\InMemoryBookRepository; +use App\Shared\Infrastructure\Symfony\Messenger\NullMiddleware; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { @@ -23,4 +24,7 @@ $services->set(DoctrineBookRepository::class) ->public(); + + $services->set('messenger.middleware.doctrine_transaction') + ->class(NullMiddleware::class); }; diff --git a/src/BookStore/Application/Command/CreateBookCommandHandler.php b/src/BookStore/Application/Command/CreateBookCommandHandler.php index a49b4a5..872df1b 100644 --- a/src/BookStore/Application/Command/CreateBookCommandHandler.php +++ b/src/BookStore/Application/Command/CreateBookCommandHandler.php @@ -25,7 +25,7 @@ public function __invoke(CreateBookCommand $command): Book $command->price, ); - $this->bookRepository->save($book); + $this->bookRepository->add($book); return $book; } diff --git a/src/BookStore/Domain/Repository/BookRepositoryInterface.php b/src/BookStore/Domain/Repository/BookRepositoryInterface.php index ad53742..d71215d 100644 --- a/src/BookStore/Domain/Repository/BookRepositoryInterface.php +++ b/src/BookStore/Domain/Repository/BookRepositoryInterface.php @@ -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; diff --git a/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php b/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php index f139cf1..2ff47b5 100644 --- a/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php +++ b/src/BookStore/Infrastructure/Doctrine/DoctrineBookRepository.php @@ -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); } diff --git a/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php b/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php index c3aadfc..9afacd5 100644 --- a/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php +++ b/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php @@ -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; } diff --git a/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php b/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php new file mode 100644 index 0000000..1fb6bb4 --- /dev/null +++ b/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php @@ -0,0 +1,17 @@ +next()->handle($envelope, $stack); + } +} diff --git a/tests/BookStore/Acceptance/AnonymizeBooksTest.php b/tests/BookStore/Acceptance/AnonymizeBooksTest.php index 899f581..05d56fb 100644 --- a/tests/BookStore/Acceptance/AnonymizeBooksTest.php +++ b/tests/BookStore/Acceptance/AnonymizeBooksTest.php @@ -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', [ diff --git a/tests/BookStore/Acceptance/BookCrudTest.php b/tests/BookStore/Acceptance/BookCrudTest.php index 75c8eff..f6b6606 100644 --- a/tests/BookStore/Acceptance/BookCrudTest.php +++ b/tests/BookStore/Acceptance/BookCrudTest.php @@ -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'); @@ -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'); @@ -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())); @@ -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' => [ @@ -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' => [ @@ -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())); diff --git a/tests/BookStore/Acceptance/CheapestBooksTest.php b/tests/BookStore/Acceptance/CheapestBooksTest.php index 07f22ff..40e7858 100644 --- a/tests/BookStore/Acceptance/CheapestBooksTest.php +++ b/tests/BookStore/Acceptance/CheapestBooksTest.php @@ -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'); @@ -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'); diff --git a/tests/BookStore/Acceptance/DiscountBookTest.php b/tests/BookStore/Acceptance/DiscountBookTest.php index 31bc118..2df1f21 100644 --- a/tests/BookStore/Acceptance/DiscountBookTest.php +++ b/tests/BookStore/Acceptance/DiscountBookTest.php @@ -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' => [ @@ -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' => [ diff --git a/tests/BookStore/Functional/AnonymizeBooksTest.php b/tests/BookStore/Functional/AnonymizeBooksTest.php index 1ec2d8f..67181ee 100644 --- a/tests/BookStore/Functional/AnonymizeBooksTest.php +++ b/tests/BookStore/Functional/AnonymizeBooksTest.php @@ -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.')); diff --git a/tests/BookStore/Functional/DeleteBookTest.php b/tests/BookStore/Functional/DeleteBookTest.php index ac9cfa6..c62fba7 100644 --- a/tests/BookStore/Functional/DeleteBookTest.php +++ b/tests/BookStore/Functional/DeleteBookTest.php @@ -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); diff --git a/tests/BookStore/Functional/DiscountBookTest.php b/tests/BookStore/Functional/DiscountBookTest.php index e74c77a..ec0ae48 100644 --- a/tests/BookStore/Functional/DiscountBookTest.php +++ b/tests/BookStore/Functional/DiscountBookTest.php @@ -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))); diff --git a/tests/BookStore/Functional/FindBookTest.php b/tests/BookStore/Functional/FindBookTest.php index 310935d..3529509 100644 --- a/tests/BookStore/Functional/FindBookTest.php +++ b/tests/BookStore/Functional/FindBookTest.php @@ -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()))); } diff --git a/tests/BookStore/Functional/FindBooksTest.php b/tests/BookStore/Functional/FindBooksTest.php index 0547760..7b3e87a 100644 --- a/tests/BookStore/Functional/FindBooksTest.php +++ b/tests/BookStore/Functional/FindBooksTest.php @@ -30,7 +30,7 @@ public function testFindBooks(): void ]; foreach ($initialBooks as $book) { - $bookRepository->save($book); + $bookRepository->add($book); } $books = $queryBus->ask(new FindBooksQuery()); @@ -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); @@ -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)); diff --git a/tests/BookStore/Functional/FindCheapestBooksTest.php b/tests/BookStore/Functional/FindCheapestBooksTest.php index fc54f01..44bda1f 100644 --- a/tests/BookStore/Functional/FindCheapestBooksTest.php +++ b/tests/BookStore/Functional/FindCheapestBooksTest.php @@ -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)); @@ -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)); diff --git a/tests/BookStore/Functional/UpdateBookTest.php b/tests/BookStore/Functional/UpdateBookTest.php index f3fac6a..f334a03 100644 --- a/tests/BookStore/Functional/UpdateBookTest.php +++ b/tests/BookStore/Functional/UpdateBookTest.php @@ -33,7 +33,7 @@ public function testUpdateBook(): void price: 1000, ); - $bookRepository->save($initialBook); + $bookRepository->add($initialBook); $commandBus->dispatch(new UpdateBookCommand( $initialBook->id(), diff --git a/tests/BookStore/Integration/Doctrine/DoctrineBookRepositoryTest.php b/tests/BookStore/Integration/Doctrine/DoctrineBookRepositoryTest.php index 8b96a10..6cabd29 100644 --- a/tests/BookStore/Integration/Doctrine/DoctrineBookRepositoryTest.php +++ b/tests/BookStore/Integration/Doctrine/DoctrineBookRepositoryTest.php @@ -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; @@ -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') @@ -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 @@ -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); } @@ -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); } @@ -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())); } @@ -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'))); @@ -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) { @@ -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) { @@ -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); @@ -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)); diff --git a/tests/BookStore/Integration/InMemory/InMemoryBookRepositoryTest.php b/tests/BookStore/Integration/InMemory/InMemoryBookRepositoryTest.php index 8ce1382..25b760c 100644 --- a/tests/BookStore/Integration/InMemory/InMemoryBookRepositoryTest.php +++ b/tests/BookStore/Integration/InMemory/InMemoryBookRepositoryTest.php @@ -20,7 +20,7 @@ public function testAdd(): void static::assertEmpty($repository); $book = DummyBookFactory::createBook(); - $repository->save($book); + $repository->add($book); static::assertCount(1, $repository); } @@ -31,7 +31,7 @@ public function testRemove(): void $repository = static::getContainer()->get(InMemoryBookRepository::class); $book = DummyBookFactory::createBook(); - $repository->save($book); + $repository->add($book); static::assertCount(1, $repository); @@ -47,7 +47,7 @@ public function testOfId(): void static::assertEmpty($repository); $book = DummyBookFactory::createBook(); - $repository->save($book); + $repository->add($book); static::assertSame($book, $repository->ofId($book->id())); } @@ -57,9 +57,9 @@ public function testWithAuthor(): void /** @var InMemoryBookRepository $repository */ $repository = static::getContainer()->get(InMemoryBookRepository::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')); static::assertCount(2, $repository->withAuthor(new Author('authorOne'))); static::assertCount(1, $repository->withAuthor(new Author('authorTwo'))); @@ -70,9 +70,9 @@ public function testWithCheapestsFirst(): void /** @var InMemoryBookRepository $repository */ $repository = static::getContainer()->get(InMemoryBookRepository::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)); $prices = []; foreach ($repository->withCheapestsFirst() as $book) { @@ -115,7 +115,7 @@ public function testIteratorWithoutPagination(): void DummyBookFactory::createBook(), ]; foreach ($books as $book) { - $repository->save($book); + $repository->add($book); } $i = 0; @@ -137,7 +137,7 @@ public function testIteratorWithPagination(): void DummyBookFactory::createBook(), ]; foreach ($books as $book) { - $repository->save($book); + $repository->add($book); } $repository = $repository->withPagination(1, 2); @@ -172,7 +172,7 @@ public function testCount(): void DummyBookFactory::createBook(), ]; foreach ($books as $book) { - $repository->save($book); + $repository->add($book); } static::assertCount(count($books), $repository); From 96a6132b48b229c4c4ad52928f669e2d0a4ccc6b Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Thu, 28 Dec 2023 09:23:27 -0500 Subject: [PATCH 3/3] Update messenger config conditionally --- config/packages/doctrine.php | 2 +- config/packages/messenger.php | 2 +- config/services/test/book_store.php | 4 ---- .../Symfony/Messenger/NullMiddleware.php | 17 ----------------- 4 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php diff --git a/config/packages/doctrine.php b/config/packages/doctrine.php index d31c353..a11d32a 100644 --- a/config/packages/doctrine.php +++ b/config/packages/doctrine.php @@ -10,7 +10,7 @@ [ 'dbal' => [ 'url' => '%env(resolve:DATABASE_URL)%', - 'use_savepoints' => true, + 'use_savepoints' => true, // Needed for nested transactions ], 'orm' => [ 'report_fields_where_declared' => true, diff --git a/config/packages/messenger.php b/config/packages/messenger.php index d32f87f..8e337ba 100644 --- a/config/packages/messenger.php +++ b/config/packages/messenger.php @@ -11,7 +11,7 @@ 'messenger' => [ 'default_bus' => 'command.bus', 'buses' => [ - 'command.bus' => [ + 'command.bus' => 'test' === $containerConfigurator->env() ? [] : [ 'middleware' => [ 'messenger.middleware.doctrine_transaction', ], diff --git a/config/services/test/book_store.php b/config/services/test/book_store.php index 776d2bf..278f181 100644 --- a/config/services/test/book_store.php +++ b/config/services/test/book_store.php @@ -5,7 +5,6 @@ use App\BookStore\Domain\Repository\BookRepositoryInterface; use App\BookStore\Infrastructure\Doctrine\DoctrineBookRepository; use App\BookStore\Infrastructure\InMemory\InMemoryBookRepository; -use App\Shared\Infrastructure\Symfony\Messenger\NullMiddleware; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { @@ -24,7 +23,4 @@ $services->set(DoctrineBookRepository::class) ->public(); - - $services->set('messenger.middleware.doctrine_transaction') - ->class(NullMiddleware::class); }; diff --git a/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php b/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php deleted file mode 100644 index 1fb6bb4..0000000 --- a/src/Shared/Infrastructure/Symfony/Messenger/NullMiddleware.php +++ /dev/null @@ -1,17 +0,0 @@ -next()->handle($envelope, $stack); - } -}