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