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 |
|
|
|
|
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'); |
|
|
|
|
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') |
|
|
|
|
54
|
|
|
->options('distinct') |
55
|
|
|
->from('table x') |
|
|
|
|
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'); |
|
|
|
|
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'); |
|
|
|
|
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"); |
|
|
|
|
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); |
|
|
|
|
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"); |
|
|
|
|
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"); |
|
|
|
|
132
|
|
|
$this->object->join("table2", ['id', 'id_table1']); |
|
|
|
|
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"); |
|
|
|
|
143
|
|
|
$this->object->join(["table2", "alias"], ['id', 'id_table1']); |
|
|
|
|
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"); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$query = $this->connection->newQuery(); |
156
|
|
|
$query->from("table2"); |
157
|
|
|
|
158
|
|
|
$this->object->join([$query, "alias"], ['id', 'id_table1']); |
|
|
|
|
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'); |
|
|
|
|
169
|
|
|
self::assertTrue($this->object->hasPart('cols')); |
170
|
|
|
|
171
|
|
|
$this->object->setCols('id, name'); |
|
|
|
|
172
|
|
|
self::assertTrue($this->object->hasPart('cols')); |
173
|
|
|
|
174
|
|
|
$this->object->limit(''); |
|
|
|
|
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'); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
211
|
|
|
$this->connection->setAdapter($adapterMock); |
212
|
|
|
$this->object->setManager($this->connection); |
213
|
|
|
|
214
|
|
|
$this->selectQuery = new Select(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths