testValidateCreatesOnlyOneValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup as Entity;
8
use AbterPhp\Admin\Orm\UserGroupRepo as GridRepo;
9
use AbterPhp\Admin\Validation\Factory\UserGroup 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 UserGroupTest extends TestCase
20
{
21
    /** @var UserGroup - 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->sut = new UserGroup(
50
            $this->gridRepoMock,
51
            $this->validatorFactoryMock,
52
            $this->unitOfWorkMock,
53
            $this->eventDispatcherMock,
54
            $this->slugifyMock
55
        );
56
    }
57
58
    public function testCreate()
59
    {
60
        $name       = 'Bar 123';
61
        $identifier = 'bar-123';
62
        $postData   = [
63
            'name' => $name,
64
        ];
65
66
        $this->gridRepoMock->expects($this->once())->method('add');
67
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
68
        $this->unitOfWorkMock->expects($this->once())->method('commit');
69
        $this->slugifyMock->expects($this->any())->method('slugify')->with($name)->willReturn($identifier);
70
71
        /** @var IStringerEntity|Entity $actualResult */
72
        $actualResult = $this->sut->create($postData, []);
73
74
        $this->assertInstanceOf(Entity::class, $actualResult);
75
        $this->assertEmpty($actualResult->getId());
76
        $this->assertSame($identifier, $actualResult->getIdentifier());
77
        $this->assertSame($name, $actualResult->getName());
78
    }
79
80
    public function testCreateWithResources()
81
    {
82
        $name       = 'Bar 123';
83
        $identifier = 'bar-123';
84
        $postData   = [
85
            'name'               => $name,
86
            'admin_resource_ids' => ['ccbe55db-2f8d-4fd0-a74e-31fe8111ab3c', '5302a228-9d4a-4167-8c84-d60404d1247e'],
87
        ];
88
89
        $this->gridRepoMock->expects($this->once())->method('add');
90
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
91
        $this->unitOfWorkMock->expects($this->once())->method('commit');
92
        $this->slugifyMock->expects($this->any())->method('slugify')->with($name)->willReturn($identifier);
93
94
        /** @var IStringerEntity|Entity $actualResult */
95
        $actualResult = $this->sut->create($postData, []);
96
97
        $this->assertInstanceOf(Entity::class, $actualResult);
98
        $this->assertEmpty($actualResult->getId());
99
        $this->assertSame($identifier, $actualResult->getIdentifier());
100
        $this->assertSame($name, $actualResult->getName());
101
        $this->assertCount(2, $actualResult->getAdminResources());
102
    }
103
104
    public function testUpdate()
105
    {
106
        $id     = 'foo';
107
        $entity = $this->sut->createEntity($id);
108
109
        $name       = 'Bar 123';
110
        $identifier = 'bar-123';
111
        $postData   = [
112
            'name' => $name,
113
        ];
114
115
        $this->gridRepoMock->expects($this->never())->method('add');
116
        $this->gridRepoMock->expects($this->never())->method('delete');
117
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
118
        $this->unitOfWorkMock->expects($this->once())->method('commit');
119
        $this->slugifyMock->expects($this->any())->method('slugify')->with($name)->willReturn($identifier);
120
121
        $actualResult = $this->sut->update($entity, $postData, []);
122
123
        $this->assertTrue($actualResult);
124
        $this->assertSame($id, $entity->getId());
125
        $this->assertSame($identifier, $entity->getIdentifier());
126
        $this->assertSame($name, $entity->getName());
127
    }
128
129
    public function testUpdateThrowsExceptionWhenCalledWithWrongEntity()
130
    {
131
        $this->expectException(\InvalidArgumentException::class);
132
133
        /** @var IStringerEntity|MockObject $entityStub */
134
        $entityStub = $this->createMock(IStringerEntity::class);
135
136
        $this->sut->update($entityStub, [], []);
137
    }
138
139
    public function testDelete()
140
    {
141
        $id     = 'foo';
142
        $entity = $this->sut->createEntity($id);
143
144
        $this->gridRepoMock->expects($this->once())->method('delete');
145
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
146
        $this->unitOfWorkMock->expects($this->once())->method('commit');
147
148
        $actualResult = $this->sut->delete($entity);
149
150
        $this->assertTrue($actualResult);
151
    }
152
153
    public function testRetrieveEntity()
154
    {
155
        $id     = 'foo';
156
        $entity = $this->sut->createEntity($id);
157
158
        $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity);
159
160
        $actualResult = $this->sut->retrieveEntity($id);
161
162
        $this->assertSame($entity, $actualResult);
163
    }
164
165
    public function testRetrieveList()
166
    {
167
        $offset     = 0;
168
        $limit      = 2;
169
        $orders     = [];
170
        $conditions = [];
171
        $params     = [];
172
173
        $id0            = 'foo';
174
        $entity0        = $this->sut->createEntity($id0);
175
        $id1            = 'bar';
176
        $entity1        = $this->sut->createEntity($id1);
177
        $expectedResult = [$entity0, $entity1];
178
179
        $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult);
180
181
        $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params);
182
183
        $this->assertSame($expectedResult, $actualResult);
184
    }
185
186
    public function testValidateFormSuccess()
187
    {
188
        $postData = ['foo' => 'bar'];
189
190
        $validatorMock = $this->createMock(IValidator::class);
191
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true);
192
        $validatorMock->expects($this->never())->method('getErrors');
193
194
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
195
196
        $result = $this->sut->validateForm($postData);
197
198
        $this->assertSame([], $result);
199
    }
200
201
    public function testValidateFormFailure()
202
    {
203
        $postData = ['foo' => 'bar'];
204
205
        $errorsStub        = new ErrorCollection();
206
        $errorsStub['foo'] = ['foo error'];
207
208
        $validatorMock = $this->createMock(IValidator::class);
209
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false);
210
        $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub);
211
212
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
213
214
        $result = $this->sut->validateForm($postData);
215
216
        $this->assertSame(['foo' => ['foo error']], $result);
217
    }
218
219
    public function testValidateCreatesOnlyOneValidator()
220
    {
221
        $postData = ['foo' => 'bar'];
222
223
        $validatorMock = $this->createMock(IValidator::class);
224
        $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true);
225
        $validatorMock->expects($this->any())->method('getErrors');
226
227
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
228
229
        $firstRun  = $this->sut->validateForm($postData);
230
        $secondRun = $this->sut->validateForm($postData);
231
232
        $this->assertSame($firstRun, $secondRun);
233
    }
234
}
235