Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

TokenSqlDataMapperTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 14
eloc 166
c 1
b 1
f 0
dl 0
loc 280
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetById() 0 28 1
A testGetByClientId() 0 27 1
A assertEntity() 0 11 2
A testAddThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testAddWithRevokedAt() 0 27 1
A testUpdate() 0 25 1
A testAdd() 0 27 1
A testUpdateThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testDeleteThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A setUp() 0 5 1
A testGetAll() 0 29 1
A testDelete() 0 19 1
A testUpdateWithRevokedAt() 0 25 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Orm\DataMapper;
6
7
use AbterPhp\Admin\Domain\Entities\Token;
8
use AbterPhp\Admin\Orm\DataMappers\TokenSqlDataMapper;
9
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase;
10
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
11
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory;
12
use PHPUnit\Framework\MockObject\MockObject;
13
14
class TokenSqlDataMapperTest extends DataMapperTestCase
15
{
16
    /** @var TokenSqlDataMapper */
17
    protected $sut;
18
19
    public function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->sut = new TokenSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
24
    }
25
26
    public function testAdd()
27
    {
28
        $nextId      = '9b6ae58b-1aff-4344-a2ae-cda43a40674e';
29
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
30
        $expiresAt   = new \DateTimeImmutable();
31
        $revokedAt   = null;
32
33
        $sql0       = 'INSERT INTO tokens (id, api_client_id, expires_at, revoked_at) VALUES (?, ?, ?, ?)'; // phpcs:ignore
34
        $values     = [
35
            [$nextId, \PDO::PARAM_STR],
36
            [$apiClientId, \PDO::PARAM_STR],
37
            [$expiresAt, \PDO::PARAM_STR],
38
            [$revokedAt, \PDO::PARAM_NULL],
39
        ];
40
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
41
42
        $this->writeConnectionMock
43
            ->expects($this->once())
44
            ->method('prepare')
45
            ->with($sql0)
46
            ->willReturn($statement0);
47
48
        $entity = new Token($nextId, $apiClientId, $expiresAt, $revokedAt);
49
50
        $this->sut->add($entity);
51
52
        $this->assertSame($nextId, $entity->getId());
53
    }
54
55
    public function testAddWithRevokedAt()
56
    {
57
        $nextId      = '9b6ae58b-1aff-4344-a2ae-cda43a40674e';
58
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
59
        $expiresAt   = new \DateTimeImmutable();
60
        $revokedAt   = new \DateTimeImmutable();
61
62
        $sql0       = 'INSERT INTO tokens (id, api_client_id, expires_at, revoked_at) VALUES (?, ?, ?, ?)'; // phpcs:ignore
63
        $values     = [
64
            [$nextId, \PDO::PARAM_STR],
65
            [$apiClientId, \PDO::PARAM_STR],
66
            [$expiresAt, \PDO::PARAM_STR],
67
            [$revokedAt, \PDO::PARAM_STR],
68
        ];
69
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
70
71
        $this->writeConnectionMock
72
            ->expects($this->once())
73
            ->method('prepare')
74
            ->with($sql0)
75
            ->willReturn($statement0);
76
77
        $entity = new Token($nextId, $apiClientId, $expiresAt, $revokedAt);
78
79
        $this->sut->add($entity);
80
81
        $this->assertSame($nextId, $entity->getId());
82
    }
83
84
    public function testDelete()
85
    {
86
        $id          = '8fe2f659-dbe5-4995-9e07-f49fb018cfe7';
87
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
88
        $expiresAt   = new \DateTimeImmutable();
89
90
        $sql0       = 'UPDATE tokens AS tokens SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
91
        $values    = [[$id, \PDO::PARAM_STR]];
92
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
93
94
        $this->writeConnectionMock
95
            ->expects($this->once())
96
            ->method('prepare')
97
            ->with($sql0)
98
            ->willReturn($statement0);
99
100
        $entity = new Token($id, $apiClientId, $expiresAt, null);
101
102
        $this->sut->delete($entity);
103
    }
104
105
    public function testGetAll()
106
    {
107
        $id0          = '24bd4165-1229-4a6e-a679-76bf90743ee1';
108
        $apiClientId0 = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
109
        $expiresAt0   = '2019-08-18 00:22:03';
110
        $revokedAt0   = null;
111
112
        $id1          = '51eac0fc-2b26-4231-9559-469e59fae694';
113
        $apiClientId1 = 'd9162b70-03e0-4969-aecc-8112adcd94c0';
114
        $expiresAt1   = '2019-08-18 00:22:13';
115
        $revokedAt1   = '2019-08-18 00:22:18';
116
117
        $sql0         = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL)'; // phpcs:ignore
118
        $values       = [];
119
        $expectedData = [
120
            ['id' => $id0, 'api_client_id' => $apiClientId0, 'expires_at' => $expiresAt0, 'revoked_at' => $revokedAt0],
121
            ['id' => $id1, 'api_client_id' => $apiClientId1, 'expires_at' => $expiresAt1, 'revoked_at' => $revokedAt1],
122
        ];
123
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
124
125
        $this->readConnectionMock
126
            ->expects($this->once())
127
            ->method('prepare')
128
            ->with($sql0)
129
            ->willReturn($statement0);
130
131
        $actualResult = $this->sut->getAll();
132
133
        $this->assertCollection($expectedData, $actualResult);
134
    }
135
136
    public function testGetByClientId()
137
    {
138
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
139
140
        $id0          = '24bd4165-1229-4a6e-a679-76bf90743ee1';
141
        $apiClientId0 = $apiClientId;
142
        $expiresAt0   = '2019-08-18 00:22:03';
143
        $revokedAt0   = null;
144
145
        $sql0         = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL) AND (tokens.api_client_id = :api_client_id)'; // phpcs:ignore
146
        $values       = [
147
            'api_client_id' => [$apiClientId, \PDO::PARAM_STR],
148
        ];
149
        $expectedData = [
150
            ['id' => $id0, 'api_client_id' => $apiClientId0, 'expires_at' => $expiresAt0, 'revoked_at' => $revokedAt0],
151
        ];
152
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
153
154
        $this->readConnectionMock
155
            ->expects($this->once())
156
            ->method('prepare')
157
            ->with($sql0)
158
            ->willReturn($statement0);
159
160
        $actualResult = $this->sut->getByClientId($apiClientId);
161
162
        $this->assertEntity($expectedData[0], $actualResult);
163
    }
164
165
    public function testGetById()
166
    {
167
        $id          = '4b72daf8-81a9-400f-b865-28306d1c1646';
168
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
169
        $expiresAt   = '2019-08-18 00:22:03';
170
        $revokedAt   = null;
171
172
        $sql0         = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL) AND (tokens.id = :token_id)'; // phpcs:ignore
173
        $values       = ['token_id' => [$id, \PDO::PARAM_STR]];
174
        $expectedData = [
175
            [
176
                'id'            => $id,
177
                'api_client_id' => $apiClientId,
178
                'expires_at'    => $expiresAt,
179
                'revoked_at'    => $revokedAt,
180
            ],
181
        ];
182
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
183
184
        $this->readConnectionMock
185
            ->expects($this->once())
186
            ->method('prepare')
187
            ->with($sql0)
188
            ->willReturn($statement0);
189
190
        $actualResult = $this->sut->getById($id);
191
192
        $this->assertEntity($expectedData[0], $actualResult);
193
    }
194
195
    public function testUpdate()
196
    {
197
        $id          = '91693481-276e-495b-82a1-33209c47ca09';
198
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
199
        $expiresAt   = new \DateTimeImmutable('2019-08-18 00:22:03');
200
        $revokedAt   = null;
201
202
        $sql0       = 'UPDATE tokens AS tokens SET api_client_id = ?, expires_at = ?, revoked_at = ? WHERE (id = ?)'; // phpcs:ignore
203
        $values     = [
204
            [$apiClientId, \PDO::PARAM_STR],
205
            [$expiresAt, \PDO::PARAM_STR],
206
            [$revokedAt, \PDO::PARAM_NULL],
207
            [$id, \PDO::PARAM_STR],
208
        ];
209
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
210
211
        $this->writeConnectionMock
212
            ->expects($this->once())
213
            ->method('prepare')
214
            ->with($sql0)
215
            ->willReturn($statement0);
216
217
        $entity = new Token($id, $apiClientId, $expiresAt, $revokedAt);
218
219
        $this->sut->update($entity);
220
    }
221
222
    public function testUpdateWithRevokedAt()
223
    {
224
        $id          = '91693481-276e-495b-82a1-33209c47ca09';
225
        $apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245';
226
        $expiresAt   = new \DateTimeImmutable('2019-08-18 00:22:03');
227
        $revokedAt   = new \DateTimeImmutable('2019-08-18 00:22:13');
228
229
        $sql0       = 'UPDATE tokens AS tokens SET api_client_id = ?, expires_at = ?, revoked_at = ? WHERE (id = ?)'; // phpcs:ignore
230
        $values     = [
231
            [$apiClientId, \PDO::PARAM_STR],
232
            [$expiresAt, \PDO::PARAM_STR],
233
            [$revokedAt, \PDO::PARAM_STR],
234
            [$id, \PDO::PARAM_STR],
235
        ];
236
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
237
238
        $this->writeConnectionMock
239
            ->expects($this->once())
240
            ->method('prepare')
241
            ->with($sql0)
242
            ->willReturn($statement0);
243
244
        $entity = new Token($id, $apiClientId, $expiresAt, $revokedAt);
245
246
        $this->sut->update($entity);
247
    }
248
249
    public function testAddThrowsExceptionIfCalledWithInvalidEntity()
250
    {
251
        $this->expectException(\InvalidArgumentException::class);
252
253
        /** @var IStringerEntity|MockObject $entity */
254
        $entity = $this->createMock(IStringerEntity::class);
255
256
        $this->sut->add($entity);
257
    }
258
259
    public function testDeleteThrowsExceptionIfCalledWithInvalidEntity()
260
    {
261
        $this->expectException(\InvalidArgumentException::class);
262
263
        /** @var IStringerEntity|MockObject $entity */
264
        $entity = $this->createMock(IStringerEntity::class);
265
266
        $this->sut->delete($entity);
267
    }
268
269
    public function testUpdateThrowsExceptionIfCalledWithInvalidEntity()
270
    {
271
        $this->expectException(\InvalidArgumentException::class);
272
273
        /** @var IStringerEntity|MockObject $entity */
274
        $entity = $this->createMock(IStringerEntity::class);
275
276
        $this->sut->update($entity);
277
    }
278
279
    /**
280
     * @param array $expectedData
281
     * @param Token $entity
282
     */
283
    protected function assertEntity(array $expectedData, $entity)
284
    {
285
        $this->assertInstanceOf(Token::class, $entity);
286
        $this->assertEquals($expectedData['id'], $entity->getId());
287
        $this->assertSame($expectedData['api_client_id'], $entity->getApiClientId());
288
        $this->assertEquals(new \DateTimeImmutable($expectedData['expires_at']), $entity->getExpiresAt());
289
290
        if (null === $expectedData['revoked_at']) {
291
            $this->assertNull($entity->getRevokedAt());
292
        } else {
293
            $this->assertEquals(new \DateTimeImmutable($expectedData['revoked_at']), $entity->getRevokedAt());
294
        }
295
    }
296
}
297