PageCategoryTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 105
c 2
b 0
f 0
dl 0
loc 202
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateFormSuccess() 0 13 1
A testDelete() 0 12 1
A testRetrieveList() 0 19 1
A testCreate() 0 23 1
A testUpdateThrowsExceptionWhenCalledWithWrongEntity() 0 8 1
A testValidateFormFailure() 0 16 1
A testRetrieveEntity() 0 10 1
A testUpdate() 0 22 1
A testValidateCreatesOnlyOneValidator() 0 14 1
A testCreateEntity() 0 8 1
A setUp() 0 16 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\PageCategory as Entity;
9
use AbterPhp\Website\Orm\PageCategoryRepo as GridRepo;
10
use AbterPhp\Website\Validation\Factory\PageCategory as ValidatorFactory;
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 PageCategoryTest extends TestCase
20
{
21
    /** @var PageCategory - 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 PageCategory(
50
            $this->gridRepoMock,
51
            $this->validatorFactoryMock,
52
            $this->unitOfWorkMock,
53
            $this->eventDispatcherMock,
54
            $this->slugifyMock
55
        );
56
    }
57
58
    public function testCreateEntity()
59
    {
60
        $id = 'foo';
61
62
        $actualResult = $this->sut->createEntity($id);
63
64
        $this->assertInstanceOf(Entity::class, $actualResult);
65
        $this->assertSame($id, $actualResult->getId());
66
    }
67
68
    public function testCreate()
69
    {
70
        $identifier = 'foo';
71
        $name       = 'bar';
72
        $ugId0      = '2b54ef5e-39b6-4422-81be-e6c147779753';
73
        $ugId1      = '83e69b07-9a64-4db3-aaa7-28e28a4c918d';
74
        $postData   = [
75
            'identifier'     => $identifier,
76
            'name'           => $name,
77
            'user_group_ids' => [$ugId0, $ugId1],
78
        ];
79
80
        $this->gridRepoMock->expects($this->once())->method('add');
81
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
82
        $this->unitOfWorkMock->expects($this->once())->method('commit');
83
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
84
85
        /** @var IStringerEntity|Entity $actualResult */
86
        $actualResult = $this->sut->create($postData, []);
87
88
        $this->assertInstanceOf(Entity::class, $actualResult);
89
        $this->assertEmpty($actualResult->getId());
90
        $this->assertSame($identifier, $actualResult->getIdentifier());
91
    }
92
93
    public function testUpdate()
94
    {
95
        $id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb';
96
97
        $entity = $this->sut->createEntity($id);
98
99
        $identifier = 'foo';
100
        $name       = 'bar';
101
        $postData   = [
102
            'identifier' => $identifier,
103
            'name'       => $name,
104
        ];
105
106
        $this->gridRepoMock->expects($this->never())->method('add');
107
        $this->gridRepoMock->expects($this->never())->method('delete');
108
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
109
        $this->unitOfWorkMock->expects($this->once())->method('commit');
110
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
111
112
        $actualResult = $this->sut->update($entity, $postData, []);
113
114
        $this->assertTrue($actualResult);
115
    }
116
117
    public function testUpdateThrowsExceptionWhenCalledWithWrongEntity()
118
    {
119
        $this->expectException(\InvalidArgumentException::class);
120
121
        /** @var IStringerEntity|MockObject $entityStub */
122
        $entityStub = $this->createMock(IStringerEntity::class);
123
124
        $this->sut->update($entityStub, [], []);
125
    }
126
127
    public function testDelete()
128
    {
129
        $id     = 'foo';
130
        $entity = $this->sut->createEntity($id);
131
132
        $this->gridRepoMock->expects($this->once())->method('delete');
133
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
134
        $this->unitOfWorkMock->expects($this->once())->method('commit');
135
136
        $actualResult = $this->sut->delete($entity);
137
138
        $this->assertTrue($actualResult);
139
    }
140
141
    public function testRetrieveEntity()
142
    {
143
        $id     = 'foo';
144
        $entity = $this->sut->createEntity($id);
145
146
        $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity);
147
148
        $actualResult = $this->sut->retrieveEntity($id);
149
150
        $this->assertSame($entity, $actualResult);
151
    }
152
153
    public function testRetrieveList()
154
    {
155
        $offset     = 0;
156
        $limit      = 2;
157
        $orders     = [];
158
        $conditions = [];
159
        $params     = [];
160
161
        $id0            = 'foo';
162
        $entity0        = $this->sut->createEntity($id0);
163
        $id1            = 'bar';
164
        $entity1        = $this->sut->createEntity($id1);
165
        $expectedResult = [$entity0, $entity1];
166
167
        $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult);
168
169
        $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params);
170
171
        $this->assertSame($expectedResult, $actualResult);
172
    }
173
174
    public function testValidateFormSuccess()
175
    {
176
        $postData = ['foo' => 'bar'];
177
178
        $validatorMock = $this->createMock(IValidator::class);
179
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true);
180
        $validatorMock->expects($this->never())->method('getErrors');
181
182
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
183
184
        $result = $this->sut->validateForm($postData);
185
186
        $this->assertSame([], $result);
187
    }
188
189
    public function testValidateFormFailure()
190
    {
191
        $postData = ['foo' => 'bar'];
192
193
        $errorsStub        = new ErrorCollection();
194
        $errorsStub['foo'] = ['foo error'];
195
196
        $validatorMock = $this->createMock(IValidator::class);
197
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false);
198
        $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub);
199
200
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
201
202
        $result = $this->sut->validateForm($postData);
203
204
        $this->assertSame(['foo' => ['foo error']], $result);
205
    }
206
207
    public function testValidateCreatesOnlyOneValidator()
208
    {
209
        $postData = ['foo' => 'bar'];
210
211
        $validatorMock = $this->createMock(IValidator::class);
212
        $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true);
213
        $validatorMock->expects($this->any())->method('getErrors');
214
215
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
216
217
        $firstRun  = $this->sut->validateForm($postData);
218
        $secondRun = $this->sut->validateForm($postData);
219
220
        $this->assertSame($firstRun, $secondRun);
221
    }
222
}
223