mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Migrated CloseDbConnectionEventListenerTest to use PHPUnit mocks
This commit is contained in:
parent
3608a6d068
commit
cad53e397a
@ -5,9 +5,8 @@ declare(strict_types=1);
|
||||
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use RuntimeException;
|
||||
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\EventDispatcher\CloseDbConnectionEventListener;
|
||||
@ -16,13 +15,11 @@ use Throwable;
|
||||
|
||||
class CloseDbConnectionEventListenerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private ObjectProphecy $em;
|
||||
private MockObject $em;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->em = $this->prophesize(ReopeningEntityManagerInterface::class);
|
||||
$this->em = $this->createMock(ReopeningEntityManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,16 +28,14 @@ class CloseDbConnectionEventListenerTest extends TestCase
|
||||
*/
|
||||
public function connectionIsOpenedBeforeAndClosedAfter(callable $wrapped, bool &$wrappedWasCalled): void
|
||||
{
|
||||
$conn = $this->prophesize(Connection::class);
|
||||
$close = $conn->close()->will(function (): void {
|
||||
});
|
||||
$getConn = $this->em->getConnection()->willReturn($conn->reveal());
|
||||
$close = $this->em->close()->will(function (): void {
|
||||
});
|
||||
$open = $this->em->open()->will(function (): void {
|
||||
});
|
||||
$conn = $this->createMock(Connection::class);
|
||||
$conn->expects($this->once())->method('close');
|
||||
|
||||
$eventListener = new CloseDbConnectionEventListener($this->em->reveal(), $wrapped);
|
||||
$this->em->expects($this->once())->method('getConnection')->willReturn($conn);
|
||||
$this->em->expects($this->once())->method('close');
|
||||
$this->em->expects($this->once())->method('open');
|
||||
|
||||
$eventListener = new CloseDbConnectionEventListener($this->em, $wrapped);
|
||||
|
||||
try {
|
||||
($eventListener)(new stdClass());
|
||||
@ -49,25 +44,21 @@ class CloseDbConnectionEventListenerTest extends TestCase
|
||||
}
|
||||
|
||||
self::assertTrue($wrappedWasCalled);
|
||||
$close->shouldHaveBeenCalledOnce();
|
||||
$getConn->shouldHaveBeenCalledOnce();
|
||||
$close->shouldHaveBeenCalledOnce();
|
||||
$open->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideWrapped(): iterable
|
||||
{
|
||||
yield 'does not throw exception' => (function (): array {
|
||||
yield 'does not throw exception' => (static function (): array {
|
||||
$wrappedWasCalled = false;
|
||||
$wrapped = function () use (&$wrappedWasCalled): void {
|
||||
$wrapped = static function () use (&$wrappedWasCalled): void {
|
||||
$wrappedWasCalled = true;
|
||||
};
|
||||
|
||||
return [$wrapped, &$wrappedWasCalled];
|
||||
})();
|
||||
yield 'throws exception' => (function (): array {
|
||||
yield 'throws exception' => (static function (): array {
|
||||
$wrappedWasCalled = false;
|
||||
$wrapped = function () use (&$wrappedWasCalled): void {
|
||||
$wrapped = static function () use (&$wrappedWasCalled): void {
|
||||
$wrappedWasCalled = true;
|
||||
throw new RuntimeException('Some error');
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user