testLoadAllThrowsExceptionIfQueryFails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 18
rs 9.8666
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Databases\Queries;
6
7
use AbterPhp\Admin\Exception\Database;
8
use AbterPhp\Framework\TestCase\Database\QueryTestCase;
9
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory;
10
11
class UserAuthLoaderTest extends QueryTestCase
12
{
13
    /** @var UserAuthLoader - System Under Test */
14
    protected $sut;
15
16
    public function setUp(): void
17
    {
18
        parent::setUp();
19
20
        $this->sut = new UserAuthLoader($this->connectionPoolMock);
21
    }
22
23
    public function testLoadAll()
24
    {
25
        $username            = 'foo';
26
        $userGroupIdentifier = 'bar';
27
28
        $sql0         = 'SELECT u.username AS v0, ug.identifier AS v1 FROM users AS u INNER JOIN users_user_groups AS uug ON uug.user_id = u.id AND uug.deleted_at IS NULL INNER JOIN user_groups AS ug ON uug.user_group_id = ug.id AND ug.deleted_at IS NULL WHERE (u.deleted_at IS NULL)'; // phpcs:ignore
29
        $valuesToBind = [];
30
        $returnValues = [
31
            [
32
                'v0' => $username,
33
                'v1' => $userGroupIdentifier,
34
            ],
35
        ];
36
        $statement0   = MockStatementFactory::createReadStatement($this, $valuesToBind, $returnValues);
37
38
        $this->readConnectionMock
39
            ->expects($this->exactly(1))
40
            ->method('prepare')
41
            ->withConsecutive([$sql0])
42
            ->willReturnOnConsecutiveCalls($statement0);
43
44
        $actualResult = $this->sut->loadAll();
45
46
        $this->assertEquals($returnValues, $actualResult);
47
    }
48
49
    public function testLoadAllThrowsExceptionIfQueryFails()
50
    {
51
        $errorInfo = ['FOO123', 1, 'near AS v0, ar.identifier: hello'];
52
53
        $this->expectException(Database::class);
54
        $this->expectExceptionCode($errorInfo[1]);
55
56
        $sql0         = 'SELECT u.username AS v0, ug.identifier AS v1 FROM users AS u INNER JOIN users_user_groups AS uug ON uug.user_id = u.id AND uug.deleted_at IS NULL INNER JOIN user_groups AS ug ON uug.user_group_id = ug.id AND ug.deleted_at IS NULL WHERE (u.deleted_at IS NULL)'; // phpcs:ignore
57
        $valuesToBind = [];
58
        $statement0   = MockStatementFactory::createErrorStatement($this, $valuesToBind, $errorInfo);
59
60
        $this->readConnectionMock
61
            ->expects($this->exactly(1))
62
            ->method('prepare')
63
            ->withConsecutive([$sql0])
64
            ->willReturnOnConsecutiveCalls($statement0);
65
66
        $this->sut->loadAll();
67
    }
68
}
69