Passed
Push — master ( 380517...f13cd3 )
by Peter
09:19
created

FileCategoryTest::testRetrieveEntity()   A

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\Files\Service\Execute;
6
7
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
8
use AbterPhp\Files\Orm\FileCategoryRepo as GridRepo;
9
use AbterPhp\Files\Validation\Factory\FileCategory 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 FileCategoryTest extends TestCase
20
{
21
    /** @var FileCategory - 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 FileCategory(
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
        $name       = 'Bar';
71
        $identifier = 'bar';
72
        $postData   = [
73
            'name'       => $name,
74
            'identifier' => $identifier,
75
        ];
76
77
        $this->gridRepoMock->expects($this->once())->method('add');
78
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
79
        $this->unitOfWorkMock->expects($this->once())->method('commit');
80
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
81
82
        /** @var IStringerEntity|Entity $actualResult */
83
        $actualResult = $this->sut->create($postData, []);
84
85
        $this->assertInstanceOf(Entity::class, $actualResult);
86
        $this->assertEmpty($actualResult->getId());
87
        $this->assertSame($identifier, $actualResult->getIdentifier());
88
    }
89
90
    public function testCreateWithUserGroups()
91
    {
92
        $name       = 'Bar';
93
        $identifier = 'bar';
94
        $ugId0      = '4567f007-6efa-4dae-b0af-795a3dbfe44e';
95
        $ugId1      = '66822f5c-e32c-434c-a220-552a41138653';
96
        $postData   = [
97
            'name'           => $name,
98
            'identifier'     => $identifier,
99
            'user_group_ids' => [$ugId0, $ugId1],
100
        ];
101
102
        $this->gridRepoMock->expects($this->once())->method('add');
103
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
104
        $this->unitOfWorkMock->expects($this->once())->method('commit');
105
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
106
107
        /** @var IStringerEntity|Entity $actualResult */
108
        $actualResult = $this->sut->create($postData, []);
109
110
        $this->assertInstanceOf(Entity::class, $actualResult);
111
        $this->assertEmpty($actualResult->getId());
112
        $this->assertSame($identifier, $actualResult->getIdentifier());
113
    }
114
115
    public function testUpdate()
116
    {
117
        $id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb';
118
119
        $entity = $this->sut->createEntity($id);
120
121
        $name       = 'Bar';
122
        $identifier = 'bar';
123
        $postData   = [
124
            'name'       => $name,
125
            'identifier' => $identifier,
126
        ];
127
128
        $this->gridRepoMock->expects($this->never())->method('add');
129
        $this->gridRepoMock->expects($this->never())->method('delete');
130
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
131
        $this->unitOfWorkMock->expects($this->once())->method('commit');
132
        $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0);
133
134
        $actualResult = $this->sut->update($entity, $postData, []);
135
136
        $this->assertTrue($actualResult);
137
    }
138
139
    public function testUpdateThrowsExceptionWhenCalledWithWrongEntity()
140
    {
141
        $this->expectException(\InvalidArgumentException::class);
142
143
        /** @var IStringerEntity|MockObject $entityStub */
144
        $entityStub = $this->createMock(IStringerEntity::class);
145
146
        $this->sut->update($entityStub, [], []);
147
    }
148
149
    public function testDelete()
150
    {
151
        $id     = 'foo';
152
        $entity = $this->sut->createEntity($id);
153
154
        $this->gridRepoMock->expects($this->once())->method('delete');
155
        $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch');
156
        $this->unitOfWorkMock->expects($this->once())->method('commit');
157
158
        $actualResult = $this->sut->delete($entity);
159
160
        $this->assertTrue($actualResult);
161
    }
162
163
    public function testRetrieveEntity()
164
    {
165
        $id     = 'foo';
166
        $entity = $this->sut->createEntity($id);
167
168
        $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity);
169
170
        $actualResult = $this->sut->retrieveEntity($id);
171
172
        $this->assertSame($entity, $actualResult);
173
    }
174
175
    public function testRetrieveList()
176
    {
177
        $offset     = 0;
178
        $limit      = 2;
179
        $orders     = [];
180
        $conditions = [];
181
        $params     = [];
182
183
        $id0            = 'foo';
184
        $entity0        = $this->sut->createEntity($id0);
185
        $id1            = 'bar';
186
        $entity1        = $this->sut->createEntity($id1);
187
        $expectedResult = [$entity0, $entity1];
188
189
        $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult);
190
191
        $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params);
192
193
        $this->assertSame($expectedResult, $actualResult);
194
    }
195
196
    public function testValidateFormSuccess()
197
    {
198
        $postData = ['foo' => 'bar'];
199
200
        $validatorMock = $this->createMock(IValidator::class);
201
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true);
202
        $validatorMock->expects($this->never())->method('getErrors');
203
204
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
205
206
        $result = $this->sut->validateForm($postData);
207
208
        $this->assertSame([], $result);
209
    }
210
211
    public function testValidateFormFailure()
212
    {
213
        $postData = ['foo' => 'bar'];
214
215
        $errorsStub        = new ErrorCollection();
216
        $errorsStub['foo'] = ['foo error'];
217
218
        $validatorMock = $this->createMock(IValidator::class);
219
        $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false);
220
        $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub);
221
222
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
223
224
        $result = $this->sut->validateForm($postData);
225
226
        $this->assertSame(['foo' => ['foo error']], $result);
227
    }
228
229
    public function testValidateCreatesOnlyOneValidator()
230
    {
231
        $postData = ['foo' => 'bar'];
232
233
        $validatorMock = $this->createMock(IValidator::class);
234
        $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true);
235
        $validatorMock->expects($this->any())->method('getErrors');
236
237
        $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock);
238
239
        $firstRun  = $this->sut->validateForm($postData);
240
        $secondRun = $this->sut->validateForm($postData);
241
242
        $this->assertSame($firstRun, $secondRun);
243
    }
244
}
245