generated from spiral-packages/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0da5e5
commit 0a418c2
Showing
11 changed files
with
335 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App\Command; | ||
|
||
use Spiral\Cqrs\CommandInterface; | ||
|
||
final class StoreUser implements CommandInterface | ||
{ | ||
public function __construct( | ||
public string $uuid, | ||
public string $username, | ||
public string $password | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App; | ||
|
||
interface EntityManagerInterface | ||
{ | ||
public function store(array $data): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App\Handler; | ||
|
||
use Spiral\Cqrs\Tests\App\Command\StoreUser; | ||
use Spiral\Cqrs\Tests\App\EntityManagerInterface; | ||
|
||
final class StoreUserHandler | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $entityManager | ||
) { | ||
|
||
} | ||
|
||
#[\Spiral\Cqrs\Attribute\CommandHandler] | ||
public function __invoke(StoreUser $command) | ||
{ | ||
$this->entityManager->store([ | ||
'uuid' => $command->uuid, | ||
'username' => $command->username, | ||
'password' => $command->password | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App\Handler; | ||
|
||
use Spiral\Cqrs\Tests\App\Query\FindUserById; | ||
use Spiral\Cqrs\Tests\App\UserRepositoryInterface; | ||
|
||
final class UsersQueries | ||
{ | ||
public function __construct( | ||
private UserRepositoryInterface $users | ||
) { | ||
} | ||
|
||
#[\Spiral\Cqrs\Attribute\QueryHandler] | ||
public function findById(FindUserById $query): array | ||
{ | ||
return $this->users->findByPK($query->id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App\Query; | ||
|
||
use Spiral\Cqrs\QueryInterface; | ||
|
||
final class FindUserById implements QueryInterface | ||
{ | ||
public function __construct( | ||
public int $id | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests\App; | ||
|
||
interface UserRepositoryInterface | ||
{ | ||
public function findByPk(int $pk): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests; | ||
|
||
use Mockery as m; | ||
use Spiral\Cqrs\CommandBus; | ||
use Spiral\Cqrs\CommandInterface; | ||
use Spiral\Cqrs\Exception\CommandNotRegisteredException; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
|
||
final class CommandBusTest extends TestCase | ||
{ | ||
private CommandBus $bus; | ||
private m\LegacyMockInterface|MessageBusInterface|m\MockInterface $messageBus; | ||
private m\LegacyMockInterface|m\MockInterface|CommandInterface $command; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->bus = new CommandBus( | ||
$this->messageBus = m::mock(MessageBusInterface::class) | ||
); | ||
$this->command = m::mock(CommandInterface::class); | ||
} | ||
|
||
public function testDispatch(): void | ||
{ | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->command) | ||
->andReturn( | ||
new Envelope(new \stdClass(), [ | ||
new HandledStamp('foo', 'bar'), | ||
]) | ||
); | ||
|
||
$this->assertSame('foo', $this->bus->dispatch($this->command)); | ||
} | ||
|
||
public function testDispatchWithoutStamp(): void | ||
{ | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->command) | ||
->andReturn(new Envelope(new \stdClass(), [])); | ||
|
||
$this->assertNull($this->bus->dispatch($this->command)); | ||
} | ||
|
||
public function testNoHandlerForMessageException(): void | ||
{ | ||
$this->expectException(CommandNotRegisteredException::class); | ||
$this->expectErrorMessage( | ||
\sprintf('The command <%s> hasn\'t a command handler associated', $this->command::class) | ||
); | ||
|
||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->command) | ||
->andThrow(new NoHandlerForMessageException()); | ||
|
||
$this->bus->dispatch($this->command); | ||
} | ||
|
||
public function testHandlerFailedException(): void | ||
{ | ||
$this->expectException(\Exception::class); | ||
$this->expectErrorMessage('Something went wrong.'); | ||
|
||
$envelope = new Envelope(new \stdClass(), []); | ||
|
||
$exception = new \Exception('Something went wrong.'); | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->command) | ||
->andThrow(new HandlerFailedException($envelope, [ | ||
$exception | ||
])); | ||
|
||
$this->bus->dispatch($this->command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests; | ||
|
||
use Spiral\Cqrs\Tests\App\Command\StoreUser; | ||
use Spiral\Cqrs\Tests\App\EntityManagerInterface; | ||
use Spiral\Cqrs\Tests\App\Query\FindUserById; | ||
use Spiral\Cqrs\Tests\App\UserRepositoryInterface; | ||
|
||
final class HandlersLocatorTest extends TestCase | ||
{ | ||
public function testHandleCommand(): void | ||
{ | ||
$em = $this->mockContainer(EntityManagerInterface::class); | ||
|
||
$em->shouldReceive('store')->once()->with([ | ||
'uuid' => 'uuid-string', | ||
'username' => 'john_smith', | ||
'password' => 'secret', | ||
]); | ||
|
||
$this->getContainer() | ||
->get(\Spiral\Cqrs\CommandBusInterface::class)->dispatch( | ||
new StoreUser( | ||
'uuid-string', | ||
'john_smith', | ||
'secret' | ||
) | ||
); | ||
} | ||
|
||
public function testHandleQuery(): void | ||
{ | ||
$em = $this->mockContainer(UserRepositoryInterface::class); | ||
|
||
$em->shouldReceive('findByPk')->once()->with(123)->andReturn( | ||
$user = [ | ||
'uuid' => 'uuid-string', | ||
'username' => 'john_smith', | ||
'password' => 'secret', | ||
] | ||
); | ||
|
||
$this->assertSame( | ||
$user, | ||
$this->getContainer()->get(\Spiral\Cqrs\QueryBusInterface::class)->ask(new FindUserById(123)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cqrs\Tests; | ||
|
||
use Mockery as m; | ||
use Spiral\Cqrs\Exception\CommandNotRegisteredException; | ||
use Spiral\Cqrs\Exception\QueryNotRegisteredException; | ||
use Spiral\Cqrs\QueryBus; | ||
use Spiral\Cqrs\QueryInterface; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
|
||
final class QueryBusTest extends TestCase | ||
{ | ||
private QueryBus $bus; | ||
private m\LegacyMockInterface|MessageBusInterface|m\MockInterface $messageBus; | ||
private m\LegacyMockInterface|m\MockInterface|QueryInterface $query; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->bus = new QueryBus( | ||
$this->messageBus = m::mock(MessageBusInterface::class) | ||
); | ||
$this->query = m::mock(QueryInterface::class); | ||
} | ||
|
||
public function testAsk(): void | ||
{ | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->query) | ||
->andReturn( | ||
new Envelope(new \stdClass(), [ | ||
new HandledStamp('foo', 'bar'), | ||
]) | ||
); | ||
|
||
$this->assertSame('foo', $this->bus->ask($this->query)); | ||
} | ||
|
||
public function testDispatchWithoutStamp(): void | ||
{ | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->query) | ||
->andReturn(new Envelope(new \stdClass(), [])); | ||
|
||
$this->assertNull($this->bus->ask($this->query)); | ||
} | ||
|
||
public function testNoHandlerForMessageException(): void | ||
{ | ||
$this->expectException(QueryNotRegisteredException::class); | ||
$this->expectErrorMessage( | ||
\sprintf('The query <%s> hasn\'t a query handler associated', $this->query::class) | ||
); | ||
|
||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->query) | ||
->andThrow(new NoHandlerForMessageException()); | ||
|
||
$this->bus->ask($this->query); | ||
} | ||
|
||
public function testHandlerFailedException(): void | ||
{ | ||
$this->expectException(\Exception::class); | ||
$this->expectErrorMessage('Something went wrong.'); | ||
|
||
$envelope = new Envelope(new \stdClass(), []); | ||
|
||
$exception = new \Exception('Something went wrong.'); | ||
$this->messageBus->shouldReceive('dispatch') | ||
->once() | ||
->with($this->query) | ||
->andThrow(new HandlerFailedException($envelope, [ | ||
$exception | ||
])); | ||
|
||
$this->bus->ask($this->query); | ||
} | ||
} |