diff --git a/.gitignore b/.gitignore index cc77e3f..0d42bb7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ Thumbs.db /vendor/ composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index ebe76bc..f743517 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,8 @@ "symfony/messenger": "^4.4" }, "require-dev": { - "pccomponentes/coding-standard": "^1.0" + "pccomponentes/coding-standard": "^1.0", + "phpunit/phpunit": "^8.5", + "dg/bypass-finals": "^1.1" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0418ea8 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,27 @@ + + + + + + + + + + + + + tests + + + + + + src + + + diff --git a/src/Driver/TransactionalConnection.php b/src/Driver/TransactionalConnection.php index be81910..268d20d 100644 --- a/src/Driver/TransactionalConnection.php +++ b/src/Driver/TransactionalConnection.php @@ -5,7 +5,7 @@ interface TransactionalConnection { - public function beginTransaction(): bool; - public function commit(): bool; - public function rollBack(): bool; + public function beginTransaction(): void; + public function commit(): void; + public function rollBack(): void; } diff --git a/src/Driver/TransactionalConnections.php b/src/Driver/TransactionalConnections.php deleted file mode 100644 index 7ac919a..0000000 --- a/src/Driver/TransactionalConnections.php +++ /dev/null @@ -1,47 +0,0 @@ -connections = $connections; - } - - public function current(): ?TransactionalConnection - { - $connection = \current($this->connections); - if (false === $connection) { - return null; - } - - return $connection; - } - - public function next() - { - \next($this->connections); - } - - public function key() - { - return \key($this->connections); - } - - public function valid(): bool - { - return \array_key_exists( - $this->key(), - $this->connections - ); - } - - public function rewind() - { - \reset($this->connections); - } -} diff --git a/src/SymfonyMessenger/TransactionMiddleware.php b/src/SymfonyMessenger/TransactionMiddleware.php index fa15974..5fdda96 100644 --- a/src/SymfonyMessenger/TransactionMiddleware.php +++ b/src/SymfonyMessenger/TransactionMiddleware.php @@ -3,52 +3,32 @@ namespace PcComponentes\Transaction\SymfonyMessenger; +use PcComponentes\Transaction\Driver\TransactionalConnection; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Middleware\StackInterface; -use PcComponentes\Transaction\Driver\TransactionalConnections; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; final class TransactionMiddleware implements MiddlewareInterface { - private TransactionalConnections $connections; + private TransactionalConnection $connection; - public function __construct(TransactionalConnections $connections) + public function __construct(TransactionalConnection $connection) { - $this->connections = $connections; + $this->connection = $connection; } + public function handle(Envelope $envelope, StackInterface $stack): Envelope { try { - $this->beginTransaction(); + $this->connection->beginTransaction(); $envelope = $stack->next()->handle($envelope, $stack); - $this->commit(); + $this->connection->commit(); return $envelope; } catch (\Throwable $exception) { - $this->rollBack(); + $this->connection->rollBack(); throw $exception; } } - - private function beginTransaction(): void - { - foreach ($this->connections as $connection) { - $connection->beginTransaction(); - } - } - - private function commit(): void - { - foreach ($this->connections as $connection) { - $connection->commit(); - } - } - - private function rollBack(): void - { - foreach ($this->connections as $connection) { - $connection->rollBack(); - } - } } diff --git a/tests/SymfonyMessenger/TransactionMiddlewareTest.php b/tests/SymfonyMessenger/TransactionMiddlewareTest.php new file mode 100644 index 0000000..33151bc --- /dev/null +++ b/tests/SymfonyMessenger/TransactionMiddlewareTest.php @@ -0,0 +1,110 @@ +createMock(TransactionalConnection::class); + + $transactionalConnection + ->expects($this->once()) + ->method('beginTransaction') + ; + $transactionalConnection + ->expects($this->once()) + ->method('commit') + ; + + $transactionMiddleware = new TransactionMiddleware($transactionalConnection); + $transactionMiddleware->handle( + $this->createMock(Envelope::class), + $this->createMock(StackInterface::class) + ); + } + + /** + * @test + */ + public function given_envelope_and_stack_with_next_middleware_when_handle_then_go_forward_to_next_middleware_and_execute_it() + { + $envelope = $this->createMock(Envelope::class); + $nextMiddleware = $this->createMock(MiddlewareInterface::class); + $stack = $this->createMock(StackInterface::class); + + $stack + ->expects($this->once()) + ->method('next') + ->willReturn($nextMiddleware) + ; + $nextMiddleware + ->expects($this->once()) + ->method('handle') + ->with($envelope, $stack) + ->willReturn($envelope) + ; + + $transactionMiddleware = new TransactionMiddleware( + $this->createMock(TransactionalConnection::class) + ); + $transactionMiddleware->handle( + $envelope, + $stack + ); + } + + /** + * @test + */ + public function given_next_middleware_throwing_exception_when_handle_then_rollback_transaction_and_throw_catch_exception() + { + $transactionalConnection = $this->createMock(TransactionalConnection::class); + $nextMiddleware = $this->createMock(MiddlewareInterface::class); + $stack = $this->createMock(StackInterface::class); + $exception = new class extends \Exception {}; + + $transactionalConnection + ->expects($this->once()) + ->method('beginTransaction') + ; + $stack + ->expects($this->once()) + ->method('next') + ->willReturn($nextMiddleware) + ; + $nextMiddleware + ->expects($this->once()) + ->method('handle') + ->willThrowException($exception) + ; + $transactionalConnection + ->expects($this->never()) + ->method('commit') + ; + $transactionalConnection + ->expects($this->once()) + ->method('rollback') + ; + $this->expectException( + \get_class($exception) + ); + + $transactionMiddleware = new TransactionMiddleware($transactionalConnection); + $transactionMiddleware->handle( + $this->createMock(Envelope::class), + $stack + ); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..6bd210f --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,6 @@ +