FormSqlDataMapperTest::testGetAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 36
rs 9.488
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Orm\DataMappers;
6
7
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase;
8
use AbterPhp\Contact\Domain\Entities\Form;
9
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
10
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory;
11
use PHPUnit\Framework\MockObject\MockObject;
12
13
class FormSqlDataMapperTest extends DataMapperTestCase
14
{
15
    /** @var FormSqlDataMapper - System Under Test */
16
    protected $sut;
17
18
    public function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $this->sut = new FormSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
23
    }
24
25
    public function testAdd()
26
    {
27
        $nextId        = 'c2883287-ae5d-42d1-ab0c-7d3da2846452';
28
        $identifier    = 'foo';
29
        $name          = 'bar';
30
        $toName        = 'Baz';
31
        $toEmail       = '[email protected]';
32
        $successUrl    = 'https://example.com/success';
33
        $failureUrl    = 'https://failure.example.com/';
34
        $maxBodyLength = 16;
35
36
        $sql0       = 'INSERT INTO contact_forms (id, name, identifier, to_name, to_email, success_url, failure_url, max_body_length) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore
37
        $values     = [
38
            [$nextId, \PDO::PARAM_STR],
39
            [$name, \PDO::PARAM_STR],
40
            [$identifier, \PDO::PARAM_STR],
41
            [$toName, \PDO::PARAM_STR],
42
            [$toEmail, \PDO::PARAM_STR],
43
            [$successUrl, \PDO::PARAM_STR],
44
            [$failureUrl, \PDO::PARAM_STR],
45
            [$maxBodyLength, \PDO::PARAM_INT],
46
        ];
47
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
48
49
        $this->writeConnectionMock
50
            ->expects($this->once())
51
            ->method('prepare')
52
            ->with($sql0)
53
            ->willReturn($statement0);
54
55
        $entity = new Form($nextId, $name, $identifier, $toName, $toEmail, $successUrl, $failureUrl, $maxBodyLength);
56
57
        $this->sut->add($entity);
58
59
        $this->assertSame($nextId, $entity->getId());
60
    }
61
62
    public function testDelete()
63
    {
64
        $id            = 'd23a94ed-b75c-43a9-9987-a783183dadd5';
65
        $identifier    = 'foo';
66
        $name          = 'bar';
67
        $toName        = 'Baz';
68
        $toEmail       = '[email protected]';
69
        $successUrl    = 'https://example.com/success';
70
        $failureUrl    = 'https://failure.example.com/';
71
        $maxBodyLength = 16;
72
73
        $sql0       = 'UPDATE contact_forms AS contact_forms SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
74
        $values     = [[$id, \PDO::PARAM_STR]];
75
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
76
77
        $this->writeConnectionMock
78
            ->expects($this->once())
79
            ->method('prepare')
80
            ->with($sql0)
81
            ->willReturn($statement0);
82
83
        $entity = new Form($id, $name, $identifier, $toName, $toEmail, $successUrl, $failureUrl, $maxBodyLength);
84
85
        $this->sut->delete($entity);
86
    }
87
88
    public function testGetAll()
89
    {
90
        $id            = '40a59d7d-7550-4b16-a90b-89adbfec8979';
91
        $identifier    = 'foo';
92
        $name          = 'bar';
93
        $toName        = 'Baz';
94
        $toEmail       = '[email protected]';
95
        $successUrl    = 'https://example.com/success';
96
        $failureUrl    = 'https://failure.example.com/';
97
        $maxBodyLength = 16;
98
99
        $sql0         = 'SELECT cf.id, cf.name, cf.identifier, cf.to_name, cf.to_email, cf.success_url, cf.failure_url, cf.max_body_length FROM contact_forms AS cf WHERE (cf.deleted_at IS NULL)'; // phpcs:ignore
100
        $values       = [];
101
        $expectedData = [
102
            [
103
                'id'              => $id,
104
                'identifier'      => $identifier,
105
                'name'            => $name,
106
                'to_name'         => $toName,
107
                'to_email'        => $toEmail,
108
                'success_url'     => $successUrl,
109
                'failure_url'     => $failureUrl,
110
                'max_body_length' => $maxBodyLength,
111
            ],
112
        ];
113
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
114
115
        $this->readConnectionMock
116
            ->expects($this->once())
117
            ->method('prepare')
118
            ->with($sql0)
119
            ->willReturn($statement0);
120
121
        $actualResult = $this->sut->getAll();
122
123
        $this->assertCollection($expectedData, $actualResult);
124
    }
125
126
    public function testGetPage()
127
    {
128
        $id            = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0';
129
        $identifier    = 'foo';
130
        $name          = 'bar';
131
        $toName        = 'Baz';
132
        $toEmail       = '[email protected]';
133
        $successUrl    = 'https://example.com/success';
134
        $failureUrl    = 'https://failure.example.com/';
135
        $maxBodyLength = 16;
136
137
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS cf.id, cf.name, cf.identifier, cf.to_name, cf.to_email, cf.success_url, cf.failure_url, cf.max_body_length FROM contact_forms AS cf WHERE (cf.deleted_at IS NULL) ORDER BY cf.name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
138
        $values       = [];
139
        $expectedData = [
140
            [
141
                'id'              => $id,
142
                'identifier'      => $identifier,
143
                'name'            => $name,
144
                'to_name'         => $toName,
145
                'to_email'        => $toEmail,
146
                'success_url'     => $successUrl,
147
                'failure_url'     => $failureUrl,
148
                'max_body_length' => $maxBodyLength,
149
            ],
150
        ];
151
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
152
153
        $this->readConnectionMock
154
            ->expects($this->once())
155
            ->method('prepare')
156
            ->with($sql0)
157
            ->willReturn($statement0);
158
159
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
160
161
        $this->assertCollection($expectedData, $actualResult);
162
    }
163
164
    public function testGetPageWithOrdersAndConditions()
165
    {
166
        $id            = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0';
167
        $identifier    = 'foo';
168
        $name          = 'bar';
169
        $toName        = 'Baz';
170
        $toEmail       = '[email protected]';
171
        $successUrl    = 'https://example.com/success';
172
        $failureUrl    = 'https://failure.example.com/';
173
        $maxBodyLength = 16;
174
175
        $orders     = ['block_layouts.identifier ASC'];
176
        $conditions = ['block_layouts.identifier LIKE \'abc%\'', 'block_layouts.identifier LIKE \'%bca\''];
177
178
        $sql0         = "SELECT SQL_CALC_FOUND_ROWS cf.id, cf.name, cf.identifier, cf.to_name, cf.to_email, cf.success_url, cf.failure_url, cf.max_body_length FROM contact_forms AS cf WHERE (cf.deleted_at IS NULL) AND (block_layouts.identifier LIKE 'abc%') AND (block_layouts.identifier LIKE '%bca') ORDER BY block_layouts.identifier ASC LIMIT 10 OFFSET 0"; // phpcs:ignore
179
        $values       = [];
180
        $expectedData = [
181
            [
182
                'id'              => $id,
183
                'identifier'      => $identifier,
184
                'name'            => $name,
185
                'to_name'         => $toName,
186
                'to_email'        => $toEmail,
187
                'success_url'     => $successUrl,
188
                'failure_url'     => $failureUrl,
189
                'max_body_length' => $maxBodyLength,
190
            ],
191
        ];
192
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
193
194
        $this->readConnectionMock
195
            ->expects($this->once())
196
            ->method('prepare')
197
            ->with($sql0)
198
            ->willReturn($statement0);
199
200
        $actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []);
201
202
        $this->assertCollection($expectedData, $actualResult);
203
    }
204
205
    public function testGetById()
206
    {
207
        $id            = 'adbeb333-3110-42ec-a2ed-74a33db518ff';
208
        $identifier    = 'foo';
209
        $name          = 'bar';
210
        $toName        = 'Baz';
211
        $toEmail       = '[email protected]';
212
        $successUrl    = 'https://example.com/success';
213
        $failureUrl    = 'https://failure.example.com/';
214
        $maxBodyLength = 16;
215
216
        $sql0         = 'SELECT cf.id, cf.name, cf.identifier, cf.to_name, cf.to_email, cf.success_url, cf.failure_url, cf.max_body_length FROM contact_forms AS cf WHERE (cf.deleted_at IS NULL) AND (cf.id = :form_id)'; // phpcs:ignore
217
        $values       = ['form_id' => [$id, \PDO::PARAM_STR]];
218
        $expectedData = [
219
            [
220
                'id'              => $id,
221
                'identifier'      => $identifier,
222
                'name'            => $name,
223
                'to_name'         => $toName,
224
                'to_email'        => $toEmail,
225
                'success_url'     => $successUrl,
226
                'failure_url'     => $failureUrl,
227
                'max_body_length' => $maxBodyLength,
228
            ],
229
        ];
230
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
231
232
        $this->readConnectionMock
233
            ->expects($this->once())
234
            ->method('prepare')
235
            ->with($sql0)
236
            ->willReturn($statement0);
237
238
        $actualResult = $this->sut->getById($id);
239
240
        $this->assertEntity($expectedData[0], $actualResult);
241
    }
242
243
    public function testGetByIdentifier()
244
    {
245
        $id            = 'b0538bd0-5762-417c-8208-4e6b04b72f86';
246
        $identifier    = 'foo';
247
        $name          = 'bar';
248
        $toName        = 'Baz';
249
        $toEmail       = '[email protected]';
250
        $successUrl    = 'https://example.com/success';
251
        $failureUrl    = 'https://failure.example.com/';
252
        $maxBodyLength = 16;
253
254
        $sql0         = 'SELECT cf.id, cf.name, cf.identifier, cf.to_name, cf.to_email, cf.success_url, cf.failure_url, cf.max_body_length FROM contact_forms AS cf WHERE (cf.deleted_at IS NULL) AND (cf.identifier = :form_identifier)'; // phpcs:ignore
255
        $values       = ['form_identifier' => [$identifier, \PDO::PARAM_STR]];
256
        $expectedData = [
257
            [
258
                'id'              => $id,
259
                'identifier'      => $identifier,
260
                'name'            => $name,
261
                'to_name'         => $toName,
262
                'to_email'        => $toEmail,
263
                'success_url'     => $successUrl,
264
                'failure_url'     => $failureUrl,
265
                'max_body_length' => $maxBodyLength,
266
            ],
267
        ];
268
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
269
270
        $this->readConnectionMock
271
            ->expects($this->once())
272
            ->method('prepare')
273
            ->with($sql0)
274
            ->willReturn($statement0);
275
276
        $actualResult = $this->sut->getByIdentifier($identifier);
277
278
        $this->assertEntity($expectedData[0], $actualResult);
279
    }
280
281
    public function testUpdate()
282
    {
283
        $id            = '10ada92f-9ed8-4b7b-897a-9e10c640caec';
284
        $identifier    = 'foo';
285
        $name          = 'bar';
286
        $toName        = 'Baz';
287
        $toEmail       = '[email protected]';
288
        $successUrl    = 'https://example.com/success';
289
        $failureUrl    = 'https://failure.example.com/';
290
        $maxBodyLength = 16;
291
292
        $sql0       = 'UPDATE contact_forms AS contact_forms SET name = ?, identifier = ?, to_name = ?, to_email = ?, success_url = ?, failure_url = ?, max_body_length = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
293
        $values     = [
294
            [$name, \PDO::PARAM_STR],
295
            [$identifier, \PDO::PARAM_STR],
296
            [$toName, \PDO::PARAM_STR],
297
            [$toEmail, \PDO::PARAM_STR],
298
            [$successUrl, \PDO::PARAM_STR],
299
            [$failureUrl, \PDO::PARAM_STR],
300
            [$maxBodyLength, \PDO::PARAM_INT],
301
            [$id, \PDO::PARAM_STR],
302
        ];
303
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
304
305
        $this->writeConnectionMock
306
            ->expects($this->once())
307
            ->method('prepare')
308
            ->with($sql0)
309
            ->willReturn($statement0);
310
311
        $entity = new Form($id, $name, $identifier, $toName, $toEmail, $successUrl, $failureUrl, $maxBodyLength);
312
313
        $this->sut->update($entity);
314
    }
315
316
    public function testAddThrowsExceptionIfCalledWithInvalidEntity()
317
    {
318
        $this->expectException(\InvalidArgumentException::class);
319
320
        /** @var IStringerEntity|MockObject $entity */
321
        $entity = $this->createMock(IStringerEntity::class);
322
323
        $this->sut->add($entity);
324
    }
325
326
    public function testDeleteThrowsExceptionIfCalledWithInvalidEntity()
327
    {
328
        $this->expectException(\InvalidArgumentException::class);
329
330
        /** @var IStringerEntity|MockObject $entity */
331
        $entity = $this->createMock(IStringerEntity::class);
332
333
        $this->sut->delete($entity);
334
    }
335
336
    public function testUpdateThrowsExceptionIfCalledWithInvalidEntity()
337
    {
338
        $this->expectException(\InvalidArgumentException::class);
339
340
        /** @var IStringerEntity|MockObject $entity */
341
        $entity = $this->createMock(IStringerEntity::class);
342
343
        $this->sut->update($entity);
344
    }
345
346
    /**
347
     * @param array $expectedData
348
     * @param Form  $entity
349
     */
350
    protected function assertEntity(array $expectedData, $entity)
351
    {
352
        $this->assertInstanceOf(Form::class, $entity);
353
        $this->assertSame($expectedData['id'], $entity->getId());
354
        $this->assertSame($expectedData['identifier'], $entity->getIdentifier());
355
        $this->assertSame($expectedData['name'], $entity->getName());
356
        $this->assertSame($expectedData['to_name'], $entity->getToName());
357
        $this->assertSame($expectedData['to_email'], $entity->getToEmail());
358
        $this->assertSame($expectedData['success_url'], $entity->getSuccessUrl());
359
        $this->assertSame($expectedData['failure_url'], $entity->getFailureUrl());
360
        $this->assertSame($expectedData['max_body_length'], $entity->getMaxBodyLength());
361
    }
362
}
363