Skip to content

Commit

Permalink
Remove Prophecy
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Dec 18, 2022
1 parent 9d337e1 commit 4e6119b
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 115 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 16 additions & 14 deletions tests/DispatcherWithHandlerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<HandlerProvider>
* @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);
Expand All @@ -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
);
}
}
35 changes: 17 additions & 18 deletions tests/HandlerProviderWithChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
Expand Down
29 changes: 15 additions & 14 deletions tests/PSR/HandlerProviderWithContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContainerInterface>
* @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([]);

Expand All @@ -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);
Expand All @@ -84,7 +83,9 @@ public function testReturnExpectedService(): void

];

$this->container->get($expectedServiceId)
$this->container
->method('get')
->with($expectedServiceId)
->willReturn($expectedService);

$provider = $this->makeProvider($handlers);
Expand All @@ -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);
}
}
34 changes: 16 additions & 18 deletions tests/PSR/VoterProviderWithContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -33,31 +30,32 @@ final class VoterProviderWithContainerTest extends TestCase
private Voter $voterIsAdmin;

/**
* @var ObjectProphecy<ContainerInterface>
* @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
{
$this->expectException(VoterNotFound::class);
$this->expectExceptionMessage("Voter not found for permission: is_madonna");

$this->makeSTU()->getVoterForPermission('is_madonna');
$this->makeSUT()->getVoterForPermission('is_madonna');
}

/**
Expand All @@ -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());
Expand All @@ -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,
]);
Expand Down
Loading

0 comments on commit 4e6119b

Please sign in to comment.