PageTest::testRetrieveEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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