Passed
Push — master ( 383493...d29574 )
by Chito
02:01
created

SqliteCompilerTest::testUnion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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