1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AbterPhp\Contact\Service\Execute; |
||
6 | |||
7 | use AbterPhp\Contact\Domain\Entities\Form; |
||
0 ignored issues
–
show
|
|||
8 | use AbterPhp\Contact\Domain\Entities\Message as Entity; |
||
9 | use AbterPhp\Contact\Orm\FormRepo as GridRepo; |
||
10 | use AbterPhp\Contact\Validation\Factory\Message as ValidatorFactory; |
||
11 | use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
||
12 | use AbterPhp\Framework\Email\Sender; |
||
13 | use AbterPhp\Framework\I18n\ITranslator; |
||
14 | use Opulence\Events\Dispatchers\IEventDispatcher; |
||
15 | use Opulence\Orm\OrmException; |
||
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 MessageTest extends TestCase |
||
22 | { |
||
23 | /** @var Message - 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 IEventDispatcher|MockObject */ |
||
33 | protected $eventDispatcherMock; |
||
34 | |||
35 | /** @var Sender|MockObject */ |
||
36 | protected $senderMock; |
||
37 | |||
38 | /** @var ITranslator|MockObject */ |
||
39 | protected $translatorMock; |
||
40 | |||
41 | public function setUp(): void |
||
42 | { |
||
43 | parent::setUp(); |
||
44 | |||
45 | $this->gridRepoMock = $this->createMock(GridRepo::class); |
||
46 | $this->validatorFactoryMock = $this->createMock(ValidatorFactory::class); |
||
47 | $this->eventDispatcherMock = $this->createMock(IEventDispatcher::class); |
||
48 | $this->senderMock = $this->createMock(Sender::class); |
||
49 | $this->translatorMock = $this->createMock(ITranslator::class); |
||
50 | |||
51 | $this->sut = new Message( |
||
52 | $this->gridRepoMock, |
||
53 | $this->validatorFactoryMock, |
||
54 | $this->eventDispatcherMock, |
||
55 | $this->senderMock, |
||
56 | $this->translatorMock |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | public function testCreateEntity() |
||
61 | { |
||
62 | $id = 'foo'; |
||
63 | |||
64 | $actualResult = $this->sut->createEntity($id); |
||
65 | |||
66 | $this->assertInstanceOf(Entity::class, $actualResult); |
||
67 | $this->assertSame($id, $actualResult->getId()); |
||
68 | } |
||
69 | |||
70 | public function testGetFormGetsFormByIdentifierByDefault() |
||
71 | { |
||
72 | $formIdentifier = 'foo'; |
||
73 | |||
74 | $formStub = $this->createMock(Form::class); |
||
75 | |||
76 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willReturn($formStub); |
||
77 | $this->gridRepoMock->expects($this->never())->method('getById'); |
||
78 | |||
79 | /** @var Form $actualResult */ |
||
80 | $actualResult = $this->sut->getForm($formIdentifier); |
||
81 | |||
82 | $this->assertSame($formStub, $actualResult); |
||
83 | } |
||
84 | |||
85 | public function testGetFormGetsFormByIdentifierFromCacheTheSecondTime() |
||
86 | { |
||
87 | $formIdentifier = 'foo'; |
||
88 | |||
89 | $formStub = $this->createMock(Form::class); |
||
90 | $formStub->expects($this->any())->method('getIdentifier')->willReturn($formIdentifier); |
||
91 | |||
92 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willReturn($formStub); |
||
93 | $this->gridRepoMock->expects($this->never())->method('getById'); |
||
94 | |||
95 | /** @var Form $actualResult */ |
||
96 | $actualResult1 = $this->sut->getForm($formIdentifier); |
||
97 | $actualResult2 = $this->sut->getForm($formIdentifier); |
||
98 | |||
99 | $this->assertSame($actualResult1, $actualResult2); |
||
100 | $this->assertSame($formStub, $actualResult1); |
||
101 | } |
||
102 | |||
103 | public function testGetFormGetsFormByIdByIfByIdentifierNothingFound() |
||
104 | { |
||
105 | $formIdentifier = 'foo'; |
||
106 | |||
107 | $formStub = $this->createMock(Form::class); |
||
108 | |||
109 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willThrowException(new OrmException()); |
||
110 | $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($formStub); |
||
111 | |||
112 | /** @var Form $actualResult */ |
||
113 | $actualResult = $this->sut->getForm($formIdentifier); |
||
114 | |||
115 | $this->assertSame($formStub, $actualResult); |
||
116 | } |
||
117 | |||
118 | public function testGetFormCanReturnNull() |
||
119 | { |
||
120 | $formIdentifier = 'foo'; |
||
121 | |||
122 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willThrowException(new OrmException()); |
||
123 | $this->gridRepoMock->expects($this->once())->method('getById')->willThrowException(new OrmException()); |
||
124 | |||
125 | /** @var Form $actualResult */ |
||
126 | $actualResult = $this->sut->getForm($formIdentifier); |
||
127 | |||
128 | $this->assertNull($actualResult); |
||
129 | } |
||
130 | |||
131 | public function testSend() |
||
132 | { |
||
133 | $expectedResult = 13; |
||
134 | |||
135 | /** @var Entity|MockObject $entityStub */ |
||
136 | $entityStub = $this->createMock(Entity::class); |
||
137 | |||
138 | $this->senderMock |
||
139 | ->expects($this->once()) |
||
140 | ->method('send') |
||
141 | ->willReturn($expectedResult); |
||
142 | |||
143 | /** @var Form $actualResult */ |
||
144 | $actualResult = $this->sut->send($entityStub); |
||
145 | |||
146 | $this->assertSame($expectedResult, $actualResult); |
||
147 | } |
||
148 | |||
149 | public function testGetFailedRecipients() |
||
150 | { |
||
151 | $expectedResult = ['foo' => 'bar']; |
||
152 | |||
153 | $this->senderMock |
||
154 | ->expects($this->once()) |
||
155 | ->method('getFailedRecipients') |
||
156 | ->willReturn($expectedResult); |
||
157 | |||
158 | /** @var Form $actualResult */ |
||
159 | $actualResult = $this->sut->getFailedRecipients(); |
||
160 | |||
161 | $this->assertSame($expectedResult, $actualResult); |
||
162 | } |
||
163 | |||
164 | public function testValidateFormReturnsEmptyArrayIfValid() |
||
165 | { |
||
166 | $formIdentifier = 'foo'; |
||
167 | $maxBodyLength = 128; |
||
168 | |||
169 | $postData = [ |
||
170 | 'foo' => 'bar', |
||
171 | ]; |
||
172 | |||
173 | $formStub = $this->createMock(Form::class); |
||
174 | $formStub->expects($this->any())->method('getMaxBodyLength')->willReturn($maxBodyLength); |
||
175 | |||
176 | $this->gridRepoMock->expects($this->any())->method('getByIdentifier')->willReturn($formStub); |
||
177 | |||
178 | $validatorMock = $this->createMock(IValidator::class); |
||
179 | $validatorMock->expects($this->any())->method('isValid')->willReturn(true); |
||
180 | |||
181 | $this->validatorFactoryMock->expects($this->any())->method('setMaxBodyLength')->willReturnSelf(); |
||
182 | $this->validatorFactoryMock->expects($this->any())->method('createValidator')->willReturn($validatorMock); |
||
183 | |||
184 | /** @var Form $actualResult */ |
||
185 | $actualResult = $this->sut->validateForm($formIdentifier, $postData); |
||
186 | |||
187 | $this->assertSame([], $actualResult); |
||
188 | } |
||
189 | |||
190 | public function testValidateFormReturnsErrorsIfAny() |
||
191 | { |
||
192 | $formIdentifier = 'foo'; |
||
193 | $maxBodyLength = 128; |
||
194 | $errors = [ |
||
195 | 'bar' => 'baz', |
||
196 | ]; |
||
197 | |||
198 | $postData = [ |
||
199 | 'foo' => 'bar', |
||
200 | ]; |
||
201 | |||
202 | $formStub = $this->createMock(Form::class); |
||
203 | $formStub->expects($this->any())->method('getMaxBodyLength')->willReturn($maxBodyLength); |
||
204 | |||
205 | $this->gridRepoMock->expects($this->any())->method('getByIdentifier')->willReturn($formStub); |
||
206 | |||
207 | $errorsMock = $this->createMock(ErrorCollection::class); |
||
208 | $errorsMock->expects($this->any())->method('getAll')->willReturn($errors); |
||
209 | |||
210 | $validatorMock = $this->createMock(IValidator::class); |
||
211 | $validatorMock->expects($this->any())->method('isValid')->willReturn(false); |
||
212 | $validatorMock->expects($this->any())->method('getErrors')->willReturn($errorsMock); |
||
213 | |||
214 | $this->validatorFactoryMock->expects($this->any())->method('setMaxBodyLength')->willReturnSelf(); |
||
215 | $this->validatorFactoryMock->expects($this->any())->method('createValidator')->willReturn($validatorMock); |
||
216 | |||
217 | /** @var Form $actualResult */ |
||
218 | $actualResult = $this->sut->validateForm($formIdentifier, $postData); |
||
219 | |||
220 | $this->assertSame($errors, $actualResult); |
||
221 | } |
||
222 | |||
223 | public function testValidateFormSetsMaxBodyLength() |
||
224 | { |
||
225 | $formIdentifier = 'foo'; |
||
226 | $maxBodyLength = 128; |
||
227 | |||
228 | $postData = [ |
||
229 | 'foo' => 'bar', |
||
230 | ]; |
||
231 | |||
232 | $formStub = $this->createMock(Form::class); |
||
233 | $formStub->expects($this->any())->method('getMaxBodyLength')->willReturn($maxBodyLength); |
||
234 | |||
235 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willReturn($formStub); |
||
236 | |||
237 | $validatorMock = $this->createMock(IValidator::class); |
||
238 | $validatorMock |
||
239 | ->expects($this->once()) |
||
240 | ->method('isValid') |
||
241 | ->with($postData) |
||
242 | ->willReturn(true); |
||
243 | |||
244 | $this->validatorFactoryMock |
||
245 | ->expects($this->once()) |
||
246 | ->method('setMaxBodyLength') |
||
247 | ->with($maxBodyLength) |
||
248 | ->willReturnSelf(); |
||
249 | $this->validatorFactoryMock |
||
250 | ->expects($this->once()) |
||
251 | ->method('createValidator') |
||
252 | ->willReturn($validatorMock); |
||
253 | |||
254 | /** @var Form $actualResult */ |
||
255 | $actualResult = $this->sut->validateForm($formIdentifier, $postData); |
||
256 | |||
257 | $this->assertSame([], $actualResult); |
||
258 | } |
||
259 | |||
260 | public function testValidateThrowsExceptionIfEntityIsNotFound() |
||
261 | { |
||
262 | $this->expectException(\InvalidArgumentException::class); |
||
263 | |||
264 | $formIdentifier = 'foo'; |
||
265 | $maxBodyLength = 128; |
||
266 | |||
267 | $postData = [ |
||
268 | 'foo' => 'bar', |
||
269 | ]; |
||
270 | |||
271 | $formStub = $this->createMock(Form::class); |
||
272 | $formStub->expects($this->any())->method('getMaxBodyLength')->willReturn($maxBodyLength); |
||
273 | |||
274 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willThrowException(new OrmException()); |
||
275 | $this->gridRepoMock->expects($this->once())->method('getById')->willThrowException(new OrmException()); |
||
276 | |||
277 | $validatorMock = $this->createMock(IValidator::class); |
||
278 | $validatorMock->expects($this->never())->method('isValid'); |
||
279 | |||
280 | $this->sut->validateForm($formIdentifier, $postData); |
||
281 | } |
||
282 | |||
283 | public function testFillEntityThrowsExceptionOnWrongEntity() |
||
284 | { |
||
285 | $this->expectException(\InvalidArgumentException::class); |
||
286 | |||
287 | $formIdentifier = 'foo'; |
||
288 | $entityMock = $this->createMock(IStringerEntity::class); |
||
289 | $postData = []; |
||
290 | |||
291 | $this->sut->fillEntity($formIdentifier, $entityMock, $postData, []); |
||
292 | } |
||
293 | |||
294 | public function testFillEntityDoesNotChangeEntityIfFormIsNotFound() |
||
295 | { |
||
296 | $formIdentifier = 'foo'; |
||
297 | $entityMock = $this->createMock(Entity::class); |
||
298 | $postData = []; |
||
299 | |||
300 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willThrowException(new OrmException()); |
||
301 | $this->gridRepoMock->expects($this->once())->method('getById')->willThrowException(new OrmException()); |
||
302 | |||
303 | $entityMock->expects($this->never())->method('setForm'); |
||
304 | |||
305 | $this->sut->fillEntity($formIdentifier, $entityMock, $postData, []); |
||
306 | } |
||
307 | |||
308 | public function testFillEntity() |
||
309 | { |
||
310 | $formIdentifier = 'foo'; |
||
311 | $subject = 'bar'; |
||
312 | $body = 'baz'; |
||
313 | $fromName = 'Qux'; |
||
314 | $fromEmail = '[email protected]'; |
||
315 | $fromPhone = '32 234 4567'; |
||
316 | |||
317 | $postData = [ |
||
318 | 'subject' => $subject, |
||
319 | 'body' => $body, |
||
320 | 'from_name' => $fromName, |
||
321 | 'from_email' => $fromEmail, |
||
322 | 'from_phone' => $fromPhone, |
||
323 | ]; |
||
324 | |||
325 | $entityMock = $this->createMock(Entity::class); |
||
326 | |||
327 | $formStub = $this->createMock(Form::class); |
||
328 | |||
329 | $this->gridRepoMock->expects($this->once())->method('getByIdentifier')->willThrowException(new OrmException()); |
||
330 | $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($formStub); |
||
331 | |||
332 | $entityMock->expects($this->once())->method('setForm'); |
||
333 | |||
334 | $actualResult = $this->sut->fillEntity($formIdentifier, $entityMock, $postData, []); |
||
335 | |||
336 | $this->assertSame($entityMock, $actualResult); |
||
337 | } |
||
338 | } |
||
339 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: