1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Cqrs\Tests\Units\Middlewares\Handler; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Locator\InMemoryLocator; |
13
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerClass\HandlerClassResolver; |
14
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\DefaultResolver as HandlerMethodNameDefaultResolver; |
15
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\CommandHandlerMiddleware; |
16
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfCommand\FromClassNameResolver; |
17
|
|
|
use Cubiche\Core\Cqrs\Tests\Fixtures\Command\LoginUserCommand; |
18
|
|
|
use Cubiche\Core\Cqrs\Tests\Fixtures\Command\LoginUserCommandHandler; |
19
|
|
|
use Cubiche\Core\Cqrs\Tests\Units\TestCase; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* CommandHandlerMiddlewareTests class. |
23
|
|
|
* |
24
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CommandHandlerMiddlewareTests extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Test handle method. |
30
|
|
|
*/ |
31
|
|
|
public function testHandle() |
32
|
|
|
{ |
33
|
|
|
$this |
34
|
|
|
->given( |
35
|
|
|
$resolver = new HandlerClassResolver( |
36
|
|
|
new FromClassNameResolver(), |
37
|
|
|
new HandlerMethodNameDefaultResolver(), |
38
|
|
|
new InMemoryLocator([LoginUserCommand::class => new LoginUserCommandHandler()]) |
39
|
|
|
) |
40
|
|
|
) |
41
|
|
|
->and($middleware = new CommandHandlerMiddleware($resolver)) |
42
|
|
|
->and($command = new LoginUserCommand('[email protected]', 'plainpassword')) |
43
|
|
|
->and($callable = function (LoginUserCommand $command) { |
44
|
|
|
$command->setEmail('[email protected]'); |
45
|
|
|
}) |
46
|
|
|
->when($middleware->handle($command, $callable)) |
47
|
|
|
->then() |
48
|
|
|
->string($command->email()) |
49
|
|
|
->isEqualTo('[email protected]') |
50
|
|
|
->exception(function () use ($middleware, $callable) { |
51
|
|
|
$middleware->handle(new \StdClass(), $callable); |
52
|
|
|
})->isInstanceOf(\InvalidArgumentException::class) |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Test dispatcher method. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testDispatcher() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this |
62
|
|
|
->given( |
63
|
|
|
$resolver = new HandlerClassResolver( |
64
|
|
|
new FromClassNameResolver(), |
65
|
|
|
new HandlerMethodNameDefaultResolver(), |
66
|
|
|
new InMemoryLocator([LoginUserCommand::class => new LoginUserCommandHandler()]) |
67
|
|
|
) |
68
|
|
|
) |
69
|
|
|
->and($middleware = new CommandHandlerMiddleware($resolver)) |
70
|
|
|
->when($result = $middleware->resolver()) |
71
|
|
|
->then() |
72
|
|
|
->object($result) |
73
|
|
|
->isEqualTo($resolver) |
74
|
|
|
; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.