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\Bus\Tests\Units\Middlewares\Validator; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
15
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Locator\InMemoryLocator; |
16
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerClass\HandlerClassResolver; |
17
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\ChainResolver; |
18
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameAndSuffixResolver; |
19
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\NameOfMessage\FromClassNameResolver; |
20
|
|
|
use Cubiche\Core\Bus\Middlewares\Validator\ValidatorMiddleware; |
21
|
|
|
use Cubiche\Core\Bus\Tests\Fixtures\Message\LoginUserMessage; |
22
|
|
|
use Cubiche\Core\Bus\Tests\Fixtures\Message\LogoutUserMessage; |
23
|
|
|
use Cubiche\Core\Bus\Tests\Fixtures\Message\RemoveUserMessage; |
24
|
|
|
use Cubiche\Core\Bus\Tests\Fixtures\Message\UserMessageValidator; |
25
|
|
|
use Cubiche\Core\Bus\Tests\Units\TestCase; |
26
|
|
|
use Cubiche\Core\Validator\Exception\ValidationException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ValidatorMiddleware class. |
30
|
|
|
* |
31
|
|
|
* Generated by TestGenerator on 2016-04-11 at 15:18:25. |
32
|
|
|
*/ |
33
|
|
|
class ValidatorMiddlewareTests extends TestCase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Test handle method. |
37
|
|
|
*/ |
38
|
|
|
public function testHandle() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->given( |
42
|
|
|
$resolver = new HandlerClassResolver( |
43
|
|
|
new FromClassNameResolver(), |
44
|
|
|
new ChainResolver([ |
45
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
46
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
47
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
48
|
|
|
]), |
49
|
|
|
new InMemoryLocator([LoginUserMessage::class => new UserMessageValidator()]) |
50
|
|
|
) |
51
|
|
|
) |
52
|
|
|
->and($middleware = new ValidatorMiddleware($resolver)) |
53
|
|
|
->and($command = new LoginUserMessage('[email protected]', 'plainpassword')) |
|
|
|
|
54
|
|
|
->and($callable = function (LoginUserMessage $command) { |
55
|
|
|
$command->setEmail('[email protected]'); |
56
|
|
|
}) |
57
|
|
|
->when($middleware->handle($command, $callable)) |
58
|
|
|
->then() |
59
|
|
|
->string($command->email()) |
60
|
|
|
->isEqualTo('[email protected]') |
61
|
|
|
->and() |
62
|
|
|
->when($command = new LoginUserMessage('[email protected]', 'plainpassword')) |
|
|
|
|
63
|
|
|
->then() |
64
|
|
|
->exception(function () use ($middleware, $command, $callable) { |
65
|
|
|
$middleware->handle($command, $callable); |
66
|
|
|
})->isInstanceOf(ValidationException::class) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$this |
70
|
|
|
->given( |
71
|
|
|
$resolver = new HandlerClassResolver( |
72
|
|
|
new FromClassNameResolver(), |
73
|
|
|
new ChainResolver([ |
74
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
75
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
76
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
77
|
|
|
]), |
78
|
|
|
new InMemoryLocator([LoginUserMessage::class => new UserMessageValidator()]) |
79
|
|
|
) |
80
|
|
|
) |
81
|
|
|
->and($middleware = new ValidatorMiddleware($resolver)) |
82
|
|
|
->and($command = new LoginUserMessage('invalid.email.com', 'plainpassword')) |
|
|
|
|
83
|
|
|
->and($callable = function () { |
84
|
|
|
}) |
85
|
|
|
->then() |
86
|
|
|
->exception(function () use ($middleware, $command, $callable) { |
87
|
|
|
$middleware->handle($command, $callable); |
88
|
|
|
})->isInstanceOf(ValidationException::class) |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
$this |
92
|
|
|
->given( |
93
|
|
|
$resolver = new HandlerClassResolver( |
94
|
|
|
new FromClassNameResolver(), |
95
|
|
|
new ChainResolver([ |
96
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
97
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
98
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
99
|
|
|
]), |
100
|
|
|
new InMemoryLocator([LogoutUserMessage::class => new UserMessageValidator()]) |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
->and($middleware = new ValidatorMiddleware($resolver)) |
104
|
|
|
->and($command = new LogoutUserMessage('invalid.email.com')) |
105
|
|
|
->and($callable = function (LogoutUserMessage $command) { |
106
|
|
|
$command->setEmail('[email protected]'); |
107
|
|
|
}) |
108
|
|
|
->then() |
109
|
|
|
->exception(function () use ($middleware, $command, $callable) { |
110
|
|
|
$middleware->handle($command, $callable); |
111
|
|
|
})->isInstanceOf(ValidationException::class) |
112
|
|
|
; |
113
|
|
|
|
114
|
|
|
$this |
115
|
|
|
->given( |
116
|
|
|
$resolver = new HandlerClassResolver( |
117
|
|
|
new FromClassNameResolver(), |
118
|
|
|
new ChainResolver([ |
119
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
120
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
121
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
122
|
|
|
]), |
123
|
|
|
new InMemoryLocator([]) |
124
|
|
|
) |
125
|
|
|
) |
126
|
|
|
->and($middleware = new ValidatorMiddleware($resolver)) |
127
|
|
|
->and($command = new RemoveUserMessage('[email protected]')) |
128
|
|
|
->and($callable = function (RemoveUserMessage $command) { |
129
|
|
|
$command->setEmail('[email protected]'); |
130
|
|
|
}) |
131
|
|
|
->when($middleware->handle($command, $callable)) |
132
|
|
|
->then() |
133
|
|
|
->string($command->email()) |
134
|
|
|
->isEqualTo('[email protected]') |
135
|
|
|
; |
136
|
|
|
|
137
|
|
|
$this |
138
|
|
|
->given( |
139
|
|
|
$resolver = new HandlerClassResolver( |
140
|
|
|
new FromClassNameResolver(), |
141
|
|
|
new ChainResolver([ |
142
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
143
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
144
|
|
|
new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
145
|
|
|
]), |
146
|
|
|
new InMemoryLocator([RemoveUserMessage::class => new UserMessageValidator()]) |
147
|
|
|
) |
148
|
|
|
) |
149
|
|
|
->and($middleware = new ValidatorMiddleware($resolver)) |
150
|
|
|
->and($command = new RemoveUserMessage('[email protected]')) |
151
|
|
|
->and($callable = function (RemoveUserMessage $command) { |
152
|
|
|
$command->setEmail('[email protected]'); |
153
|
|
|
}) |
154
|
|
|
->then() |
155
|
|
|
->exception(function () use ($middleware, $command, $callable) { |
156
|
|
|
$middleware->handle($command, $callable); |
157
|
|
|
})->isInstanceOf(NotFoundException::class) |
158
|
|
|
; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.