SelectTest::testNested()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 14
rs 9.9666
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
79
    public function testWhereOrWhere()
80
    {
81
        $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

81
        $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

81
        $this->selectQuery->cols(/** @scrutinizer ignore-type */ 'id, name')->from('table x');
Loading history...
82
        $this->selectQuery->where('id = 5')->orWhere('id = 7');
83
        static::assertEquals(
84
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
85
            $this->selectQuery->assemble()
86
        );
87
        $this->object->cols('id, name')->from('table x');
88
        $this->object->where('id = 5')->orWhere('id = 7');
89
        static::assertEquals(
90
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
91
            $this->object->assemble()
92
        );
93
    }
94
95
    public function testInitializeCondition()
96
    {
97
        $condition = $this->object->getCondition("lorem ipsum");
98
        static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition"));
99
    }
100
101
    public function testNested()
102
    {
103
        $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

103
        $this->selectQuery->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
104
        $this->object->from("table1");
105
106
        $query = $this->connection->newQuery();
107
        $query->from("table2");
108
        $query->where("id != 5");
109
110
        $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

110
        $this->object->where("id NOT IN ?", /** @scrutinizer ignore-type */ $query);
Loading history...
111
112
        static::assertEquals(
113
            "SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)",
114
            $this->object->assemble()
115
        );
116
    }
117
118
    public function testUnion()
119
    {
120
        $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

120
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
121
122
        $query = $this->connection->newQuery();
123
        $query->from("table2");
124
125
        $union = $this->object->union($query);
126
127
        static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble());
128
    }
129
130
    public function testJoinTableName()
131
    {
132
        $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

132
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
133
        $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

133
        $this->object->join("table2", /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
134
135
        static::assertEquals(
136
            "SELECT * FROM `table1` JOIN `table2` ON `table1`.`id` = `table2`.`id_table1`",
137
            $this->object->assemble()
138
        );
139
    }
140
141
    public function testJoinTableNameWithAlias()
142
    {
143
        $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

143
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
144
        $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

144
        $this->object->join(["table2", "alias"], /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
145
146
        static::assertEquals(
147
            "SELECT * FROM `table1` JOIN `table2` AS `alias` ON `table1`.`id` = `table2`.`id_table1`",
148
            $this->object->assemble()
149
        );
150
    }
151
152
    public function testJoinSubQuery()
153
    {
154
        $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

154
        $this->object->from(/** @scrutinizer ignore-type */ "table1");
Loading history...
155
156
        $query = $this->connection->newQuery();
157
        $query->from("table2");
158
159
        $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

159
        $this->object->join([$query, "alias"], /** @scrutinizer ignore-type */ ['id', 'id_table1']);
Loading history...
160
161
        static::assertEquals(
162
            'SELECT * FROM `table1` JOIN (SELECT * FROM `table2`) AS `alias` ON `table1`.`id` = `alias`.`id_table1`',
163
            $this->object->assemble()
164
        );
165
    }
166
167
    public function testHasPart()
168
    {
169
        $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

169
        $this->object->cols(/** @scrutinizer ignore-type */ 'id, name');
Loading history...
170
        self::assertTrue($this->object->hasPart('cols'));
171
172
        $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

172
        $this->object->setCols(/** @scrutinizer ignore-type */ 'id, name');
Loading history...
173
        self::assertTrue($this->object->hasPart('cols'));
174
175
        $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

175
        $this->object->limit(/** @scrutinizer ignore-type */ '');
Loading history...
176
        self::assertFalse($this->object->hasPart('limit'));
177
178
        $this->object->limit('6');
179
        self::assertTrue($this->object->hasPart('limit'));
180
181
        self::assertFalse($this->object->hasPart('where'));
182
    }
183
184
    public function testLimit()
185
    {
186
        $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

186
        $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

186
        $this->object->cols('id, name')->from(/** @scrutinizer ignore-type */ 'table x');
Loading history...
187
        $this->object->where('id = 5')->where("active = 'yes'");
188
        $this->object->limit(5);
189
190
        static::assertEquals(
191
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5",
192
            $this->object->assemble()
193
        );
194
195
        $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

195
        $this->object->limit(5, /** @scrutinizer ignore-type */ 10);
Loading history...
196
        static::assertEquals(
197
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10",
198
            $this->object->assemble()
199
        );
200
    }
201
202
    protected function setUp(): void
203
    {
204
        parent::setUp();
205
        $this->object = new Select();
206
207
        $adapterMock = m::mock(MySQLi::class)->makePartial();
208
        $adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) {
209
            return $data;
210
        });
211
        $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

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