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