1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AbterPhp\Website\Service\Execute; |
||
6 | |||
7 | use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
||
8 | use AbterPhp\Website\Domain\Entities\Block as Entity; |
||
9 | use AbterPhp\Website\Orm\BlockRepo as GridRepo; |
||
10 | use AbterPhp\Website\Validation\Factory\Block as ValidatorFactory; |
||
11 | use Casbin\Enforcer; |
||
12 | use Cocur\Slugify\Slugify; |
||
13 | use Opulence\Events\Dispatchers\IEventDispatcher; |
||
14 | use Opulence\Orm\IUnitOfWork; |
||
15 | use Opulence\Sessions\ISession; |
||
16 | use Opulence\Validation\IValidator; |
||
17 | use Opulence\Validation\Rules\Errors\ErrorCollection; |
||
18 | use PHPUnit\Framework\MockObject\MockObject; |
||
19 | use PHPUnit\Framework\TestCase; |
||
20 | |||
21 | class BlockTest extends TestCase |
||
22 | { |
||
23 | /** @var PageCategory - System Under Test */ |
||
24 | protected $sut; |
||
25 | |||
26 | /** @var GridRepo|MockObject */ |
||
27 | protected $gridRepoMock; |
||
28 | |||
29 | /** @var ValidatorFactory|MockObject */ |
||
30 | protected $validatorFactoryMock; |
||
31 | |||
32 | /** @var IUnitOfWork|MockObject */ |
||
33 | protected $unitOfWorkMock; |
||
34 | |||
35 | /** @var IEventDispatcher|MockObject */ |
||
36 | protected $eventDispatcherMock; |
||
37 | |||
38 | /** @var Slugify|MockObject */ |
||
39 | protected $slugifyMock; |
||
40 | |||
41 | /** @var ISession|MockObject */ |
||
42 | protected $sessionMock; |
||
43 | |||
44 | /** @var Enforcer|MockObject */ |
||
45 | protected $enforcerMock; |
||
46 | |||
47 | public function setUp(): void |
||
48 | { |
||
49 | parent::setUp(); |
||
50 | |||
51 | $this->gridRepoMock = $this->createMock(GridRepo::class); |
||
52 | $this->validatorFactoryMock = $this->createMock(ValidatorFactory::class); |
||
53 | $this->unitOfWorkMock = $this->createMock(IUnitOfWork::class); |
||
54 | $this->eventDispatcherMock = $this->createMock(IEventDispatcher::class); |
||
55 | $this->slugifyMock = $this->createMock(Slugify::class); |
||
56 | $this->sessionMock = $this->createMock(ISession::class); |
||
57 | $this->enforcerMock = $this->createMock(Enforcer::class); |
||
58 | |||
59 | $this->sut = new Block( |
||
0 ignored issues
–
show
|
|||
60 | $this->gridRepoMock, |
||
61 | $this->validatorFactoryMock, |
||
62 | $this->unitOfWorkMock, |
||
63 | $this->eventDispatcherMock, |
||
64 | $this->slugifyMock, |
||
65 | $this->sessionMock, |
||
66 | $this->enforcerMock |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | public function testCreateEntity() |
||
71 | { |
||
72 | $id = 'foo'; |
||
73 | |||
74 | $actualResult = $this->sut->createEntity($id); |
||
75 | |||
76 | $this->assertInstanceOf(Entity::class, $actualResult); |
||
77 | $this->assertSame($id, $actualResult->getId()); |
||
78 | } |
||
79 | |||
80 | public function testCreate() |
||
81 | { |
||
82 | $title = 'Bar'; |
||
83 | $identifier = 'bar'; |
||
84 | $layout = 'baz'; |
||
85 | $postData = [ |
||
86 | 'title' => $title, |
||
87 | 'identifier' => $identifier, |
||
88 | 'body' => '', |
||
89 | 'layout_id' => '', |
||
90 | 'layout' => $layout, |
||
91 | ]; |
||
92 | |||
93 | $this->gridRepoMock->expects($this->once())->method('add'); |
||
94 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
95 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
96 | $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
||
97 | |||
98 | /** @var IStringerEntity|Entity $actualResult */ |
||
99 | $actualResult = $this->sut->create($postData, []); |
||
100 | |||
101 | $this->assertInstanceOf(Entity::class, $actualResult); |
||
102 | $this->assertEmpty($actualResult->getId()); |
||
103 | $this->assertSame($identifier, $actualResult->getIdentifier()); |
||
104 | } |
||
105 | |||
106 | public function testUpdate() |
||
107 | { |
||
108 | $id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb'; |
||
109 | |||
110 | $entity = $this->sut->createEntity($id); |
||
111 | |||
112 | $title = 'Bar'; |
||
113 | $identifier = 'bar'; |
||
114 | $layout = 'baz'; |
||
115 | $postData = [ |
||
116 | 'title' => $title, |
||
117 | 'identifier' => $identifier, |
||
118 | 'body' => '', |
||
119 | 'layout_id' => '', |
||
120 | 'layout' => $layout, |
||
121 | ]; |
||
122 | |||
123 | $this->gridRepoMock->expects($this->never())->method('add'); |
||
124 | $this->gridRepoMock->expects($this->never())->method('delete'); |
||
125 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
126 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
127 | $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
||
128 | |||
129 | $actualResult = $this->sut->update($entity, $postData, []); |
||
130 | |||
131 | $this->assertTrue($actualResult); |
||
132 | } |
||
133 | |||
134 | public function testUpdateThrowsExceptionWhenCalledWithWrongEntity() |
||
135 | { |
||
136 | $this->expectException(\InvalidArgumentException::class); |
||
137 | |||
138 | /** @var IStringerEntity|MockObject $entityStub */ |
||
139 | $entityStub = $this->createMock(IStringerEntity::class); |
||
140 | |||
141 | $this->sut->update($entityStub, [], []); |
||
142 | } |
||
143 | |||
144 | public function testDelete() |
||
145 | { |
||
146 | $id = 'foo'; |
||
147 | $entity = $this->sut->createEntity($id); |
||
148 | |||
149 | $this->gridRepoMock->expects($this->once())->method('delete'); |
||
150 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
151 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
152 | |||
153 | $actualResult = $this->sut->delete($entity); |
||
154 | |||
155 | $this->assertTrue($actualResult); |
||
156 | } |
||
157 | |||
158 | public function testRetrieveEntity() |
||
159 | { |
||
160 | $id = 'foo'; |
||
161 | $entity = $this->sut->createEntity($id); |
||
162 | |||
163 | $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity); |
||
164 | |||
165 | $actualResult = $this->sut->retrieveEntity($id); |
||
166 | |||
167 | $this->assertSame($entity, $actualResult); |
||
168 | } |
||
169 | |||
170 | public function testRetrieveList() |
||
171 | { |
||
172 | $offset = 0; |
||
173 | $limit = 2; |
||
174 | $orders = []; |
||
175 | $conditions = []; |
||
176 | $params = []; |
||
177 | |||
178 | $id0 = 'foo'; |
||
179 | $entity0 = $this->sut->createEntity($id0); |
||
180 | $id1 = 'bar'; |
||
181 | $entity1 = $this->sut->createEntity($id1); |
||
182 | $expectedResult = [$entity0, $entity1]; |
||
183 | |||
184 | $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult); |
||
185 | |||
186 | $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params); |
||
187 | |||
188 | $this->assertSame($expectedResult, $actualResult); |
||
189 | } |
||
190 | |||
191 | public function testValidateFormSuccess() |
||
192 | { |
||
193 | $postData = ['foo' => 'bar']; |
||
194 | |||
195 | $validatorMock = $this->createMock(IValidator::class); |
||
196 | $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true); |
||
197 | $validatorMock->expects($this->never())->method('getErrors'); |
||
198 | |||
199 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
200 | |||
201 | $result = $this->sut->validateForm($postData); |
||
202 | |||
203 | $this->assertSame([], $result); |
||
204 | } |
||
205 | |||
206 | public function testValidateFormFailure() |
||
207 | { |
||
208 | $postData = ['foo' => 'bar']; |
||
209 | |||
210 | $errorsStub = new ErrorCollection(); |
||
211 | $errorsStub['foo'] = ['foo error']; |
||
212 | |||
213 | $validatorMock = $this->createMock(IValidator::class); |
||
214 | $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false); |
||
215 | $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub); |
||
216 | |||
217 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
218 | |||
219 | $result = $this->sut->validateForm($postData); |
||
220 | |||
221 | $this->assertSame(['foo' => ['foo error']], $result); |
||
222 | } |
||
223 | |||
224 | public function testValidateCreatesOnlyOneValidator() |
||
225 | { |
||
226 | $postData = ['foo' => 'bar']; |
||
227 | |||
228 | $validatorMock = $this->createMock(IValidator::class); |
||
229 | $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true); |
||
230 | $validatorMock->expects($this->any())->method('getErrors'); |
||
231 | |||
232 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
233 | |||
234 | $firstRun = $this->sut->validateForm($postData); |
||
235 | $secondRun = $this->sut->validateForm($postData); |
||
236 | |||
237 | $this->assertSame($firstRun, $secondRun); |
||
238 | } |
||
239 | } |
||
240 |
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..