Skip to content

Commit

Permalink
Generalized use and covered with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiohm committed Feb 17, 2020
1 parent f8fe54d commit 9ded1ad
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Thumbs.db

/vendor/
composer.lock
.phpunit.result.cache
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
6 changes: 3 additions & 3 deletions src/Driver/TransactionalConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
47 changes: 0 additions & 47 deletions src/Driver/TransactionalConnections.php

This file was deleted.

36 changes: 8 additions & 28 deletions src/SymfonyMessenger/TransactionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
110 changes: 110 additions & 0 deletions tests/SymfonyMessenger/TransactionMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);

namespace PcComponentes\Transaction\Tests\SymfonyMessenger;

use PcComponentes\Transaction\Driver\TransactionalConnection;
use PcComponentes\Transaction\SymfonyMessenger\TransactionMiddleware;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

final class TransactionMiddlewareTest extends TestCase
{
/**
* @test
*/
public function given_transactional_connection_when_handle_then_begin_transaction_and_commit()
{
$transactionalConnection = $this->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
);
}
}
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
declare(strict_types=1);

require dirname(__DIR__).'/vendor/autoload.php';

\DG\BypassFinals::enable();

0 comments on commit 9ded1ad

Please sign in to comment.