1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Database\Tests\Query; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Nip\Database\Connections\Connection; |
7
|
|
|
use Nip\Database\Query\Select; |
8
|
|
|
use Nip\Database\Tests\AbstractTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SelectTest |
12
|
|
|
* @package Nip\Database\Tests\Query |
13
|
|
|
*/ |
14
|
|
|
class SelectTest extends AbstractTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \UnitTester |
18
|
|
|
*/ |
19
|
|
|
protected $tester; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Connection |
23
|
|
|
*/ |
24
|
|
|
protected $connection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Select |
28
|
|
|
*/ |
29
|
|
|
protected $selectQuery; |
30
|
|
|
|
31
|
|
|
public function testSelectSimple() |
32
|
|
|
{ |
33
|
|
|
$array = ['id, name as new_name', 'table2.test', 'MAX(pos) as pos']; |
34
|
|
|
call_user_func_array([$this->selectQuery, 'cols'], $array); |
35
|
|
|
$this->selectQuery->from('table x')->where('id = 5'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
static::assertEquals( |
38
|
|
|
'SELECT id, name as new_name, table2.test, MAX(pos) as pos FROM table x WHERE id = 5', |
39
|
|
|
$this->selectQuery->assemble() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testSimpleSelectDistinct() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->selectQuery->cols('id, name')->options('distinct')->from('table x')->where('id = 5'); |
|
|
|
|
46
|
|
|
static::assertEquals( |
47
|
|
|
"SELECT DISTINCT id, name FROM table x WHERE id = 5", |
48
|
|
|
$this->selectQuery->assemble() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testWhereAndWhere() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->selectQuery->cols('id, name')->from('table x'); |
|
|
|
|
55
|
|
|
$this->selectQuery->where('id = 5')->where("active = 'yes'"); |
56
|
|
|
static::assertEquals( |
57
|
|
|
"SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'", |
58
|
|
|
$this->selectQuery->assemble() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testWhereOrWhere() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->selectQuery->cols('id, name')->from('table x'); |
|
|
|
|
65
|
|
|
$this->selectQuery->where('id = 5')->orWhere('id = 7'); |
66
|
|
|
static::assertEquals( |
67
|
|
|
"SELECT id, name FROM table x WHERE id = 5 OR id = 7", |
68
|
|
|
$this->selectQuery->assemble() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testInitializeCondition() |
73
|
|
|
{ |
74
|
|
|
$condition = $this->selectQuery->getCondition("lorem ipsum"); |
75
|
|
|
static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition")); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testNested() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->selectQuery->from("table1"); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$query = $this->connection->newQuery(); |
83
|
|
|
$query->from("table2"); |
|
|
|
|
84
|
|
|
$query->where("id != 5"); |
85
|
|
|
|
86
|
|
|
$this->selectQuery->where("id NOT IN ?", $query); |
|
|
|
|
87
|
|
|
|
88
|
|
|
static::assertEquals( |
89
|
|
|
"SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)", |
90
|
|
|
$this->selectQuery->assemble() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testUnion() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->selectQuery->from("table1"); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$query = $this->connection->newQuery(); |
99
|
|
|
$query->from("table2"); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$union = $this->selectQuery->union($query); |
102
|
|
|
|
103
|
|
|
static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testHasPart() |
107
|
|
|
{ |
108
|
|
|
$this->selectQuery->cols('id, name'); |
|
|
|
|
109
|
|
|
self::assertTrue($this->selectQuery->hasPart('cols')); |
110
|
|
|
|
111
|
|
|
$this->selectQuery->setCols('id, name'); |
|
|
|
|
112
|
|
|
self::assertTrue($this->selectQuery->hasPart('cols')); |
113
|
|
|
|
114
|
|
|
$this->selectQuery->limit(''); |
115
|
|
|
self::assertFalse($this->selectQuery->hasPart('limit')); |
116
|
|
|
|
117
|
|
|
$this->selectQuery->limit('6'); |
118
|
|
|
self::assertTrue($this->selectQuery->hasPart('limit')); |
119
|
|
|
|
120
|
|
|
self::assertFalse($this->selectQuery->hasPart('where')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testLimit() |
124
|
|
|
{ |
125
|
|
|
$this->selectQuery->cols('id, name')->from('table x'); |
|
|
|
|
126
|
|
|
$this->selectQuery->where('id = 5')->where("active = 'yes'"); |
127
|
|
|
$this->selectQuery->limit(5); |
128
|
|
|
|
129
|
|
|
static::assertEquals( |
130
|
|
|
"SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5", |
131
|
|
|
$this->selectQuery->assemble() |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->selectQuery->limit(5, 10); |
|
|
|
|
135
|
|
|
static::assertEquals( |
136
|
|
|
"SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10", |
137
|
|
|
$this->selectQuery->assemble() |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
parent::setUp(); |
144
|
|
|
$this->selectQuery = new Select(); |
145
|
|
|
|
146
|
|
|
$adapterMock = m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing(); |
147
|
|
|
$adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) { |
148
|
|
|
return $data; |
149
|
|
|
}); |
150
|
|
|
$this->connection = new Connection(false); |
|
|
|
|
151
|
|
|
$this->connection->setAdapter($adapterMock); |
152
|
|
|
$this->selectQuery->setManager($this->connection); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.