PageCategoryAuthLoaderTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\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 PageCategoryAuthLoaderTest extends QueryTestCase
12
{
13
    /** @var PageCategoryAuthLoader - System Under Test */
14
    protected $sut;
15
16
    public function setUp(): void
17
    {
18
        parent::setUp();
19
20
        $this->sut = new PageCategoryAuthLoader($this->connectionPoolMock);
21
    }
22
23
    public function testLoadAll()
24
    {
25
        $sql0         = 'SELECT ug.identifier AS v0, pc.identifier AS v1 FROM user_groups_page_categories AS ugpc INNER JOIN page_categories AS pc ON ugpc.page_category_id = pc.id AND pc.deleted_at IS NULL INNER JOIN user_groups AS ug ON ugpc.user_group_id = ug.id AND ug.deleted_at IS NULL'; // phpcs:ignore
26
        $valuesToBind = [];
27
        $returnValue  = [
28
            ['v0' => 'foo', 'v1' => 'bar'],
29
            ['v0' => 'foo', 'v1' => 'baz'],
30
            ['v0' => 'qux', 'v1' => 'quux'],
31
        ];
32
        $statement0   = MockStatementFactory::createReadStatement($this, $valuesToBind, $returnValue);
33
34
        $this->readConnectionMock
35
            ->expects($this->once())
36
            ->method('prepare')
37
            ->with($sql0)
38
            ->willReturn($statement0);
39
40
        $actualResult = $this->sut->loadAll();
41
42
        $this->assertSame($returnValue, $actualResult);
43
    }
44
45
    public function testLoadAllThrowsExceptionIfQueryFails()
46
    {
47
        $errorInfo = ['FOO123', 1, 'near AS v0, ar.identifier: hello'];
48
49
        $this->expectException(Database::class);
50
        $this->expectExceptionCode($errorInfo[1]);
51
52
        $sql0         = 'SELECT ug.identifier AS v0, pc.identifier AS v1 FROM user_groups_page_categories AS ugpc INNER JOIN page_categories AS pc ON ugpc.page_category_id = pc.id AND pc.deleted_at IS NULL INNER JOIN user_groups AS ug ON ugpc.user_group_id = ug.id AND ug.deleted_at IS NULL'; // phpcs:ignore
53
        $valuesToBind = [];
54
        $statement0   = MockStatementFactory::createErrorStatement($this, $valuesToBind, $errorInfo);
55
56
        $this->readConnectionMock
57
            ->expects($this->once())
58
            ->method('prepare')
59
            ->with($sql0)
60
            ->willReturn($statement0);
61
62
        $this->sut->loadAll();
63
    }
64
}
65