ContentListTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 136
c 1
b 0
f 0
dl 0
loc 238
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateFormSuccess() 0 13 1
A testValidateFormFailure() 0 16 1
A testRetrieveEntity() 0 10 1
A testCreateEntity() 0 8 1
A testValidateCreatesOnlyOneValidator() 0 14 1
A testCreate() 0 35 1
A testUpdate() 0 36 1
A setUp() 0 20 1
A testUpdateThrowsExceptionWhenCalledWithWrongEntity() 0 8 1
A testRetrieveList() 0 19 1
A testDelete() 0 12 1
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\ContentList as Entity;
9
use AbterPhp\Website\Orm\ContentListRepo as GridRepo;
10
use AbterPhp\Website\Validation\Factory\ContentList 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 ContentListTest extends TestCase
22
{
23
    /** @var ContentList - 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 ContentList(
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
        $name           = 'Bar';
83
        $identifier     = 'bar';
84
        $classes        = 'baz0 baz1';
85
        $protected      = '1';
86
        $withLinks      = '1';
87
        $withLabelLinks = '1';
88
        $withHtml       = '1';
89
        $withImages     = '1';
90
        $withClasses    = '1';
91
92
        $postData = [
93
            'name'             => $name,
94
            'identifier'       => $identifier,
95
            'classes'          => $classes,
96
            'protected'        => $protected,
97
            'with_links'       => $withLinks,
98
            'with_label_links' => $withLabelLinks,
99
            'with_html'        => $withHtml,
100
            'with_images'      => $withImages,
101
            'with_classes'     => $withClasses,
102
        ];
103
104
        $this->gridRepoMock->expects($this->once())->method('add');
105
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
106
        $this->unitOfWorkMock->expects($this->once())->method('commit');
107
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
108
109
        /** @var IStringerEntity|Entity $actualResult */
110
        $actualResult = $this->sut->create($postData, []);
111
112
        $this->assertInstanceOf(Entity::class, $actualResult);
113
        $this->assertEmpty($actualResult->getId());
114
        $this->assertSame($identifier, $actualResult->getIdentifier());
115
    }
116
117
    public function testUpdate()
118
    {
119
        $id             = '5c003d37-c59e-43eb-a471-e7b3c031fbeb';
120
        $name           = 'Bar';
121
        $identifier     = 'bar';
122
        $classes        = 'baz0 baz1';
123
        $protected      = '1';
124
        $withLinks      = '1';
125
        $withLabelLinks = '1';
126
        $withHtml       = '1';
127
        $withImages     = '1';
128
        $withClasses    = '1';
129
130
        $postData = [
131
            'name'             => $name,
132
            'identifier'       => $identifier,
133
            'classes'          => $classes,
134
            'protected'        => $protected,
135
            'with_links'       => $withLinks,
136
            'with_label_links' => $withLabelLinks,
137
            'with_html'        => $withHtml,
138
            'with_images'      => $withImages,
139
            'with_classes'     => $withClasses,
140
        ];
141
142
        $entity = $this->sut->createEntity($id);
143
144
        $this->gridRepoMock->expects($this->never())->method('add');
145
        $this->gridRepoMock->expects($this->never())->method('delete');
146
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
147
        $this->unitOfWorkMock->expects($this->once())->method('commit');
148
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
149
150
        $actualResult = $this->sut->update($entity, $postData, []);
151
152
        $this->assertTrue($actualResult);
153
    }
154
155
    public function testUpdateThrowsExceptionWhenCalledWithWrongEntity()
156
    {
157
        $this->expectException(\InvalidArgumentException::class);
158
159
        /** @var IStringerEntity|MockObject $entityStub */
160
        $entityStub = $this->createMock(IStringerEntity::class);
161
162
        $this->sut->update($entityStub, [], []);
163
    }
164
165
    public function testDelete()
166
    {
167
        $id     = 'foo';
168
        $entity = $this->sut->createEntity($id);
169
170
        $this->gridRepoMock->expects($this->once())->method('delete');
171
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
172
        $this->unitOfWorkMock->expects($this->once())->method('commit');
173
174
        $actualResult = $this->sut->delete($entity);
175
176
        $this->assertTrue($actualResult);
177
    }
178
179
    public function testRetrieveEntity()
180
    {
181
        $id     = 'foo';
182
        $entity = $this->sut->createEntity($id);
183
184
        $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity);
185
186
        $actualResult = $this->sut->retrieveEntity($id);
187
188
        $this->assertSame($entity, $actualResult);
189
    }
190
191
    public function testRetrieveList()
192
    {
193
        $offset     = 0;
194
        $limit      = 2;
195
        $orders     = [];
196
        $conditions = [];
197
        $params     = [];
198
199
        $id0            = 'foo';
200
        $entity0        = $this->sut->createEntity($id0);
201
        $id1            = 'bar';
202
        $entity1        = $this->sut->createEntity($id1);
203
        $expectedResult = [$entity0, $entity1];
204
205
        $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult);
206
207
        $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params);
208
209
        $this->assertSame($expectedResult, $actualResult);
210
    }
211
212
    public function testValidateFormSuccess()
213
    {
214
        $postData = ['foo' => 'bar'];
215
216
        $validatorMock = $this->createMock(IValidator::class);
217
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true);
218
        $validatorMock->expects($this->never())->method('getErrors');
219
220
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
221
222
        $result = $this->sut->validateForm($postData);
223
224
        $this->assertSame([], $result);
225
    }
226
227
    public function testValidateFormFailure()
228
    {
229
        $postData = ['foo' => 'bar'];
230
231
        $errorsStub        = new ErrorCollection();
232
        $errorsStub['foo'] = ['foo error'];
233
234
        $validatorMock = $this->createMock(IValidator::class);
235
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false);
236
        $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub);
237
238
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
239
240
        $result = $this->sut->validateForm($postData);
241
242
        $this->assertSame(['foo' => ['foo error']], $result);
243
    }
244
245
    public function testValidateCreatesOnlyOneValidator()
246
    {
247
        $postData = ['foo' => 'bar'];
248
249
        $validatorMock = $this->createMock(IValidator::class);
250
        $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true);
251
        $validatorMock->expects($this->any())->method('getErrors');
252
253
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
254
255
        $firstRun  = $this->sut->validateForm($postData);
256
        $secondRun = $this->sut->validateForm($postData);
257
258
        $this->assertSame($firstRun, $secondRun);
259
    }
260
}
261