From 4e6119bfe64c3752934cd5b7b3a89264d19d9fa2 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 18 Dec 2022 12:47:28 +0100 Subject: [PATCH] Remove Prophecy --- README.md | 2 + composer.json | 2 - tests/DispatcherWithHandlerProviderTest.php | 30 +++++------ tests/HandlerProviderWithChainTest.php | 35 +++++++------ .../PSR/HandlerProviderWithContainerTest.php | 29 +++++------ tests/PSR/VoterProviderWithContainerTest.php | 34 ++++++------- tests/RestrictedDispatcherWithVoterTest.php | 50 ++++++++++--------- tests/VoterWithPermissionsTest.php | 49 +++++++++--------- 8 files changed, 116 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index ef149b4..8062352 100644 --- a/README.md +++ b/README.md @@ -407,6 +407,8 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. +[Composer]: https://getcomposer.org/ +[composer-attribute-collector]: https://github.com/olvlvl/composer-attribute-collector/ [ICanBoogie]: https://icanboogie.org/ [JWT]: https://jwt.io/ [symfony/dependency-injection]: https://symfony.com/doc/current/components/dependency_injection.html diff --git a/composer.json b/composer.json index 280824b..f10f4e7 100644 --- a/composer.json +++ b/composer.json @@ -34,9 +34,7 @@ "php": ">=8.0.2" }, "require-dev": { - "jangregor/phpstan-prophecy": "^1.0", "olvlvl/composer-attribute-collector": "^1.0", - "phpspec/prophecy-phpunit": "^2.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^1.9", "phpunit/phpunit": "^9.5", diff --git a/tests/DispatcherWithHandlerProviderTest.php b/tests/DispatcherWithHandlerProviderTest.php index ef43768..67fd66c 100644 --- a/tests/DispatcherWithHandlerProviderTest.php +++ b/tests/DispatcherWithHandlerProviderTest.php @@ -11,37 +11,37 @@ namespace ICanBoogie\MessageBus; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use function uniqid; final class DispatcherWithHandlerProviderTest extends TestCase { - use ProphecyTrait; - private object $message; /** - * @var ObjectProphecy + * @var MockObject&HandlerProvider */ - private ObjectProphecy $handlerProvider; + private MockObject $handlerProvider; protected function setUp(): void { parent::setUp(); $this->message = (object) [ uniqid() => uniqid() ]; - $this->handlerProvider = $this->prophesize(HandlerProvider::class); + $this->handlerProvider = $this->createMock(HandlerProvider::class); } public function testFailOnMissingHandler(): void { - $this->handlerProvider->getHandlerForMessage($this->message) - ->shouldBeCalled()->willReturn(null); + $this->handlerProvider + ->expects($this->once()) + ->method('getHandlerForMessage') + ->with($this->message) + ->willReturn(null); - $stu = $this->makeSTU(); + $stu = $this->makeSUT(); $this->expectException(HandlerNotFound::class); $stu->dispatch($this->message); @@ -51,17 +51,19 @@ public function testDispatch(): void { $result = uniqid(); - $this->handlerProvider->getHandlerForMessage($this->message) + $this->handlerProvider + ->method('getHandlerForMessage') + ->with($this->message) ->willReturn(fn() => $result); - $stu = $this->makeSTU(); + $stu = $this->makeSUT(); $this->assertSame($result, $stu->dispatch($this->message)); } - private function makeSTU(): Dispatcher + private function makeSUT(): Dispatcher { return new DispatcherWithHandlerProvider( - $this->handlerProvider->reveal() + $this->handlerProvider ); } } diff --git a/tests/HandlerProviderWithChainTest.php b/tests/HandlerProviderWithChainTest.php index 4b61885..bf95897 100644 --- a/tests/HandlerProviderWithChainTest.php +++ b/tests/HandlerProviderWithChainTest.php @@ -12,12 +12,9 @@ namespace ICanBoogie\MessageBus; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; final class HandlerProviderWithChainTest extends TestCase { - use ProphecyTrait; - public function testChain(): void { $messageA = new MessageA(); @@ -30,21 +27,23 @@ public function testChain(): void $handler2 = function () { }; - $provider1 = $this->prophesize(HandlerProvider::class); - $provider1->getHandlerForMessage($messageA) - ->willReturn($handler1); - $provider1->getHandlerForMessage($messageB) - ->willReturn(null); - $provider1->getHandlerForMessage($messageC) - ->willReturn(null); - - $provider2 = $this->prophesize(HandlerProvider::class); - $provider2->getHandlerForMessage($messageB) - ->willReturn($handler2); - $provider2->getHandlerForMessage($messageC) - ->willReturn(null); - - $provider = new HandlerProviderWithChain([ $provider1->reveal(), $provider2->reveal() ]); + $provider1 = $this->createMock(HandlerProvider::class); + $provider1 + ->method('getHandlerForMessage') + ->willReturnCallback(fn(object $message) => match ($message) { + $messageA => $handler1, + default => null + }); + + $provider2 = $this->createMock(HandlerProvider::class); + $provider2 + ->method('getHandlerForMessage') + ->willReturnCallback(fn (object $message) => match ($message) { + $messageB => $handler2, + default => null + }); + + $provider = new HandlerProviderWithChain([ $provider1, $provider2 ]); $this->assertSame($handler1, $provider->getHandlerForMessage($messageA)); $this->assertSame($handler2, $provider->getHandlerForMessage($messageB)); diff --git a/tests/PSR/HandlerProviderWithContainerTest.php b/tests/PSR/HandlerProviderWithContainerTest.php index 91e7fbf..28aeeef 100644 --- a/tests/PSR/HandlerProviderWithContainerTest.php +++ b/tests/PSR/HandlerProviderWithContainerTest.php @@ -15,33 +15,30 @@ use ICanBoogie\MessageBus\HandlerProvider; use ICanBoogie\MessageBus\MessageA; use ICanBoogie\MessageBus\MessageB; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; final class HandlerProviderWithContainerTest extends TestCase { - use ProphecyTrait; - /** - * @var ObjectProphecy + * @var MockObject&ContainerInterface */ - private ObjectProphecy $container; + private MockObject $container; protected function setUp(): void { - $this->container = $this->prophesize(ContainerInterface::class); + $this->container = $this->createMock(ContainerInterface::class); parent::setUp(); } public function testFailOnUndefinedHandler(): void { - $this->container->get(Argument::any()) - ->shouldNotBeCalled(); + $this->container + ->expects($this->never()) + ->method('get'); $provider = $this->makeProvider([]); @@ -60,8 +57,10 @@ public function testFailOnUndefinedService(): void ]; - $this->container->get($undefinedService) - ->willThrow(new class extends Exception implements NotFoundExceptionInterface { + $this->container + ->method('get') + ->with($undefinedService) + ->willThrowException(new class extends Exception implements NotFoundExceptionInterface { }); $provider = $this->makeProvider($handlers); @@ -84,7 +83,9 @@ public function testReturnExpectedService(): void ]; - $this->container->get($expectedServiceId) + $this->container + ->method('get') + ->with($expectedServiceId) ->willReturn($expectedService); $provider = $this->makeProvider($handlers); @@ -98,6 +99,6 @@ public function testReturnExpectedService(): void */ private function makeProvider(array $messageToHandler): HandlerProvider { - return new HandlerProviderWithContainer($this->container->reveal(), $messageToHandler); + return new HandlerProviderWithContainer($this->container, $messageToHandler); } } diff --git a/tests/PSR/VoterProviderWithContainerTest.php b/tests/PSR/VoterProviderWithContainerTest.php index 1b33950..1ab68c3 100644 --- a/tests/PSR/VoterProviderWithContainerTest.php +++ b/tests/PSR/VoterProviderWithContainerTest.php @@ -15,16 +15,13 @@ use ICanBoogie\MessageBus\VoterNotFound; use ICanBoogie\MessageBus\VoterProvider; use LogicException; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerInterface; use Throwable; final class VoterProviderWithContainerTest extends TestCase { - use ProphecyTrait; - private const PERMISSION_IS_ADMIN = 'is_admin'; private const PERMISSION_CAN_CREATE_MENU = 'can_create_menu'; private const VOTER_IS_ADMIN_CLASS = 'Acme\\Presentation\\Security\\Voters\\IsAdmin'; @@ -33,23 +30,24 @@ final class VoterProviderWithContainerTest extends TestCase private Voter $voterIsAdmin; /** - * @var ObjectProphecy + * @var MockObject&ContainerInterface */ - private ObjectProphecy $container; + private MockObject $container; private Throwable $containerException; protected function setUp(): void { parent::setUp(); - $this->voterIsAdmin = $this->prophesize(Voter::class)->reveal(); - $this->container = $this->prophesize(ContainerInterface::class); - $this->container - ->get(self::VOTER_IS_ADMIN_CLASS) - ->willReturn($this->voterIsAdmin); + $this->voterIsAdmin = $this->createMock(Voter::class); + $this->container = $this->createMock(ContainerInterface::class); + $this->containerException = new LogicException(); $this->container - ->get(self::VOTER_CAN_CREATE_MENU_CLASS) - ->willThrow($this->containerException = new LogicException()); + ->method('get') + ->willReturnCallback(fn (string $permission) => match ($permission) { + self::VOTER_IS_ADMIN_CLASS => $this->voterIsAdmin, + default => throw $this->containerException, + }); } public function testFailureOnMissingServiceId(): void @@ -57,7 +55,7 @@ public function testFailureOnMissingServiceId(): void $this->expectException(VoterNotFound::class); $this->expectExceptionMessage("Voter not found for permission: is_madonna"); - $this->makeSTU()->getVoterForPermission('is_madonna'); + $this->makeSUT()->getVoterForPermission('is_madonna'); } /** @@ -66,7 +64,7 @@ public function testFailureOnMissingServiceId(): void public function testFailureOnMissingService(): void { try { - $this->makeSTU()->getVoterForPermission(self::PERMISSION_CAN_CREATE_MENU); + $this->makeSUT()->getVoterForPermission(self::PERMISSION_CAN_CREATE_MENU); $this->fail("Expected exception"); } catch (Throwable $e) { $this->assertSame($this->containerException, $e->getPrevious()); @@ -82,13 +80,13 @@ public function testGetVoterForPermission(): void { $this->assertSame( $this->voterIsAdmin, - $this->makeSTU()->getVoterForPermission(self::PERMISSION_IS_ADMIN) + $this->makeSUT()->getVoterForPermission(self::PERMISSION_IS_ADMIN) ); } - private function makeSTU(): VoterProvider + private function makeSUT(): VoterProvider { - return new VoterProviderWithContainer($this->container->reveal(), [ + return new VoterProviderWithContainer($this->container, [ self::PERMISSION_IS_ADMIN => self::VOTER_IS_ADMIN_CLASS, self::PERMISSION_CAN_CREATE_MENU => self::VOTER_CAN_CREATE_MENU_CLASS, ]); diff --git a/tests/RestrictedDispatcherWithVoterTest.php b/tests/RestrictedDispatcherWithVoterTest.php index f674d10..90a7212 100644 --- a/tests/RestrictedDispatcherWithVoterTest.php +++ b/tests/RestrictedDispatcherWithVoterTest.php @@ -11,26 +11,22 @@ namespace ICanBoogie\MessageBus; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use function uniqid; final class RestrictedDispatcherWithVoterTest extends TestCase { - use ProphecyTrait; - /** - * @var ObjectProphecy + * @var MockObject&Dispatcher */ - private ObjectProphecy $innerDispatcher; + private MockObject $innerDispatcher; /** - * @var ObjectProphecy + * @var MockObject&Voter */ - private ObjectProphecy $voter; + private MockObject $voter; private Context $context; private object $message; @@ -38,8 +34,8 @@ protected function setUp(): void { parent::setUp(); - $this->innerDispatcher = $this->prophesize(Dispatcher::class); - $this->voter = $this->prophesize(Voter::class); + $this->innerDispatcher = $this->createMock(Dispatcher::class); + $this->voter = $this->createMock(Voter::class); $this->context = new Context(); $this->message = new class () { }; @@ -47,38 +43,44 @@ protected function setUp(): void public function testVoterFalse(): void { - $this->voter->isGranted($this->message, $this->context) + $this->voter + ->method('isGranted') + ->with($this->message, $this->context) ->willReturn(false); - $this->innerDispatcher->dispatch(Argument::any()) - ->shouldNotBeCalled(); + $this->innerDispatcher + ->expects($this->never()) + ->method('dispatch') + ->with($this->any()); $this->expectException(PermissionNotGranted::class); - $this->makeSTU()->dispatch($this->message, $this->context); + $this->makeSUT()->dispatch($this->message, $this->context); } public function testVoterTrue(): void { $result = uniqid(); - $this->voter->isGranted($this->message, $this->context) - ->willReturn(false); - $this->innerDispatcher->dispatch($this->message) + $this->voter + ->method('isGranted') + ->with($this->message, $this->context) + ->willReturn(true); + $this->innerDispatcher + ->method('dispatch') + ->with($this->message) ->willReturn($result); - $this->expectException(PermissionNotGranted::class); - $this->assertSame( $result, - $this->makeSTU()->dispatch($this->message, $this->context) + $this->makeSUT()->dispatch($this->message, $this->context) ); } - private function makeSTU(): RestrictedDispatcher + private function makeSUT(): RestrictedDispatcher { return new RestrictedDispatcherWithVoter( - $this->innerDispatcher->reveal(), - $this->voter->reveal(), + $this->innerDispatcher, + $this->voter, ); } } diff --git a/tests/VoterWithPermissionsTest.php b/tests/VoterWithPermissionsTest.php index 5a451b3..7b5dc5d 100644 --- a/tests/VoterWithPermissionsTest.php +++ b/tests/VoterWithPermissionsTest.php @@ -11,18 +11,15 @@ namespace ICanBoogie\MessageBus; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; final class VoterWithPermissionsTest extends TestCase { - use ProphecyTrait; - /** - * @var ObjectProphecy + * @var MockObject&VoterProvider */ - private ObjectProphecy $voters; + private MockObject $voters; private Context $context; private object $message1; private object $message2; @@ -32,7 +29,7 @@ protected function setUp(): void { parent::setUp(); - $this->voters = $this->prophesize(VoterProvider::class); + $this->voters = $this->createMock(VoterProvider::class); $this->context = new Context(); $this->message1 = new class () { }; @@ -44,49 +41,51 @@ protected function setUp(): void public function testNoPermission(): void { - $this->assertTrue($this->makeSTU()->isGranted($this->message1, $this->context)); + $this->assertTrue($this->makeSUT()->isGranted($this->message1, $this->context)); } public function testNoVoter(): void { - $this->assertFalse($this->makeSTU()->isGranted($this->message2, $this->context)); + $this->assertFalse($this->makeSUT()->isGranted($this->message2, $this->context)); } public function testVoterFalse(): void { - $voter = $this->prophesize(Voter::class); - $voter->isGranted($this->message2, $this->context) - ->shouldBeCalled() + $voter = $this->createMock(Voter::class); + $voter + ->method('isGranted') + ->with($this->message2, $this->context) ->willReturn(false); $this->voters - ->getVoterForPermission('perm1') + ->method('getVoterForPermission') + ->with('perm1') ->willReturn($voter); - $this->assertFalse($this->makeSTU()->isGranted($this->message2, $this->context)); + $this->assertFalse($this->makeSUT()->isGranted($this->message2, $this->context)); } public function testVoterTrue(): void { - $voter = $this->prophesize(Voter::class); - $voter->isGranted($this->message3, $this->context) - ->shouldBeCalled() + $voter = $this->createMock(Voter::class); + $voter + ->expects($this->once()) + ->method('isGranted') + ->with($this->message3, $this->context) ->willReturn(true); $this->voters - ->getVoterForPermission('perm1') + ->expects($this->once()) + ->method('getVoterForPermission') + ->with('perm1') ->willReturn($voter); - $this->voters - ->getVoterForPermission('perm2') - ->shouldNotBeCalled(); - - $this->assertTrue($this->makeSTU()->isGranted($this->message3, $this->context)); + $this->assertTrue($this->makeSUT()->isGranted($this->message3, $this->context)); } - private function makeSTU(): Voter + private function makeSUT(): Voter { - return new VoterWithPermissions($this->voters->reveal(), [ + return new VoterWithPermissions($this->voters, [ $this->message2::class => [ 'perm1' ], $this->message3::class => [ 'perm1', 'perm2' ], ]);