1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace test; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use \PHPUnit\Framework\TestCase; |
8
|
|
|
use src\pdodatabase\elementos\Campos; |
9
|
|
|
use src\pdodatabase\elementos\CamposYTabla; |
10
|
|
|
use src\pdodatabase\elementos\Como; |
11
|
|
|
use src\pdodatabase\elementos\Tabla; |
12
|
|
|
use src\pdodatabase\elementos\ValidadorDeParametrosWhereBetween; |
13
|
|
|
use src\pdodatabase\elementos\ValidadorDeParametrosWhere; |
14
|
|
|
use src\pdodatabase\elementos\ValidadorDeParametrosWhereAndOthers; |
15
|
|
|
use src\pdodatabase\elementos\Where; |
16
|
|
|
use src\pdodatabase\elementos\WhereAnd; |
17
|
|
|
use src\pdodatabase\elementos\WhereBetween; |
18
|
|
|
use src\pdodatabase\elementos\WhereNotBetween; |
19
|
|
|
use src\pdodatabase\elementos\WhereOr; |
20
|
|
|
use src\pdodatabase\sentencias\select\SentenciaSelect; |
21
|
|
|
use src\pdodatabase\sentencias\select\SentenciaSelectWhere; |
22
|
|
|
|
23
|
|
|
class ElementosTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
//Clase Tabla |
26
|
|
|
|
27
|
|
|
public function testSiLaTablaEstaVaciaLanzaExcepcion() |
28
|
|
|
{ |
29
|
|
|
$this->expectException(Exception::class); |
30
|
|
|
$tabla = new Tabla(''); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testTablaSoloRetornaTexto() |
34
|
|
|
{ |
35
|
|
|
$tabla = new Tabla('Hola'); |
36
|
|
|
$this->assertIsString($tabla->sql()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
//Clase Campos |
40
|
|
|
|
41
|
|
|
public function testSiElCampoEstaVacioLanzaExcepcion() |
42
|
|
|
{ |
43
|
|
|
$this->expectException(Exception::class); |
44
|
|
|
$campos = new Campos([]); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCamposSoloRetornaTexto() |
48
|
|
|
{ |
49
|
|
|
$campos = new Campos(['*','78']); |
50
|
|
|
$this->assertIsString($campos->sql()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
//Clase CamposyTabla |
54
|
|
|
|
55
|
|
|
public function testCamposYTablaDevuelveValorCorrecto() |
56
|
|
|
{ |
57
|
|
|
$tabla = new Tabla('prueba'); |
58
|
|
|
$campos = new Campos(['a,b,c,d']); |
59
|
|
|
|
60
|
|
|
$camposytabla = new CamposYTabla($campos,$tabla); |
61
|
|
|
|
62
|
|
|
$this->assertSame('a,b,c,d FROM prueba',$camposytabla->sql()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
//Clase ValidadorDeParametrosWhere |
66
|
|
|
|
67
|
|
|
public function testValidadorDeParametrosWhereElArrayNoPuedeEstarVacio() |
68
|
|
|
{ |
69
|
|
|
$this->expectException(Exception::class); |
70
|
|
|
new ValidadorDeParametrosWhere([]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testValidadorDeParametrosWhereLaColumnaSoloPuedeSerString() |
74
|
|
|
{ |
75
|
|
|
$this->expectException(Exception::class); |
76
|
|
|
new ValidadorDeParametrosWhere([1,'1','2']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testValidadorDeParametrosWhereNingunCampoPuedeEstarVacio() |
80
|
|
|
{ |
81
|
|
|
$this->expectException(Exception::class); |
82
|
|
|
new ValidadorDeParametrosWhere(['','1','2']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//Clase ValidadorDeParametrosWhereBetween |
86
|
|
|
|
87
|
|
|
public function testValidadorDeParametrosWhereBetweenElArrayNoPuedeEstarVacio() |
88
|
|
|
{ |
89
|
|
|
$this->expectException(Exception::class); |
90
|
|
|
new ValidadorDeParametrosWhereBetween([]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testValidadorDeParametrosWhereBetweenNoPuedeTenerValoresIguales() |
94
|
|
|
{ |
95
|
|
|
$this->expectException(Exception::class); |
96
|
|
|
new ValidadorDeParametrosWhereBetween(['id','1','1']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testValidadorDeParametrosWhereBetweenLaColumnaSoloPuedeSerString() |
100
|
|
|
{ |
101
|
|
|
$this->expectException(Exception::class); |
102
|
|
|
new ValidadorDeParametrosWhereBetween([1,'1','2']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testValidadorDeParametrosWhereBetweenNingunCampoPuedeEstarVacio() |
106
|
|
|
{ |
107
|
|
|
$this->expectException(Exception::class); |
108
|
|
|
new ValidadorDeParametrosWhereBetween(['','1','2']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
//Clase Where |
112
|
|
|
|
113
|
|
|
public function testWhereArrojaStringValido() |
114
|
|
|
{ |
115
|
|
|
$where = new Where( |
116
|
|
|
new ValidadorDeParametrosWhere( |
117
|
|
|
['id','=','21'] |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$this->assertSame('WHERE id = ?', $where->sql()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testWhereArrojaArrayEnMetodoDatos() |
125
|
|
|
{ |
126
|
|
|
$where = new Where( |
127
|
|
|
new ValidadorDeParametrosWhere( |
128
|
|
|
['id','=','21'] |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertIsArray($where->datos()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//Clase ValidadorDeParametrosWhereAndOthers |
136
|
|
|
|
137
|
|
|
public function testValidadorDeParametrosWhereAndOthersElArrayNoPuedeEstarVacio() |
138
|
|
|
{ |
139
|
|
|
$this->expectException(Exception::class); |
140
|
|
|
new ValidadorDeParametrosWhereAndOthers([]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testValidadorDeParametrosWhereAndOthersLaColumnaSoloPuedeSerString() |
144
|
|
|
{ |
145
|
|
|
$this->expectException(Exception::class); |
146
|
|
|
new ValidadorDeParametrosWhereAndOthers([1,'=','2','1','=','2']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testValidadorDeParametrosWhereAndOthersNingunCampoPuedeEstarVacio() |
150
|
|
|
{ |
151
|
|
|
$this->expectException(Exception::class); |
152
|
|
|
new ValidadorDeParametrosWhereAndOthers(['','1','2','','','']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
//Clase Where And |
156
|
|
|
|
157
|
|
|
public function testWhereAndArrojaStringValido() |
158
|
|
|
{ |
159
|
|
|
$where = new WhereAnd( |
160
|
|
|
new ValidadorDeParametrosWhereAndOthers( |
161
|
|
|
['id','=','21','uno','=','1'] |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$this->assertSame('WHERE id = ? AND uno = ?', $where->sql()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
//Clase Where Or |
169
|
|
|
|
170
|
|
|
public function testWhereOrArrojaStringValido() |
171
|
|
|
{ |
172
|
|
|
$where = new WhereOr( |
173
|
|
|
new ValidadorDeParametrosWhereAndOthers( |
174
|
|
|
['id','=','21','uno','=','1'] |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$this->assertSame('WHERE id = ? OR uno = ?', $where->sql()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
//Clase Where Between |
182
|
|
|
|
183
|
|
|
public function testWhereBetweenArrojaStringValido() |
184
|
|
|
{ |
185
|
|
|
$where = new WhereBetween( |
186
|
|
|
new ValidadorDeParametrosWhereBetween( |
187
|
|
|
['id','1','21'] |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$this->assertSame('WHERE BETWEEN id ? AND ?', $where->sql()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
//Clase Where Not Between |
195
|
|
|
|
196
|
|
|
public function testWhereNotBetweenArrojaStringValido() |
197
|
|
|
{ |
198
|
|
|
$where = new WhereNotBetween( |
199
|
|
|
new ValidadorDeParametrosWhereBetween( |
200
|
|
|
['id','1','21'] |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$this->assertSame('WHERE NOT BETWEEN id ? AND ?', $where->sql()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
//Clase Como |
208
|
|
|
|
209
|
|
|
public function testComoArrojaStringValido() |
210
|
|
|
{ |
211
|
|
|
$where = new WhereNotBetween( |
212
|
|
|
new ValidadorDeParametrosWhereBetween( |
213
|
|
|
['id','1','21'] |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$como = new Como( |
218
|
|
|
$where |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
$this->assertSame('WHERE NOT BETWEEN id ? AND ?', $como->sql()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
//Clase Sentencia Select Where |
225
|
|
|
|
226
|
|
|
public function testSentenciaSelectWhereArrojaStringValido() |
227
|
|
|
{ |
228
|
|
|
$where = new WhereNotBetween( |
229
|
|
|
new ValidadorDeParametrosWhereBetween( |
230
|
|
|
['id','1','21'] |
231
|
|
|
) |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$como = new Como( |
235
|
|
|
$where |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
$sentencia = new SentenciaSelectWhere( |
239
|
|
|
new CamposYTabla( |
240
|
|
|
new Campos(['*']), |
241
|
|
|
new Tabla('prueba') |
242
|
|
|
),$como |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$this->assertSame('SELECT * FROM prueba WHERE NOT BETWEEN id ? AND ?', $sentencia->sql()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
//Clase SentenciaSelect |
249
|
|
|
|
250
|
|
|
public function testSentenciaSelectArrojaStringValido() |
251
|
|
|
{ |
252
|
|
|
$sentencia = new SentenciaSelect( |
253
|
|
|
new CamposYTabla( |
254
|
|
|
new Campos(['*']), |
255
|
|
|
new Tabla('prueba') |
256
|
|
|
) |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$this->assertSame('SELECT * FROM prueba', $sentencia->sql()); |
260
|
|
|
} |
261
|
|
|
} |