1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Cqrs\Tests\Units\Command; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
15
|
|
|
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware; |
16
|
|
|
use Cubiche\Core\Bus\Tests\Units\BusTests; |
17
|
|
|
use Cubiche\Core\Cqrs\Command\CommandBus; |
18
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\CommandHandlerMiddleware; |
19
|
|
|
use Cubiche\Core\Cqrs\Tests\Fixtures\Command\EncodePasswordCommand; |
20
|
|
|
use Cubiche\Core\Cqrs\Tests\Fixtures\Command\EncodePasswordHandler; |
21
|
|
|
use Cubiche\Core\Cqrs\Tests\Fixtures\FooMessage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* CommandBusTests class. |
25
|
|
|
* |
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CommandBusTests extends BusTests |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Test create without command handler middleware. |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function testCreateWithoutCommandHandlerMiddleware() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->given($middleware = new LockingMiddleware()) |
37
|
|
|
->and($commandBus = new CommandBus([$middleware])) |
38
|
|
|
->then() |
39
|
|
|
->exception(function () use ($commandBus) { |
40
|
|
|
$commandBus->dispatch(new EncodePasswordCommand('plainpassword')); |
41
|
|
|
}) |
42
|
|
|
->isInstanceOf(NotFoundException::class) |
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test dispatch chained middlewares. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testDispatchChainedMiddlewares() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this |
52
|
|
|
->given($commandBus = CommandBus::create()) |
53
|
|
|
->and($command = new EncodePasswordCommand('plainpassword')) |
54
|
|
|
->and($commandBus->addHandler(EncodePasswordCommand::class, new EncodePasswordHandler('md5'))) |
55
|
|
|
->and($commandBus->dispatch($command)) |
56
|
|
|
->then() |
57
|
|
|
->string($command->password()) |
58
|
|
|
->isEqualTo(md5('plainpassword')) |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test getHandlerFor. |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testGetHandlerFor() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this |
68
|
|
|
->given($commandBus = CommandBus::create()) |
69
|
|
|
->and($command = new EncodePasswordCommand('plainpassword')) |
70
|
|
|
->and($commandHandler = new EncodePasswordHandler('md5')) |
71
|
|
|
->and($commandBus->addHandler(EncodePasswordCommand::class, $commandHandler)) |
72
|
|
|
->then() |
73
|
|
|
->object($commandBus->getHandlerFor(EncodePasswordCommand::class)) |
74
|
|
|
->isEqualTo($commandHandler) |
75
|
|
|
; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test dispatch with invalid command. |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testDispatchWithInvalidCommand() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this |
84
|
|
|
->given($commandBus = CommandBus::create()) |
85
|
|
|
->then() |
86
|
|
|
->exception(function () use ($commandBus) { |
87
|
|
|
$commandBus->dispatch(new FooMessage()); |
88
|
|
|
}) |
89
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Test handlerMiddleware method. |
95
|
|
|
*/ |
96
|
|
|
public function testHandlerMiddleware() |
97
|
|
|
{ |
98
|
|
|
$this |
99
|
|
|
->given($commandBus = CommandBus::create()) |
100
|
|
|
->when($middleware = $commandBus->handlerMiddleware()) |
101
|
|
|
->then() |
102
|
|
|
->object($middleware) |
103
|
|
|
->isInstanceOf(CommandHandlerMiddleware::class) |
104
|
|
|
; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.