FormTest::testUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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