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\SymfonyGenerics\Tests\Unit\Controllers; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Controllers\GenericTemplateRenderController; |
15
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
16
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
22
|
|
|
|
23
|
|
|
final class GenericTemplateRenderControllerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ControllerHelperInterface |
28
|
|
|
*/ |
29
|
|
|
private $controllerHelper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ArgumentCompilerInterface |
33
|
|
|
*/ |
34
|
|
|
private $argumentCompiler; |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
39
|
|
|
$this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function shouldRenderTemplate() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
/** @var Request $request */ |
48
|
|
|
$request = $this->createMock(Request::class); |
49
|
|
|
|
50
|
|
|
/** @var Response $expectedResponse */ |
51
|
|
|
$expectedResponse = $this->createMock(Response::class); |
52
|
|
|
|
53
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
|
|
|
|
54
|
|
|
$this->equalTo(['foo' => 'bar']), |
55
|
|
|
$this->identicalTo($request) |
56
|
|
|
)->willReturn([ |
57
|
|
|
'bar' => 'baz' |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->controllerHelper->expects($this->once())->method('renderTemplate')->with( |
|
|
|
|
61
|
|
|
$this->equalTo("@foo/bar/baz.html"), |
62
|
|
|
$this->equalTo(['bar' => 'baz']) |
63
|
|
|
)->willReturn($expectedResponse); |
64
|
|
|
|
65
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
66
|
|
|
'template' => "@foo/bar/baz.html", |
67
|
|
|
'arguments' => [ |
68
|
|
|
'foo' => 'bar' |
69
|
|
|
] |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
/** @var Response $actualResponse */ |
73
|
|
|
$actualResponse = $controller->renderTemplate($request); |
74
|
|
|
|
75
|
|
|
$this->assertSame($expectedResponse, $actualResponse); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function shouldCheckIfAccessIsGranted() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->expectException(AccessDeniedException::class); |
84
|
|
|
|
85
|
|
|
/** @var Request $request */ |
86
|
|
|
$request = $this->createMock(Request::class); |
87
|
|
|
|
88
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
89
|
|
|
$this->equalTo('some-attribute'), |
90
|
|
|
$this->equalTo($request) |
91
|
|
|
)->will($this->returnCallback( |
92
|
|
|
function () { |
93
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
94
|
|
|
} |
95
|
|
|
)); |
96
|
|
|
|
97
|
|
|
/** @var mixed $controller */ |
98
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
99
|
|
|
'template' => "@foo/bar/baz.html", |
100
|
|
|
'authorization-attribute' => 'some-attribute', |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
$controller->renderTemplate($request); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function shouldRejectConstructorCalledAgain() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->expectException(InvalidArgumentException::class); |
112
|
|
|
|
113
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
114
|
|
|
'template' => "@foo/bar/baz.html", |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$controller->__construct($this->controllerHelper, $this->argumentCompiler, [ |
118
|
|
|
'template' => "@foo/bar/baz.html", |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @test |
124
|
|
|
*/ |
125
|
|
|
public function shouldThrowExceptionIfTemplatePathIsMissing() |
126
|
|
|
{ |
127
|
|
|
$this->expectException(InvalidArgumentException::class); |
128
|
|
|
|
129
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
|
|
|
|
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function shouldBeCallableByInvokingController() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
/** @var Request $request */ |
139
|
|
|
$request = $this->createMock(Request::class); |
140
|
|
|
|
141
|
|
|
/** @var Response $expectedResponse */ |
142
|
|
|
$expectedResponse = $this->createMock(Response::class); |
143
|
|
|
|
144
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
|
|
|
|
145
|
|
|
$this->equalTo(['foo' => 'bar']), |
146
|
|
|
$this->identicalTo($request) |
147
|
|
|
)->willReturn([ |
148
|
|
|
'bar' => 'baz' |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$this->controllerHelper->expects($this->once())->method('renderTemplate')->with( |
|
|
|
|
152
|
|
|
$this->equalTo("@foo/bar/baz.html"), |
153
|
|
|
$this->equalTo(['bar' => 'baz']) |
154
|
|
|
)->willReturn($expectedResponse); |
155
|
|
|
|
156
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
157
|
|
|
'template' => "@foo/bar/baz.html", |
158
|
|
|
'arguments' => [ |
159
|
|
|
'foo' => 'bar' |
160
|
|
|
] |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
164
|
|
|
|
165
|
|
|
/** @var Response $actualResponse */ |
166
|
|
|
$actualResponse = $controller(); |
167
|
|
|
|
168
|
|
|
$this->assertSame($expectedResponse, $actualResponse); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function shouldRejectCallWithoutRequest() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$this->expectException(InvalidArgumentException::class); |
177
|
|
|
|
178
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
179
|
|
|
'template' => "@foo/bar/baz.html", |
180
|
|
|
'arguments' => [] |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn(null); |
|
|
|
|
184
|
|
|
|
185
|
|
|
$controller(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
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..