SqliteCompilerTest::testSelect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 19
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lampager\Cake\Test\TestCase\Database;
6
7
use Cake\Datasource\ConnectionManager;
8
use Cake\ORM\TableRegistry;
9
use Lampager\Cake\Test\TestCase\TestCase;
10
11
class SqliteCompilerTest extends TestCase
12
{
13
    public array $fixtures = [
14
        'plugin.Lampager\\Cake.Posts',
15
    ];
16
17
    public function setUp(): void
18
    {
19
        $config = ConnectionManager::getConfig('test');
20
        $this->skipIf(strpos($config['driver'], 'Sqlite') === false, 'Not using Sqlite');
21
    }
22
23
    public function testSelect(): void
24
    {
25
        $posts = TableRegistry::getTableLocator()->get('Posts');
26
27
        $expected = '
28
            SELECT
29
                "Posts".* AS "Posts__*"
30
            FROM
31
                "posts" "Posts"
32
            ORDER BY
33
                "modified" ASC
34
        ';
35
36
        $actual = $posts->find()
37
            ->select(['*'])
38
            ->orderAsc('modified')
39
            ->sql();
40
41
        $this->assertSqlEquals($expected, $actual);
42
    }
43
44
    public function testUnion(): void
45
    {
46
        $posts = TableRegistry::getTableLocator()->get('Posts');
47
48
        $expected = '
49
            SELECT
50
                *
51
            FROM
52
                (
53
                    SELECT
54
                        "Posts"."id" AS "Posts__id",
55
                        "Posts"."modified" AS "Posts__modified"
56
                    FROM
57
                        "posts" "Posts"
58
                    WHERE
59
                        "id" > :c0
60
                    ORDER BY
61
                        "modified" ASC
62
                )
63
            UNION ALL
64
            SELECT
65
                *
66
            FROM
67
                (
68
                    SELECT
69
                        "Posts"."id" AS "Posts__id",
70
                        "Posts"."modified" AS "Posts__modified"
71
                    FROM
72
                        "posts" "Posts"
73
                    ORDER BY
74
                        "modified" ASC
75
                )
76
        ';
77
78
        $subQuery = $posts->find()
79
            ->select(['id', 'modified'])
80
            ->orderAsc('modified');
81
82
        $mainQuery = $posts->find()
83
            ->select(['id', 'modified'])
84
            ->where(['id >' => 1])
85
            ->orderAsc('modified')
86
            ->unionAll($subQuery);
87
88
        $actual = $mainQuery->sql();
89
        $this->assertSqlEquals($expected, $actual);
90
    }
91
}
92