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\GenericEntityRemoveController; |
15
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
16
|
|
|
use stdClass; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Symfony\Component\Finder\Exception\AccessDeniedException; |
20
|
|
|
|
21
|
|
|
final class GenericEntityRemoveControllerTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var GenericEntityRemoveController |
26
|
|
|
*/ |
27
|
|
|
private $controller; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ControllerHelperInterface |
31
|
|
|
*/ |
32
|
|
|
private $controllerHelper; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->controller = new GenericEntityRemoveController($this->controllerHelper, [ |
|
|
|
|
39
|
|
|
'entity-class' => stdClass::class |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function shouldRejectMissingEntityClass() |
47
|
|
|
{ |
48
|
|
|
$this->expectException(InvalidArgumentException::class); |
49
|
|
|
|
50
|
|
|
new GenericEntityRemoveController($this->controllerHelper, []); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function shouldRejectControllerCalledAgain() |
57
|
|
|
{ |
58
|
|
|
$this->expectException(InvalidArgumentException::class); |
59
|
|
|
|
60
|
|
|
$this->controller->__construct($this->controllerHelper, [ |
61
|
|
|
'entity-class' => stdClass::class |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function shouldThrowExceptionIfEntityNotFound() |
69
|
|
|
{ |
70
|
|
|
$this->expectException(InvalidArgumentException::class); |
71
|
|
|
|
72
|
|
|
$this->controller->removeEntity("some-id"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function shouldRemoveEntity() |
79
|
|
|
{ |
80
|
|
|
$entity = new stdClass(); |
81
|
|
|
|
82
|
|
|
$this->controllerHelper->expects($this->once())->method('findEntity')->with( |
|
|
|
|
83
|
|
|
$this->equalTo(stdClass::class), |
84
|
|
|
$this->equalTo('some-id') |
85
|
|
|
)->willReturn($entity); |
86
|
|
|
|
87
|
|
|
$this->controllerHelper->expects($this->once())->method('removeEntity')->with( |
|
|
|
|
88
|
|
|
$this->identicalTo($entity) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
/** @var Response $response */ |
92
|
|
|
$response = $this->controller->removeEntity("some-id"); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
95
|
|
|
$this->assertEquals('Entity removed!', $response->getContent()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @test |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function shouldCheckIfAccessIsGranted() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->expectException(AccessDeniedException::class); |
104
|
|
|
|
105
|
|
|
$entity = new SampleEntity(); |
106
|
|
|
|
107
|
|
|
$this->controllerHelper->expects($this->once())->method('findEntity')->with( |
|
|
|
|
108
|
|
|
$this->equalTo(SampleEntity::class), |
109
|
|
|
$this->equalTo("some-id") |
110
|
|
|
)->willReturn($entity); |
111
|
|
|
|
112
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
113
|
|
|
$this->equalTo('some-attribute'), |
114
|
|
|
$this->identicalTo($entity) |
115
|
|
|
)->will($this->returnCallback( |
116
|
|
|
function () { |
117
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
118
|
|
|
} |
119
|
|
|
)); |
120
|
|
|
|
121
|
|
|
$controller = new GenericEntityRemoveController($this->controllerHelper, [ |
122
|
|
|
'entity-class' => SampleEntity::class, |
123
|
|
|
'authorization-attribute' => 'some-attribute', |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$controller->removeEntity("some-id"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
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..