1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Controllers\GenericEntityInvokeController; |
15
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
16
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use stdClass; |
19
|
|
|
use ReflectionMethod; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
final class GenericEntityInvokeControllerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var GenericEntityInvokeController |
27
|
|
|
*/ |
28
|
|
|
private $controller; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ControllerHelperInterface |
32
|
|
|
*/ |
33
|
|
|
private $controllerHelper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ArgumentCompilerInterface |
37
|
|
|
*/ |
38
|
|
|
private $argumentCompiler; |
39
|
|
|
|
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
43
|
|
|
$this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function shouldInvodeAnEntityMethod() |
50
|
|
|
{ |
51
|
|
|
/** @var Request $request */ |
52
|
|
|
$request = $this->createMock(Request::class); |
53
|
|
|
|
54
|
|
|
$this->controller = new GenericEntityInvokeController( |
55
|
|
|
$this->controllerHelper, |
56
|
|
|
$this->argumentCompiler, |
57
|
|
|
[ |
58
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
59
|
|
|
'method' => 'buildRouteArguments', |
60
|
|
|
'arguments' => [ |
61
|
|
|
'argumentsConfiguration' => "Lorem", |
62
|
|
|
'request' => "ipsum" |
63
|
|
|
] |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->controllerHelper->expects($this->once())->method('findEntity')->with( |
|
|
|
|
68
|
|
|
$this->equalTo(get_class($this->argumentCompiler)), |
69
|
|
|
$this->equalTo("123") |
70
|
|
|
)->willReturn($this->argumentCompiler); |
71
|
|
|
|
72
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
73
|
|
|
$this->equalTo(new ReflectionMethod(get_class($this->argumentCompiler), 'buildRouteArguments')), |
74
|
|
|
$this->equalTo([ |
75
|
|
|
'argumentsConfiguration' => "Lorem", |
76
|
|
|
'request' => "ipsum" |
77
|
|
|
]), |
78
|
|
|
$this->identicalTo($request) |
79
|
|
|
)->willReturn([ |
80
|
|
|
['foo' => 'bar'], |
81
|
|
|
$request |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildRouteArguments')->with( |
|
|
|
|
85
|
|
|
$this->equalTo(['foo' => 'bar']), |
86
|
|
|
$this->identicalTo($request) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->controller->invokeEntityMethod($request, "123"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
|
public function shouldThrowExceptionWhenEntityNotFound() |
96
|
|
|
{ |
97
|
|
|
$this->expectException(InvalidArgumentException::class); |
98
|
|
|
|
99
|
|
|
/** @var Request $request */ |
100
|
|
|
$request = $this->createMock(Request::class); |
101
|
|
|
|
102
|
|
|
$this->controller = new GenericEntityInvokeController( |
103
|
|
|
$this->controllerHelper, |
104
|
|
|
$this->argumentCompiler, |
105
|
|
|
[ |
106
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
107
|
|
|
'method' => 'buildRouteArguments', |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->controllerHelper->expects($this->once())->method('findEntity')->with( |
|
|
|
|
112
|
|
|
$this->equalTo(get_class($this->argumentCompiler)), |
113
|
|
|
$this->equalTo("123") |
114
|
|
|
)->willReturn(null); |
115
|
|
|
|
116
|
|
|
$this->controller->invokeEntityMethod($request, "123"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @test |
121
|
|
|
*/ |
122
|
|
|
public function shouldRejectConstructorCalledAgain() |
123
|
|
|
{ |
124
|
|
|
$this->expectException(InvalidArgumentException::class); |
125
|
|
|
|
126
|
|
|
$this->controller = new GenericEntityInvokeController($this->controllerHelper, $this->argumentCompiler, [ |
127
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
128
|
|
|
'method' => 'buildRouteArguments', |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$this->controller->__construct($this->controllerHelper, $this->argumentCompiler, [ |
132
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
133
|
|
|
'method' => 'buildRouteArguments', |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @test |
139
|
|
|
*/ |
140
|
|
|
public function shouldRejectMissingEntityClass() |
141
|
|
|
{ |
142
|
|
|
$this->expectException(InvalidArgumentException::class); |
143
|
|
|
|
144
|
|
|
new GenericEntityInvokeController( |
145
|
|
|
$this->controllerHelper, |
146
|
|
|
$this->argumentCompiler, |
147
|
|
|
[ |
148
|
|
|
'method' => 'buildRouteArguments', |
149
|
|
|
] |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @test |
155
|
|
|
*/ |
156
|
|
|
public function shouldRejectMissingMethod() |
157
|
|
|
{ |
158
|
|
|
$this->expectException(InvalidArgumentException::class); |
159
|
|
|
|
160
|
|
|
new GenericEntityInvokeController( |
161
|
|
|
$this->controllerHelper, |
162
|
|
|
$this->argumentCompiler, |
163
|
|
|
[ |
164
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @test |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function shouldRejectNonEntityClass() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$this->expectException(InvalidArgumentException::class); |
175
|
|
|
$this->expectExceptionMessage('Expected an existing class name. Got: "NonExistingClass"'); |
176
|
|
|
|
177
|
|
|
new GenericEntityInvokeController( |
178
|
|
|
$this->controllerHelper, |
179
|
|
|
$this->argumentCompiler, |
180
|
|
|
[ |
181
|
|
|
'entity-class' => "NonExistingClass", |
182
|
|
|
'method' => 'buildRouteArguments', |
183
|
|
|
] |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @test |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
public function shouldRejectNonExistingMethod() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$this->expectException(InvalidArgumentException::class); |
193
|
|
|
|
194
|
|
|
new GenericEntityInvokeController( |
195
|
|
|
$this->controllerHelper, |
196
|
|
|
$this->argumentCompiler, |
197
|
|
|
[ |
198
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
199
|
|
|
'method' => 'nonExistingMethod', |
200
|
|
|
] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @test |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function shouldRejectNonArrayArguments() |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$this->expectException(InvalidArgumentException::class); |
210
|
|
|
|
211
|
|
|
new GenericEntityInvokeController( |
212
|
|
|
$this->controllerHelper, |
213
|
|
|
$this->argumentCompiler, |
214
|
|
|
[ |
215
|
|
|
'entity-class' => get_class($this->argumentCompiler), |
216
|
|
|
'method' => 'buildRouteArguments', |
217
|
|
|
'arguments' => "12345", |
218
|
|
|
] |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..