Passed
Push — master ( bd1840...de7d36 )
by Gabriel
02:59
created

SelectTest::testJoinTableNameWithAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/** @noinspection SqlResolve */
4
5
/** @noinspection SqlNoDataSourceInspection */
6
7
namespace Nip\Database\Tests\Query;
8
9
use Mockery as m;
10
use Nip\Database\Adapters\MySQLi;
11
use Nip\Database\Connections\Connection;
12
use Nip\Database\Query\Select;
13
use Nip\Database\Tests\AbstractTest;
14
15
/**
16
 * Class SelectTest
17
 * @package Nip\Database\Tests\Query
18
 *
19
 * @property Select $object
20
 */
21
class SelectTest extends AbstractTest
22
{
23
    /**
24
     * @var \UnitTester
0 ignored issues
show
Bug introduced by
The type UnitTester was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     */
26
    protected $tester;
27
28
    /**
29
     * @var Connection
30
     */
31
    protected $connection;
32
33
    /**
34
     * @var Select
35
     */
36
    protected $selectQuery;
37
38
    public function testSelectSimple()
39
    {
40
        $array = ['id, name as new_name', 'table2.test', 'MAX(pos) as pos'];
41
        call_user_func_array([$this->selectQuery, 'cols'], $array);
42
        $this->selectQuery->from('table x')->where('id = 5');
0 ignored issues
show
Bug introduced by
'table x' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $this->selectQuery->from(/** @scrutinizer ignore-type */ 'table x')->where('id = 5');
Loading history...
43
44
        static::assertEquals(
45
            'SELECT id, name as new_name, table2.test, MAX(pos) as pos FROM table x WHERE id = 5',
46
            $this->selectQuery->assemble()
47
        );
48
    }
49
50
    public function testSimpleSelectDistinct()
51
    {
52
        $this->selectQuery
53
            ->cols('id, name')
0 ignored issues
show
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::cols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            ->cols(/** @scrutinizer ignore-type */ 'id, name')
Loading history...
54
            ->options('distinct')
55
            ->from('table x')
0 ignored issues
show
Bug introduced by
'table x' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            ->from(/** @scrutinizer ignore-type */ 'table x')
Loading history...
56
            ->where('id = 5');
57
        static::assertEquals(
58
            "SELECT DISTINCT id, name FROM table x WHERE id = 5",
59
            $this->selectQuery->assemble()
60
        );
61
    }
62
63
    public function testWhereAndWhere()
64
    {
65
        $this->selectQuery->cols('id, name')->from('table x');
0 ignored issues
show
Bug introduced by
'table x' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $this->selectQuery->cols('id, name')->from(/** @scrutinizer ignore-type */ 'table x');
Loading history...
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::cols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $this->selectQuery->cols(/** @scrutinizer ignore-type */ 'id, name')->from('table x');
Loading history...
66
        $this->selectQuery->where('id = 5')->where("active = 'yes'");
67
        static::assertEquals(
68
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'",
69
            $this->selectQuery->assemble()
70
        );
71
        $this->object->cols('id, name')->from('table x');
72
        $this->object->where('id = 5')->where("active = 'yes'");
73
        static::assertEquals(
74
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'",
75
            $this->object->assemble());
76
    }
77
78
    public function testWhereOrWhere()
79
    {
80
        $this->selectQuery->cols('id, name')->from('table x');
0 ignored issues
show
Bug introduced by
'table x' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->selectQuery->cols('id, name')->from(/** @scrutinizer ignore-type */ 'table x');
Loading history...
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::cols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->selectQuery->cols(/** @scrutinizer ignore-type */ 'id, name')->from('table x');
Loading history...
81
        $this->selectQuery->where('id = 5')->orWhere('id = 7');
82
        static::assertEquals(
83
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
84
            $this->selectQuery->assemble()
85
        );
86
        $this->object->cols('id, name')->from('table x');
87
        $this->object->where('id = 5')->orWhere('id = 7');
88
        static::assertEquals(
89
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
90
            $this->object->assemble()
91
        );
92
    }
93
94
    public function testInitializeCondition()
95
    {
96
        $condition = $this->object->getCondition("lorem ipsum");
97
        static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition"));
98
    }
99
100
    public function testNested()
101
    {
102
        $this->selectQuery->from("table1");
0 ignored issues
show
Bug introduced by
'table1' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        $this->selectQuery->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
103
        $this->object->from("table1");
104
105
        $query = $this->connection->newQuery();
106
        $query->from("table2");
107
        $query->where("id != 5");
108
109
        $this->object->where("id NOT IN ?", $query);
0 ignored issues
show
Bug introduced by
$query of type Nip\Database\Query\AbstractQuery is incompatible with the type array expected by parameter $values of Nip\Database\Query\AbstractQuery::where(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        $this->object->where("id NOT IN ?", /** @scrutinizer ignore-type */ $query);
Loading history...
110
111
        static::assertEquals(
112
            "SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)",
113
            $this->object->assemble()
114
        );
115
    }
116
117
    public function testUnion()
118
    {
119
        $this->object->from("table1");
0 ignored issues
show
Bug introduced by
'table1' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
120
121
        $query = $this->connection->newQuery();
122
        $query->from("table2");
123
124
        $union = $this->object->union($query);
125
126
        static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble());
127
    }
128
129
    public function testJoinTableName()
130
    {
131
        $this->object->from("table1");
0 ignored issues
show
Bug introduced by
'table1' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
132
        $this->object->join("table2", ['id', 'id_table1']);
0 ignored issues
show
Bug introduced by
array('id', 'id_table1') of type array<integer,string> is incompatible with the type boolean|string expected by parameter $on of Nip\Database\Query\Select::join(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
        $this->object->join("table2", /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
133
134
        static::assertEquals(
135
            "SELECT * FROM `table1` JOIN `table2` ON `table1`.`id` = `table2`.`id_table1`",
136
            $this->object->assemble()
137
        );
138
    }
139
140
    public function testJoinTableNameWithAlias()
141
    {
142
        $this->object->from("table1");
0 ignored issues
show
Bug introduced by
'table1' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
143
        $this->object->join(["table2", "alias"], ['id', 'id_table1']);
0 ignored issues
show
Bug introduced by
array('id', 'id_table1') of type array<integer,string> is incompatible with the type boolean|string expected by parameter $on of Nip\Database\Query\Select::join(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        $this->object->join(["table2", "alias"], /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
144
145
        static::assertEquals(
146
            "SELECT * FROM `table1` JOIN `table2` AS `alias` ON `table1`.`id` = `table2`.`id_table1`",
147
            $this->object->assemble()
148
        );
149
    }
150
151
    public function testJoinSubQuery()
152
    {
153
        $this->object->from("table1");
0 ignored issues
show
Bug introduced by
'table1' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
154
155
        $query = $this->connection->newQuery();
156
        $query->from("table2");
157
158
        $this->object->join([$query, "alias"], ['id', 'id_table1']);
0 ignored issues
show
Bug introduced by
array('id', 'id_table1') of type array<integer,string> is incompatible with the type boolean|string expected by parameter $on of Nip\Database\Query\Select::join(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        $this->object->join([$query, "alias"], /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
159
160
        static::assertEquals(
161
            'SELECT * FROM `table1` JOIN (SELECT * FROM `table2`) AS `alias` ON `table1`.`id` = `alias`.`id_table1`',
162
            $this->object->assemble()
163
        );
164
    }
165
166
    public function testHasPart()
167
    {
168
        $this->object->cols('id, name');
0 ignored issues
show
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::cols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        $this->object->cols(/** @scrutinizer ignore-type */ 'id, name');
Loading history...
169
        self::assertTrue($this->object->hasPart('cols'));
170
171
        $this->object->setCols('id, name');
0 ignored issues
show
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::setCols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
        $this->object->setCols(/** @scrutinizer ignore-type */ 'id, name');
Loading history...
172
        self::assertTrue($this->object->hasPart('cols'));
173
174
        $this->object->limit('');
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $start of Nip\Database\Query\AbstractQuery::limit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        $this->object->limit(/** @scrutinizer ignore-type */ '');
Loading history...
175
        self::assertFalse($this->object->hasPart('limit'));
176
177
        $this->object->limit('6');
178
        self::assertTrue($this->object->hasPart('limit'));
179
180
        self::assertFalse($this->object->hasPart('where'));
181
    }
182
183
    public function testLimit()
184
    {
185
        $this->object->cols('id, name')->from('table x');
0 ignored issues
show
Bug introduced by
'id, name' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::cols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
        $this->object->cols(/** @scrutinizer ignore-type */ 'id, name')->from('table x');
Loading history...
Bug introduced by
'table x' of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
        $this->object->cols('id, name')->from(/** @scrutinizer ignore-type */ 'table x');
Loading history...
186
        $this->object->where('id = 5')->where("active = 'yes'");
187
        $this->object->limit(5);
188
189
        static::assertEquals(
190
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5",
191
            $this->object->assemble()
192
        );
193
194
        $this->object->limit(5, 10);
0 ignored issues
show
Bug introduced by
10 of type integer is incompatible with the type boolean expected by parameter $offset of Nip\Database\Query\AbstractQuery::limit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
        $this->object->limit(5, /** @scrutinizer ignore-type */ 10);
Loading history...
195
        static::assertEquals(
196
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10",
197
            $this->object->assemble()
198
        );
199
    }
200
201
    protected function setUp() : void
202
    {
203
        parent::setUp();
204
        $this->object = new Select();
205
206
        $adapterMock = m::mock(MySQLi::class)->makePartial();
207
        $adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) {
208
            return $data;
209
        });
210
        $this->connection = new Connection(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type Closure|PDO expected by parameter $pdo of Nip\Database\Connections\Connection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

210
        $this->connection = new Connection(/** @scrutinizer ignore-type */ false);
Loading history...
211
        $this->connection->setAdapter($adapterMock);
212
        $this->object->setManager($this->connection);
213
214
        $this->selectQuery = new Select();
215
    }
216
}
217