testHasAnyChangedSinceReturnsTrueIfSomeBlocksHaveChanged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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