|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Micayael\NativeQueryFromFileBuilderBundle\Tests\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Exception\NonExistentQueryDirectoryException; |
|
6
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Exception\NonExistentQueryFileException; |
|
7
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Exception\NonExistentQueryKeyException; |
|
8
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Helper\NativeQueryBuilderHelper; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class NativeQueryBuilderHelperTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var NativeQueryBuilderHelper |
|
15
|
|
|
*/ |
|
16
|
|
|
private $helper; |
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->helper = new NativeQueryBuilderHelper(null, __DIR__.'/../queries'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testNonExistentQueryDirectoryException() |
|
24
|
|
|
{ |
|
25
|
|
|
$helper = new NativeQueryBuilderHelper(null, __DIR__.'/../non_existent'); |
|
26
|
|
|
|
|
27
|
|
|
$params = []; |
|
28
|
|
|
|
|
29
|
|
|
$this->expectException(NonExistentQueryDirectoryException::class); |
|
30
|
|
|
$this->expectExceptionMessageRegExp('/El directorio configurado ".+" no existe. Favor verifique la configuración del bundle "native_query_from_file_builder.sql_queries_dir"/'); |
|
31
|
|
|
|
|
32
|
|
|
$helper->getSqlFromYamlKey('clients:product', $params); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testNonExistentQueryFileException() |
|
36
|
|
|
{ |
|
37
|
|
|
$params = []; |
|
38
|
|
|
|
|
39
|
|
|
$this->expectException(NonExistentQueryFileException::class); |
|
40
|
|
|
$this->expectExceptionMessageRegExp('/El archivo de queries solicitado ".+" no existe/'); |
|
41
|
|
|
|
|
42
|
|
|
$this->helper->getSqlFromYamlKey('non_existent:client', $params); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testNonExistentQueryKey() |
|
46
|
|
|
{ |
|
47
|
|
|
$params = []; |
|
48
|
|
|
|
|
49
|
|
|
$this->expectException(NonExistentQueryKeyException::class); |
|
50
|
|
|
$this->expectExceptionMessageRegExp('/El queries solicitado ".+" no existe/'); |
|
51
|
|
|
|
|
52
|
|
|
$this->helper->getSqlFromYamlKey('clients:non_existent', $params); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testSimpleSql() |
|
56
|
|
|
{ |
|
57
|
|
|
$params = []; |
|
58
|
|
|
|
|
59
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients', $params); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals('SELECT * FROM clients', $sql); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
//---------------------------------------------------------------------------------------------- |
|
65
|
|
|
// Required Params |
|
66
|
|
|
//---------------------------------------------------------------------------------------------- |
|
67
|
|
|
|
|
68
|
|
|
public function testSqlWithRequiredParams() |
|
69
|
|
|
{ |
|
70
|
|
|
$params = [ |
|
71
|
|
|
'slug' => 'jhon-doe', |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:client_by_slug', $params); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals('SELECT * FROM clients WHERE slug = :slug', $sql); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
//---------------------------------------------------------------------------------------------- |
|
80
|
|
|
// Snippets (optionals & required) |
|
81
|
|
|
//---------------------------------------------------------------------------------------------- |
|
82
|
|
|
|
|
83
|
|
|
public function testSqlWithOptionalFiltersNotUsingFilters() |
|
84
|
|
|
{ |
|
85
|
|
|
$params = []; |
|
86
|
|
|
|
|
87
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_optional_filters.base', $params); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals('SELECT * FROM clients c ORDER BY c.id DESC', $sql); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testSqlWithOptionalFilterName() |
|
93
|
|
|
{ |
|
94
|
|
|
$params = [ |
|
95
|
|
|
'firstname' => 'Jhon', |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_optional_filters.base', $params); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals('SELECT * FROM clients c WHERE (firstname = :firstname) ORDER BY c.id DESC', $sql); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testSqlWithOptionalFiltersNameAndLastname() |
|
104
|
|
|
{ |
|
105
|
|
|
$params = [ |
|
106
|
|
|
'firstname' => 'Jhon', |
|
107
|
|
|
'lastname' => 'Doe', |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_optional_filters.base', $params); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals('SELECT * FROM clients c WHERE (firstname = :firstname) AND (lastname = :lastname) ORDER BY c.id DESC', $sql); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testSqlWithOptionalFiltersAndWhereIncluded() |
|
116
|
|
|
{ |
|
117
|
|
|
$params = [ |
|
118
|
|
|
'year' => 1983, |
|
119
|
|
|
'firstname' => 'Jhon', |
|
120
|
|
|
'lastname' => 'Doe', |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_optional_filters_and_where_included.base', $params); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals('SELECT * FROM clients c WHERE YEAR(birthday) > :year AND (firstname = :firstname) AND (lastname = :lastname)', $sql); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testSqlWithRequiredKeysAndNoFilters() |
|
129
|
|
|
{ |
|
130
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_required_key.base'); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEquals('SELECT c.id, c.firstname as name, c.lastname FROM clients c', $sql); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testSqlWithRequiredKeyAndFilters() |
|
136
|
|
|
{ |
|
137
|
|
|
$params = [ |
|
138
|
|
|
'firstname' => 'Jhon', |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_required_key.base', $params); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals('SELECT c.id, c.firstname as name, c.lastname FROM clients c WHERE (c.firstname = :firstname)', $sql); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testSqlWithRequiredKeysAndFilters() |
|
147
|
|
|
{ |
|
148
|
|
|
$params = [ |
|
149
|
|
|
'firstname' => 'Jhon', |
|
150
|
|
|
'lastname' => 'Doe', |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_required_keys.base', $params); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals('SELECT c.id, c.firstname as name, c.lastname, YEAR(c.birthday) as year FROM clients c WHERE (c.firstname = :firstname) AND (c.lastname = :lastname)', $sql); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
//---------------------------------------------------------------------------------------------- |
|
159
|
|
|
// Special filters |
|
160
|
|
|
//---------------------------------------------------------------------------------------------- |
|
161
|
|
|
|
|
162
|
|
|
public function testSqlWithWhereIn() |
|
163
|
|
|
{ |
|
164
|
|
|
$params = [ |
|
165
|
|
|
'firstnames' => ['Jhon', 'Mary', 'Steven'], |
|
166
|
|
|
]; |
|
167
|
|
|
|
|
168
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_special_filters.base', $params); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertEquals('SELECT * FROM clients c WHERE (firstname IN(:firstnames_0,:firstnames_1,:firstnames_2))', $sql); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
//---------------------------------------------------------------------------------------------- |
|
174
|
|
|
// Subqueries - Multipart Query |
|
175
|
|
|
//---------------------------------------------------------------------------------------------- |
|
176
|
|
|
|
|
177
|
|
|
public function testSqlWithAnySubquery() |
|
178
|
|
|
{ |
|
179
|
|
|
$params = [ |
|
180
|
|
|
'firstname' => 'Jhon', |
|
181
|
|
|
'date' => '2018-01-01', |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_any_subquery.base', $params); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertEquals('SELECT c.firstname, c.lastname FROM clients c WHERE c.sold > ANY(SELECT s.amount FROM sale s WHERE (s.date > :date) ORDER BY s.amount DESC LIMIT 10) AND (c.firstname = :firstname)', $sql); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
//---------------------------------------------------------------------------------------------- |
|
190
|
|
|
// Pagination |
|
191
|
|
|
//---------------------------------------------------------------------------------------------- |
|
192
|
|
|
|
|
193
|
|
|
public function testSqlWithPagination() |
|
194
|
|
|
{ |
|
195
|
|
|
$params = [ |
|
196
|
|
|
'name' => 'Jhon', |
|
197
|
|
|
'min_date' => '2018-01-01', |
|
198
|
|
|
]; |
|
199
|
|
|
|
|
200
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_pagination.count', $params); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertEquals('SELECT count(1) FROM clients c JOIN sales s on c.id = s.client_id WHERE (c.date >= :min_date) AND (c.firstname like :name)', $sql); |
|
203
|
|
|
|
|
204
|
|
|
$sql = $this->helper->getSqlFromYamlKey('clients:clients_pagination.base', $params); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertEquals('SELECT * FROM clients c JOIN sales s on c.id = s.client_id WHERE (c.date >= :min_date) AND (c.firstname like :name) ORDER BY s.date DESC', $sql); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
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