From cad53e397a20851fc2d6210fa3f1ba159a9aee0c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Oct 2022 07:32:37 +0200 Subject: [PATCH] Migrated CloseDbConnectionEventListenerTest to use PHPUnit mocks --- .../CloseDbConnectionEventListenerTest.php | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/module/Core/test/EventDispatcher/CloseDbConnectionEventListenerTest.php b/module/Core/test/EventDispatcher/CloseDbConnectionEventListenerTest.php index 7c4d74c8..76b433e1 100644 --- a/module/Core/test/EventDispatcher/CloseDbConnectionEventListenerTest.php +++ b/module/Core/test/EventDispatcher/CloseDbConnectionEventListenerTest.php @@ -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'); };