|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Query\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
10
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
11
|
|
|
use Yiisoft\Db\Query\Helper\QueryHelper; |
|
12
|
|
|
use Yiisoft\Db\Schema\Quoter; |
|
13
|
|
|
|
|
14
|
|
|
final class QueryHelperTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function tablesNameDataProvider(): array |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
|
|
[['customer'], '', ['{{customer}}' => '{{customer}}']], |
|
20
|
|
|
[['profile AS "prf"'], '', ['{{prf}}' => '{{profile}}']], |
|
21
|
|
|
[['mainframe as400'], '', ['{{as400}}' => '{{mainframe}}']], |
|
22
|
|
|
[ |
|
23
|
|
|
['x' => new Expression('(SELECT id FROM user)')], |
|
24
|
|
|
'', |
|
25
|
|
|
['{{x}}' => new Expression('(SELECT id FROM user)')], |
|
26
|
|
|
], |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @dataProvider tablesNameDataProvider |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testCleanUpTableNames(array $tables, string $prefixDatabase, array $expected): void |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$connection = $this->createConnectionMock(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals( |
|
38
|
|
|
$expected, |
|
39
|
|
|
$this->createQueryHelper()->cleanUpTableNames($tables, new Quoter('"', '"')) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testCleanUpTableNamesException(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$connection = $this->createConnectionMock(); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
48
|
|
|
$this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.'); |
|
49
|
|
|
$this->createQueryHelper()->cleanUpTableNames( |
|
50
|
|
|
[new Expression('(SELECT id FROM user)')], |
|
51
|
|
|
new Quoter('"', '"') |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function filterConditionDataProvider(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
/* like */ |
|
59
|
|
|
[['like', 'name', []], []], |
|
60
|
|
|
[['not like', 'name', []], []], |
|
61
|
|
|
[['or like', 'name', []], []], |
|
62
|
|
|
[['or not like', 'name', []], []], |
|
63
|
|
|
|
|
64
|
|
|
/* not */ |
|
65
|
|
|
[['not', ''], []], |
|
66
|
|
|
|
|
67
|
|
|
/* and */ |
|
68
|
|
|
[['and', '', ''], []], |
|
69
|
|
|
[['and', '', 'id=2'], ['and', 'id=2']], |
|
70
|
|
|
[['and', 'id=1', ''], ['and', 'id=1']], |
|
71
|
|
|
[['and', 'type=1', ['or', '', 'id=2']], ['and', 'type=1', ['or', 'id=2']]], |
|
72
|
|
|
|
|
73
|
|
|
/* or */ |
|
74
|
|
|
[['or', 'id=1', ''], ['or', 'id=1']], |
|
75
|
|
|
[['or', 'type=1', ['or', '', 'id=2']], ['or', 'type=1', ['or', 'id=2']]], |
|
76
|
|
|
|
|
77
|
|
|
/* between */ |
|
78
|
|
|
[['between', 'id', 1, null], []], |
|
79
|
|
|
[['not between', 'id', null, 10], []], |
|
80
|
|
|
|
|
81
|
|
|
/* in */ |
|
82
|
|
|
[['in', 'id', []], []], |
|
83
|
|
|
[['not in', 'id', []], []], |
|
84
|
|
|
|
|
85
|
|
|
/* simple conditions */ |
|
86
|
|
|
[['=', 'a', ''], []], |
|
87
|
|
|
[['>', 'a', ''], []], |
|
88
|
|
|
[['>=', 'a', ''], []], |
|
89
|
|
|
[['<', 'a', ''], []], |
|
90
|
|
|
[['<=', 'a', ''], []], |
|
91
|
|
|
[['<>', 'a', ''], []], |
|
92
|
|
|
[['!=', 'a', ''], []], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @dataProvider filterConditionDataProvider |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testFilterCondition(array|string $condition, array|string $expected): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->assertEquals($expected, $this->createQueryHelper()->filterCondition($condition)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function normalizeOrderByProvider(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
['id', ['id' => 4]], |
|
108
|
|
|
[['id'], ['id']], |
|
109
|
|
|
['name ASC, date DESC', ['name' => 4, 'date' => 3]], |
|
110
|
|
|
[new Expression('SUBSTR(name, 3, 4) DESC, x ASC'), [new Expression('SUBSTR(name, 3, 4) DESC, x ASC')]], |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @dataProvider normalizeOrderByProvider |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testNormalizeOrderBy(array|string|Expression $columns, array|string $expected): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->assertEquals($expected, $this->createQueryHelper()->normalizeOrderBy($columns)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function normalizeSelectProvider(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return [ |
|
125
|
|
|
['exists', ['exists' => 'exists']], |
|
126
|
|
|
['count(*) > 1', ['count(*) > 1']], |
|
127
|
|
|
['name, name, name as X, name as X', ['name' => 'name', 'X' => 'name']], |
|
128
|
|
|
[ |
|
129
|
|
|
['email', 'address', 'status' => new Expression('1')], |
|
130
|
|
|
['email' => 'email', 'address' => 'address', 'status' => new Expression('1')], |
|
131
|
|
|
], |
|
132
|
|
|
[new Expression('1 as Ab'), [new Expression('1 as Ab')]], |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @dataProvider normalizeSelectProvider |
|
138
|
|
|
*/ |
|
139
|
|
|
public function testNormalizeSelect(array|string|Expression $columns, array|string $expected): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->assertEquals($expected, $this->createQueryHelper()->normalizeSelect($columns)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function createQueryHelper(): QueryHelper |
|
145
|
|
|
{ |
|
146
|
|
|
return new QueryHelper(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function createConnectionMock(): ConnectionInterface |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->createMock(ConnectionInterface::class); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.