Passed
Push — amo/scrutinizer-new-engine ( 927449 )
by Andreas
10:16
created

ModifyLimitQueryTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 19
rs 9.8333
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\Test\DBAL\Functional;
24
25
use Crate\Test\DBAL\DBALFunctionalTestCase;
26
use Doctrine\DBAL\DBALException;
27
28
29
class ModifyLimitQueryTest extends DBALFunctionalTestCase
30
{
31
    private static $tableCreated = false;
32
33
    public function setUp() : void
34
    {
35
        parent::setUp();
36
37
        if (!self::$tableCreated) {
38
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
39
            $table = new \Doctrine\DBAL\Schema\Table("modify_limit_table");
40
            $table->addColumn('test_int', 'integer');
41
            $table->setPrimaryKey(array('test_int'));
42
43
            $table2 = new \Doctrine\DBAL\Schema\Table("modify_limit_table2");
44
            $table2->addColumn('id', 'integer', array('autoincrement' => true));
45
            $table2->addColumn('test_int', 'integer');
46
            $table2->setPrimaryKey(array('id'));
47
48
            $sm = $this->_conn->getSchemaManager();
49
            $sm->createTable($table);
50
            $sm->createTable($table2);
51
            self::$tableCreated = true;
52
        }
53
    }
54
55
    public function tearDown() : void
56
    {
57
        parent::tearDown();
58
        if (self::$tableCreated) {
59
            $sm = $this->_conn->getSchemaManager();
60
            try {
61
                $sm->dropTable('modify_limit_table');
62
                $sm->dropTable('modify_limit_table2');
63
                self::$tableCreated = false;
64
            } catch (DBALException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
65
        }
66
    }
67
68
    public function testModifyLimitQuerySimpleQuery()
69
    {
70
        $this->_conn->insert('modify_limit_table', array('test_int' => 1));
71
        $this->_conn->insert('modify_limit_table', array('test_int' => 2));
72
        $this->_conn->insert('modify_limit_table', array('test_int' => 3));
73
        $this->_conn->insert('modify_limit_table', array('test_int' => 4));
74
75
        $this->refresh('modify_limit_table');
76
77
        $sql = "SELECT * FROM modify_limit_table ORDER BY test_int ASC";
78
79
        $this->assertLimitResult(array(1, 2, 3, 4), $sql, 10, 0);
80
        $this->assertLimitResult(array(1, 2), $sql, 2, 0);
81
        $this->assertLimitResult(array(3, 4), $sql, 2, 2);
82
    }
83
84
    public function testModifyLimitQueryOrderBy()
85
    {
86
        $this->_conn->insert('modify_limit_table', array('test_int' => 1));
87
        $this->_conn->insert('modify_limit_table', array('test_int' => 2));
88
        $this->_conn->insert('modify_limit_table', array('test_int' => 3));
89
        $this->_conn->insert('modify_limit_table', array('test_int' => 4));
90
91
        $this->refresh('modify_limit_table');
92
93
        $sql = "SELECT * FROM modify_limit_table ORDER BY test_int DESC";
94
95
        $this->assertLimitResult(array(4, 3, 2, 1), $sql, 10, 0);
96
        $this->assertLimitResult(array(4, 3), $sql, 2, 0);
97
        $this->assertLimitResult(array(2, 1), $sql, 2, 2);
98
    }
99
100
    public function testModifyLimitQueryGroupBy()
101
    {
102
        $this->_conn->insert('modify_limit_table2', array('test_int' => 1, 'id' => 1));
103
        $this->_conn->insert('modify_limit_table2', array('test_int' => 1, 'id' => 2));
104
        $this->_conn->insert('modify_limit_table2', array('test_int' => 1, 'id' => 3));
105
        $this->_conn->insert('modify_limit_table2', array('test_int' => 2, 'id' => 4));
106
        $this->_conn->insert('modify_limit_table2', array('test_int' => 2, 'id' => 5));
107
108
        $this->refresh('modify_limit_table2');
109
110
        $sql = "SELECT test_int FROM modify_limit_table2 GROUP BY test_int order by test_int";
111
        $this->assertLimitResult(array(1, 2), $sql, 10, 0);
112
        $this->assertLimitResult(array(1), $sql, 1, 0);
113
        $this->assertLimitResult(array(2), $sql, 1, 1);
114
    }
115
116
    public function assertLimitResult($expectedResults, $sql, $limit, $offset)
117
    {
118
        $p = $this->_conn->getDatabasePlatform();
119
        $data = array();
120
        foreach ($this->_conn->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) AS $row) {
121
            $row = array_change_key_case($row, CASE_LOWER);
122
            $data[] = $row['test_int'];
123
        }
124
        $this->assertEquals($expectedResults, $data);
125
    }
126
}
127