|
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
|
|
|
|
|
22
|
|
|
final class GenericTemplateRenderControllerTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ControllerHelperInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $controllerHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ArgumentCompilerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $argumentCompiler; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
|
|
38
|
|
|
$this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @test |
|
43
|
|
|
*/ |
|
44
|
|
|
public function shouldRenderTemplate() |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var Request $request */ |
|
47
|
|
|
$request = $this->createMock(Request::class); |
|
48
|
|
|
|
|
49
|
|
|
/** @var Response $expectedResponse */ |
|
50
|
|
|
$expectedResponse = $this->createMock(Response::class); |
|
51
|
|
|
|
|
52
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildArguments')->with( |
|
|
|
|
|
|
53
|
|
|
$this->equalTo(['foo' => 'bar']), |
|
54
|
|
|
$this->identicalTo($request) |
|
55
|
|
|
)->willReturn([ |
|
56
|
|
|
'bar' => 'baz' |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$this->controllerHelper->expects($this->once())->method('renderTemplate')->with( |
|
|
|
|
|
|
60
|
|
|
$this->equalTo("@foo/bar/baz.html"), |
|
61
|
|
|
$this->equalTo(['bar' => 'baz']) |
|
62
|
|
|
)->willReturn($expectedResponse); |
|
63
|
|
|
|
|
64
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
|
65
|
|
|
'template' => "@foo/bar/baz.html", |
|
66
|
|
|
'arguments' => [ |
|
67
|
|
|
'foo' => 'bar' |
|
68
|
|
|
] |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
/** @var Response $actualResponse */ |
|
72
|
|
|
$actualResponse = $controller->renderTemplate($request); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertSame($expectedResponse, $actualResponse); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @test |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function shouldCheckIfAccessIsGranted() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$this->expectException(AccessDeniedException::class); |
|
83
|
|
|
|
|
84
|
|
|
/** @var Request $request */ |
|
85
|
|
|
$request = $this->createMock(Request::class); |
|
86
|
|
|
|
|
87
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
|
|
88
|
|
|
$this->equalTo('some-attribute'), |
|
89
|
|
|
$this->equalTo($request) |
|
90
|
|
|
)->will($this->returnCallback( |
|
91
|
|
|
function () { |
|
92
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
|
93
|
|
|
} |
|
94
|
|
|
)); |
|
95
|
|
|
|
|
96
|
|
|
/** @var mixed $controller */ |
|
97
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
|
98
|
|
|
'template' => "@foo/bar/baz.html", |
|
99
|
|
|
'authorization-attribute' => 'some-attribute', |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
$controller->renderTemplate($request); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @test |
|
107
|
|
|
*/ |
|
108
|
|
|
public function shouldRejectConstructorCalledAgain() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
111
|
|
|
|
|
112
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
|
113
|
|
|
'template' => "@foo/bar/baz.html", |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$controller->__construct($this->controllerHelper, $this->argumentCompiler, [ |
|
117
|
|
|
'template' => "@foo/bar/baz.html", |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @test |
|
123
|
|
|
*/ |
|
124
|
|
|
public function shouldThrowExceptionIfTemplatePathIsMissing() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
127
|
|
|
|
|
128
|
|
|
$controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [ |
|
|
|
|
|
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
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..