Skip to content

Commit

Permalink
fix DoctrineSubscriber + tests
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Andreyev <[email protected]>
  • Loading branch information
oleg-andreyev committed May 17, 2024
1 parent 96169f1 commit 6498dd2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 74 deletions.
15 changes: 6 additions & 9 deletions src/Provider/Doctrine/Auditing/Event/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Closure;
use DH\Auditor\Provider\Doctrine\Auditing\Logger\Middleware\DHDriver;
use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionManager;
use DH\Auditor\Provider\Doctrine\Model\Transaction;
use DH\Auditor\Transaction\TransactionManagerInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
Expand All @@ -20,12 +20,7 @@ final class DoctrineSubscriber implements EventSubscriber
/** @var Transaction[] */
private array $transactions = [];

private TransactionManager $transactionManager;

public function __construct(TransactionManager $transactionManager)
{
$this->transactionManager = $transactionManager;
}
public function __construct(private readonly TransactionManagerInterface $transactionManager) {}

/**
* It is called inside EntityManager#flush() after the changes to all the managed entities
Expand Down Expand Up @@ -83,7 +78,7 @@ public function getWrappedDriver(Driver $driver): Closure|Driver
}, $driver, AbstractDriverMiddleware::class)();
}

return Closure::bind(function () use ($that) {
return Closure::bind(function () use ($that): Closure|Driver|null {
/** @var Driver $this */
$properties = (new ReflectionClass($this))->getProperties();
foreach ($properties as $property) {
Expand All @@ -93,6 +88,8 @@ public function getWrappedDriver(Driver $driver): Closure|Driver
return $that->getWrappedDriver($value);
}
}
}, $driver, Driver::class)();

return null;
}, $driver, Driver::class)() ?: $driver;
}
}
105 changes: 40 additions & 65 deletions tests/Provider/Doctrine/Event/DoctrineSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
namespace DH\Auditor\Tests\Provider\Doctrine\Event;

use DH\Auditor\Provider\Doctrine\Auditing\Event\DoctrineSubscriber;
use DH\Auditor\Provider\Doctrine\Auditing\Logger\LoggerChain;
use DH\Auditor\Provider\Doctrine\Auditing\Logger\Middleware\DHDriver;
use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionManager;
use DH\Auditor\Transaction\TransactionManagerInterface;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection as ConnectionDbal;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use PHPUnit\Framework\TestCase;
use RuntimeException;

/**
* @internal
Expand All @@ -27,48 +26,20 @@
*/
final class DoctrineSubscriberTest extends TestCase
{
public function testIssue185(): void
{
$transactionManager = $this->createMock(TransactionManager::class);
$objectManager = $this->createMock(EntityManagerInterface::class);

$args = new OnFlushEventArgs($objectManager);

$objectManager
->method('getConnection')
->willReturn($connection = $this->createMock(ConnectionDbal::class))
;

$connection
->method('getDriver')
->willReturn($driver = $this->createMock(Driver::class))
;

$connection
->method('getConfiguration')
->willReturn($configuration = new Configuration())
;

$configuration->setSQLLogger(new class() implements SQLLogger {
public function startQuery($sql, ?array $params = null, ?array $types = null): void {}

public function stopQuery(): void {}
});

$target = new DoctrineSubscriber($transactionManager);
$target->onFlush($args);
$target->onFlush($args);
$target->onFlush($args);
$target->onFlush($args);
$target->onFlush($args);

$result = $configuration->getSQLLogger();
self::assertCount(2, $result->getLoggers());
}

public function testIssue184IfAbstractDriverMiddleware(): void
{
$transactionManager = $this->createMock(TransactionManager::class);
$transactionManager = new class() implements TransactionManagerInterface {
public function populate($transaction): void {}

public function process($transaction): void
{
static $i = 0;
++$i;
if ($i > 1) {
throw new RuntimeException('Expected only once');
}
}
};
$objectManager = $this->createMock(EntityManagerInterface::class);

$args = new OnFlushEventArgs($objectManager);
Expand All @@ -90,19 +61,27 @@ public function testIssue184IfAbstractDriverMiddleware(): void
$target = new DoctrineSubscriber($transactionManager);
$target->onFlush($args);

$transactionManager
->expects(self::once())
->method('process')
;

foreach ($dhDriver->getFlusherList() as $item) {
($item)();
}

self::assertTrue(true);
}

public function testIssue184IfNotAbstractDriverMiddleware(): void
{
$transactionManager = $this->createMock(TransactionManager::class);
$transactionManager = new class() implements TransactionManagerInterface {
public function populate($transaction): void {}

public function process($transaction): void
{
static $i = 0;
++$i;
if ($i > 1) {
throw new RuntimeException('Expected only once');
}
}
};
$objectManager = $this->createMock(EntityManagerInterface::class);

$args = new OnFlushEventArgs($objectManager);
Expand Down Expand Up @@ -145,19 +124,23 @@ public function createDatabasePlatformForVersion($version): void {}
$target = new DoctrineSubscriber($transactionManager);
$target->onFlush($args);

$transactionManager
->expects(self::once())
->method('process')
;

foreach ($dhDriver->getFlusherList() as $item) {
($item)();
}

self::assertTrue(true);
}

public function testIssue184Unexpected(): void
{
$transactionManager = $this->createMock(TransactionManager::class);
$transactionManager = new class() implements TransactionManagerInterface {
public function populate($transaction): void {}

public function process($transaction): void
{
throw new RuntimeException('Unexpected call');
}
};
$objectManager = $this->createMock(EntityManagerInterface::class);

$args = new OnFlushEventArgs($objectManager);
Expand Down Expand Up @@ -192,17 +175,9 @@ public function createDatabasePlatformForVersion($version): void {}
->willReturn($configuration = $this->createMock(Configuration::class))
;

$transactionManager
->expects(self::never())
->method('process')
;

$configuration->expects(self::once())
->method('setSQLLogger')
->with(self::isInstanceOf(LoggerChain::class))
;

$target = new DoctrineSubscriber($transactionManager);
$target->onFlush($args);

self::assertTrue(true);
}
}

0 comments on commit 6498dd2

Please sign in to comment.