|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
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 Core23\Form\Test; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\Form\Handler\FormHandlerInterface; |
|
15
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
16
|
|
|
use PHPUnit\Framework\Constraint\Constraint; |
|
|
|
|
|
|
17
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
|
|
|
|
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
19
|
|
|
use Symfony\Component\Form\FormError; |
|
20
|
|
|
use Symfony\Component\Form\FormInterface; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
24
|
|
|
|
|
25
|
|
|
abstract class AbstractFormHandlerTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var FormInterface|MockObject |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $form; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Request |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $request; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var MockObject|Session |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $session; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $errors; |
|
46
|
|
|
|
|
47
|
|
|
protected function setUp(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->form = $this->createMock(FormInterface::class); |
|
50
|
|
|
|
|
51
|
|
|
$this->session = $this->createMock(Session::class); |
|
52
|
|
|
|
|
53
|
|
|
$this->request = new Request(); |
|
54
|
|
|
$this->request->setSession($this->session); |
|
55
|
|
|
|
|
56
|
|
|
$this->errors = []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
abstract protected function createFormHandler(): FormHandlerInterface; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Executes the preProcess method. |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed|null $data |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \ReflectionException |
|
67
|
|
|
*/ |
|
68
|
|
|
final protected function executePreProcess(Request $request, $data = null): ?Response |
|
69
|
|
|
{ |
|
70
|
|
|
$handler = $this->createFormHandler(); |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $data) { |
|
73
|
|
|
$this->form->method('getData') |
|
|
|
|
|
|
74
|
|
|
->willReturn($data) |
|
75
|
|
|
; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$method = new \ReflectionMethod($handler, 'preProcess'); |
|
79
|
|
|
$method->setAccessible(true); |
|
80
|
|
|
|
|
81
|
|
|
$this->checkCalledErrors(); |
|
82
|
|
|
|
|
83
|
|
|
$result = $method->invoke($handler, $this->form, $request); |
|
84
|
|
|
|
|
85
|
|
|
$this->checkUncalledErrors(); |
|
86
|
|
|
|
|
87
|
|
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Executes the process method. |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed|null $data |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \ReflectionException |
|
96
|
|
|
*/ |
|
97
|
|
|
final protected function executeProcess(Request $request, $data = null): bool |
|
98
|
|
|
{ |
|
99
|
|
|
$handler = $this->createFormHandler(); |
|
100
|
|
|
|
|
101
|
|
|
if (null !== $data) { |
|
102
|
|
|
$this->form->method('getData') |
|
103
|
|
|
->willReturn($data) |
|
104
|
|
|
; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$method = new \ReflectionMethod($handler, 'process'); |
|
108
|
|
|
$method->setAccessible(true); |
|
109
|
|
|
|
|
110
|
|
|
$this->checkCalledErrors(); |
|
111
|
|
|
|
|
112
|
|
|
$result = $method->invoke($handler, $this->form, $request); |
|
113
|
|
|
|
|
114
|
|
|
$this->checkUncalledErrors(); |
|
115
|
|
|
|
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Executes the postProcess method. |
|
121
|
|
|
* |
|
122
|
|
|
* @param mixed|null $data |
|
123
|
|
|
* |
|
124
|
|
|
* @throws \ReflectionException |
|
125
|
|
|
*/ |
|
126
|
|
|
final protected function executePostProcess(Request $request, Response $response, $data = null): ?Response |
|
127
|
|
|
{ |
|
128
|
|
|
$handler = $this->createFormHandler(); |
|
129
|
|
|
|
|
130
|
|
|
if (null !== $data) { |
|
131
|
|
|
$this->form->method('getData') |
|
132
|
|
|
->willReturn($data) |
|
133
|
|
|
; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$method = new \ReflectionMethod($handler, 'postProcess'); |
|
137
|
|
|
$method->setAccessible(true); |
|
138
|
|
|
|
|
139
|
|
|
$this->checkCalledErrors(); |
|
140
|
|
|
|
|
141
|
|
|
$result = $method->invoke($handler, $this->form, $request, $response); |
|
142
|
|
|
|
|
143
|
|
|
$this->checkUncalledErrors(); |
|
144
|
|
|
|
|
145
|
|
|
return $result; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Assets an error. |
|
150
|
|
|
*/ |
|
151
|
|
|
final protected function assertError(string $message, array $messageParameters = []): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->errors[] = [ |
|
154
|
|
|
'message' => $message, |
|
155
|
|
|
'parameters' => $messageParameters, |
|
156
|
|
|
'count' => 0, |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private function equalToErrors(): Constraint |
|
161
|
|
|
{ |
|
162
|
|
|
return static::callback(function ($error) { |
|
163
|
|
|
if ($error instanceof FormError) { |
|
164
|
|
|
foreach ($this->errors as &$data) { |
|
165
|
|
|
if ($error->getMessage() === $data['message'] && $error->getMessageParameters() === $data['parameters']) { |
|
166
|
|
|
++$data['count']; |
|
167
|
|
|
|
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
throw new AssertionFailedError( |
|
173
|
|
|
sprintf("Method 'addError' was not expected to be called with message '%s'", $error->getMessage()) |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return false; |
|
178
|
|
|
}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
private function checkCalledErrors(): void |
|
182
|
|
|
{ |
|
183
|
|
|
$count = \count($this->errors); |
|
184
|
|
|
if (0 === $count) { |
|
185
|
|
|
$this->form->expects(static::never())->method('addError'); |
|
|
|
|
|
|
186
|
|
|
} else { |
|
187
|
|
|
$this->form->expects(static::exactly($count))->method('addError') |
|
188
|
|
|
->with($this->equalToErrors()) |
|
189
|
|
|
; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @throws AssertionFailedError |
|
195
|
|
|
*/ |
|
196
|
|
|
private function checkUncalledErrors(): void |
|
197
|
|
|
{ |
|
198
|
|
|
foreach ($this->errors as $data) { |
|
199
|
|
|
if (0 === $data['count']) { |
|
200
|
|
|
throw new AssertionFailedError( |
|
201
|
|
|
sprintf("Method 'addError' was expected to be called with message '%s' actually was not called", $data['message']) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths