Skip to content

Commit bc8f981

Browse files
authored
Merge pull request #387 from tempestphp/add-command-bus-tests
Adds command bus tests
2 parents 57a9e87 + e8dbd23 commit bc8f981

File tree

9 files changed

+128
-2
lines changed

9 files changed

+128
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
8+
## [Unreleased]
9+
10+
### Added
11+
- Unit tests for the CommandBus `dispatch` method.

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",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<directory suffix=".php">src</directory>
2525
</include>
2626
<exclude>
27-
<directory suffix="Test.php">src</directory>
27+
<directory suffix=".php">src/Tempest/**/tests</directory>
2828
<directory suffix=".cache.php">src</directory>
2929
</exclude>
3030
</source>

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: 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+
}

test

Whitespace-only changes.

0 commit comments

Comments
 (0)