PageCategoryCacheTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testHasAnyChangedSinceReturnsTrueIfSomeBlocksHaveChanged() 0 23 1
A testHasAnyChangedSinceReturnsFalseIfNothingHasChanged() 0 23 1
A testHasAnyChangedSinceThrowsExceptionIfQueryFails() 0 24 1
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 PageCategoryCacheTest extends QueryTestCase
12
{
13
    /** @var PageCategoryCache - System Under Test */
14
    protected $sut;
15
16
    public function setUp(): void
17
    {
18
        parent::setUp();
19
20
        $this->sut = new PageCategoryCache($this->connectionPoolMock);
21
    }
22
23
    public function testHasAnyChangedSinceReturnsFalseIfNothingHasChanged()
24
    {
25
        $identifiers = ['foo', 'bar'];
26
        $cacheTime   = 'baz';
27
28
        $sql0         = 'SELECT COUNT(*) AS count FROM pages LEFT JOIN page_categories AS page_categories ON page_categories.id = pages.category_id WHERE (pages.deleted_at IS NULL) AND (page_categories.identifier IN (?,?)) AND (pages.updated_at > ?)'; // phpcs:ignore
29
        $valuesToBind = [
30
            [$identifiers[0], \PDO::PARAM_STR],
31
            [$identifiers[1], \PDO::PARAM_STR],
32
            [$cacheTime, \PDO::PARAM_STR],
33
        ];
34
        $returnValue  = '0';
35
        $statement0   = MockStatementFactory::createReadColumnStatement($this, $valuesToBind, $returnValue);
36
37
        $this->readConnectionMock
38
            ->expects($this->once())
39
            ->method('prepare')
40
            ->with($sql0)
41
            ->willReturn($statement0);
42
43
        $actualResult = $this->sut->hasAnyChangedSince($identifiers, $cacheTime);
44
45
        $this->assertFalse($actualResult);
46
    }
47
48
    public function testHasAnyChangedSinceReturnsTrueIfSomeBlocksHaveChanged()
49
    {
50
        $identifiers = ['foo', 'bar'];
51
        $cacheTime   = 'baz';
52
53
        $sql0         = 'SELECT COUNT(*) AS count FROM pages LEFT JOIN page_categories AS page_categories ON page_categories.id = pages.category_id WHERE (pages.deleted_at IS NULL) AND (page_categories.identifier IN (?,?)) AND (pages.updated_at > ?)'; // phpcs:ignore
54
        $valuesToBind = [
55
            [$identifiers[0], \PDO::PARAM_STR],
56
            [$identifiers[1], \PDO::PARAM_STR],
57
            [$cacheTime, \PDO::PARAM_STR],
58
        ];
59
        $returnValue  = '2';
60
        $statement0   = MockStatementFactory::createReadColumnStatement($this, $valuesToBind, $returnValue);
61
62
        $this->readConnectionMock
63
            ->expects($this->once())
64
            ->method('prepare')
65
            ->with($sql0)
66
            ->willReturn($statement0);
67
68
        $actualResult = $this->sut->hasAnyChangedSince($identifiers, $cacheTime);
69
70
        $this->assertTrue($actualResult);
71
    }
72
73
    public function testHasAnyChangedSinceThrowsExceptionIfQueryFails()
74
    {
75
        $identifiers = ['foo', 'bar'];
76
        $cacheTime   = 'baz';
77
        $errorInfo   = ['FOO123', 1, 'near AS v0, ar.identifier: hello'];
78
79
        $this->expectException(Database::class);
80
        $this->expectExceptionCode($errorInfo[1]);
81
82
        $sql0         = 'SELECT COUNT(*) AS count FROM pages LEFT JOIN page_categories AS page_categories ON page_categories.id = pages.category_id WHERE (pages.deleted_at IS NULL) AND (page_categories.identifier IN (?,?)) AND (pages.updated_at > ?)'; // phpcs:ignore
83
        $valuesToBind = [
84
            [$identifiers[0], \PDO::PARAM_STR],
85
            [$identifiers[1], \PDO::PARAM_STR],
86
            [$cacheTime, \PDO::PARAM_STR],
87
        ];
88
        $statement0   = MockStatementFactory::createErrorStatement($this, $valuesToBind, $errorInfo);
89
90
        $this->readConnectionMock
91
            ->expects($this->once())
92
            ->method('prepare')
93
            ->with($sql0)
94
            ->willReturn($statement0);
95
96
        $this->sut->hasAnyChangedSince($identifiers, $cacheTime);
97
    }
98
}
99