Skip to content

Commit 65b0e82

Browse files
committed
Adds basic command bus tests.
1 parent 9f958cf commit 65b0e82

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"autoload-dev": {
100100
"psr-4": {
101101
"Tempest\\Clock\\Tests\\": "src/Tempest/Clock/tests",
102+
"Tempest\\CommandBus\\Tests\\": "src/Tempest/CommandBus/tests",
102103
"Tempest\\Container\\Tests\\": "src/Tempest/Container/tests",
103104
"Tempest\\Core\\Tests\\": "src/Tempest/Core/tests",
104105
"Tempest\\Database\\Tests\\": "src/Tempest/Database/tests",

src/Tempest/CommandBus/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"Tempest\\CommandBus\\": "src"
1414
}
1515
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Tempest\\CommandBus\\Tests\\": "tests"
19+
}
20+
},
1621
"license": "MIT",
1722
"minimum-stability": "dev"
1823
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\CommandBus\Tests\Fixtures;
6+
7+
final readonly class CreateUserCommand
8+
{
9+
public function __construct(
10+
public string $firstName,
11+
public string $lastName,
12+
) {
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\CommandBus\Tests\Fixtures;
6+
7+
use Tempest\CommandBus\CommandHandler;
8+
9+
final class CreateUserCommandHandler
10+
{
11+
public string $firstName;
12+
13+
public string $lastName;
14+
15+
#[CommandHandler]
16+
public function __invoke(CreateUserCommand $command): void
17+
{
18+
$this->firstName = $command->firstName;
19+
$this->lastName = $command->lastName;
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\CommandBus\Tests\Fixtures;
6+
7+
final readonly class DeleteUserCommand
8+
{
9+
public function __construct(public int $id)
10+
{
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\CommandBus\Tests\Fixtures;
6+
7+
final class UserRepository
8+
{
9+
private array $users = [];
10+
11+
public function addUser(string $firstName, string $lastName): void
12+
{
13+
$this->users[] = join(' ', [$firstName, $lastName]);
14+
}
15+
16+
public function getUsers(): array
17+
{
18+
return $this->users;
19+
}
20+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\CommandBus\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Tempest\CommandBus\CommandBus;
9+
use Tempest\CommandBus\CommandBusConfig;
10+
use Tempest\CommandBus\CommandHandler;
11+
use Tempest\CommandBus\CommandHandlerNotFound;
12+
use Tempest\CommandBus\GenericCommandBus;
13+
use Tempest\CommandBus\Tests\Fixtures\CreateUserCommand;
14+
use Tempest\CommandBus\Tests\Fixtures\CreateUserCommandHandler;
15+
use Tempest\CommandBus\Tests\Fixtures\DeleteUserCommand;
16+
use Tempest\Container\GenericContainer;
17+
use Tempest\Reflection\ClassReflector;
18+
19+
/**
20+
* @internal
21+
* @small
22+
*/
23+
final class GenericCommandBusTest extends TestCase
24+
{
25+
private CommandBus $commandBus;
26+
27+
public function test_getting_command_handler_that_exists(): void
28+
{
29+
$command = new CreateUserCommand('Jim', 'Halpert');
30+
31+
$this->commandBus->dispatch($command);
32+
33+
$this->assertCount(1, $this->commandBus->getHistory());
34+
$this->assertSame($command, $this->commandBus->getHistory()[0]);
35+
}
36+
37+
public function test_exception_is_thrown_when_command_handler_doesnt_exist(): void
38+
{
39+
$command = new DeleteUserCommand(12);
40+
41+
$this->expectExceptionObject(
42+
new CommandHandlerNotFound($command)
43+
);
44+
45+
$this->commandBus->dispatch($command);
46+
}
47+
48+
protected function setUp(): void
49+
{
50+
parent::setUp();
51+
52+
// TODO: I'd like to make this easier to setup.
53+
$config = new CommandBusConfig();
54+
55+
$createUserCommandHandlerClass = new ClassReflector(CreateUserCommandHandler::class);
56+
$createUserCommandHandlerMethod = $createUserCommandHandlerClass->getMethod('__invoke');
57+
$createUserCommandHandler = $createUserCommandHandlerMethod->getAttribute(CommandHandler::class);
58+
59+
$config->addHandler(
60+
commandHandler: $createUserCommandHandler,
61+
commandName: CreateUserCommand::class,
62+
handler: $createUserCommandHandlerMethod
63+
);
64+
65+
$this->commandBus = new GenericCommandBus(
66+
container: new GenericContainer(),
67+
commandBusConfig: $config
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)