|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Tests\Service; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use Doctrine\DBAL\Result; |
|
16
|
|
|
use Doctrine\DBAL\Statement; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use PDO; |
|
20
|
|
|
use Throwable; |
|
21
|
|
|
use WBW\Bundle\CoreBundle\Service\StatementService; |
|
22
|
|
|
use WBW\Bundle\CoreBundle\Service\StatementServiceInterface; |
|
23
|
|
|
use WBW\Bundle\CoreBundle\Tests\AbstractWebTestCase; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Statement service test. |
|
27
|
|
|
* |
|
28
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
29
|
|
|
* @package WBW\Bundle\CoreBundle\Tests\Service |
|
30
|
|
|
*/ |
|
31
|
|
|
class StatementServiceTest extends AbstractWebTestCase { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Statement service. |
|
35
|
|
|
* |
|
36
|
|
|
* @var StatementServiceInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $service; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function setUp(): void { |
|
44
|
|
|
parent::setUp(); |
|
45
|
|
|
|
|
46
|
|
|
// Set a Statement service. |
|
47
|
|
|
$this->service = static::$kernel->getContainer()->get(StatementService::SERVICE_NAME . ".alias"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function setUpBeforeClass(): void { |
|
54
|
|
|
parent::setUpBeforeClass(); |
|
55
|
|
|
|
|
56
|
|
|
parent::setUpSchemaTool(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Test executeQueries() |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testExecuteQueries(): void { |
|
66
|
|
|
|
|
67
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteQueriesFile.sql"; |
|
68
|
|
|
|
|
69
|
|
|
$values = array_pad([], 2, [ |
|
70
|
|
|
":date" => [(new DateTime())->format("Y-m-d"), PDO::PARAM_STR], |
|
71
|
|
|
]); |
|
72
|
|
|
$queries = $this->service->readStatementFile($filename); |
|
73
|
|
|
|
|
74
|
|
|
$obj = $this->service; |
|
75
|
|
|
|
|
76
|
|
|
$res = $obj->executeQueries($queries, $values); |
|
77
|
|
|
$this->assertCount(2, $res); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertInstanceOf(Result::class, $res[0]); |
|
80
|
|
|
$this->assertInstanceOf(Result::class, $res[1]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Test executeQueriesFile() |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testExecuteQueriesFile(): void { |
|
90
|
|
|
|
|
91
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteQueriesFile.sql"; |
|
92
|
|
|
|
|
93
|
|
|
$values = array_pad([], 2, [ |
|
94
|
|
|
":date" => [(new DateTime())->format("Y-m-d"), PDO::PARAM_STR], |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
$obj = $this->service; |
|
98
|
|
|
|
|
99
|
|
|
$res = $obj->executeQueriesFile($filename, $values); |
|
100
|
|
|
$this->assertCount(2, $res); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertInstanceOf(Result::class, $res[0]); |
|
103
|
|
|
$this->assertInstanceOf(Result::class, $res[1]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Test executeQuery() |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testExecuteQuery(): void { |
|
113
|
|
|
|
|
114
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteQueryFile.sql"; |
|
115
|
|
|
|
|
116
|
|
|
$values = [ |
|
117
|
|
|
":date" => [(new DateTime())->format("Y-m-d"), PDO::PARAM_STR], |
|
118
|
|
|
]; |
|
119
|
|
|
$query = $this->service->readStatementFile($filename); |
|
120
|
|
|
|
|
121
|
|
|
$obj = $this->service; |
|
122
|
|
|
|
|
123
|
|
|
$res = $obj->executeQuery($query, $values); |
|
124
|
|
|
$this->assertInstanceOf(Result::class, $res); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Test executeQueryFile() |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testExecuteQueryFile(): void { |
|
134
|
|
|
|
|
135
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteQueryFile.sql"; |
|
136
|
|
|
|
|
137
|
|
|
$values = [ |
|
138
|
|
|
":date" => [(new DateTime())->format("Y-m-d"), PDO::PARAM_STR], |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$obj = $this->service; |
|
142
|
|
|
|
|
143
|
|
|
$res = $obj->executeQueryFile($filename, $values); |
|
144
|
|
|
$this->assertInstanceOf(Result::class, $res); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Test executeStatement() |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function testExecuteStatement(): void { |
|
154
|
|
|
|
|
155
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteStatementFile.sql"; |
|
156
|
|
|
|
|
157
|
|
|
$values = [ |
|
158
|
|
|
":id" => [1, PDO::PARAM_INT], |
|
159
|
|
|
]; |
|
160
|
|
|
$query = $this->service->readStatementFile($filename); |
|
161
|
|
|
|
|
162
|
|
|
$obj = $this->service; |
|
163
|
|
|
|
|
164
|
|
|
$res = $obj->executeStatement($query, $values); |
|
165
|
|
|
$this->assertEquals(0, $res); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Test executeStatementFile() |
|
170
|
|
|
* |
|
171
|
|
|
* @return void |
|
172
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testExecuteStatementFile(): void { |
|
175
|
|
|
|
|
176
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteStatementFile.sql"; |
|
177
|
|
|
|
|
178
|
|
|
$values = [ |
|
179
|
|
|
":id" => [1, PDO::PARAM_INT], |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
$obj = $this->service; |
|
183
|
|
|
|
|
184
|
|
|
$res = $obj->executeStatementFile($filename, $values); |
|
185
|
|
|
$this->assertEquals(0, $res); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Test executeStatements() |
|
190
|
|
|
* |
|
191
|
|
|
* @return void |
|
192
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testExecuteStatements(): void { |
|
195
|
|
|
|
|
196
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteStatementsFile.sql"; |
|
197
|
|
|
|
|
198
|
|
|
$values = array_pad([], 2, [ |
|
199
|
|
|
":id" => [1, PDO::PARAM_INT], |
|
200
|
|
|
]); |
|
201
|
|
|
$queries = $this->service->readStatementFile($filename); |
|
202
|
|
|
|
|
203
|
|
|
$obj = $this->service; |
|
204
|
|
|
|
|
205
|
|
|
$res = $obj->executeStatements($queries, $values); |
|
206
|
|
|
$this->assertCount(2, $res); |
|
207
|
|
|
|
|
208
|
|
|
$this->assertEquals(0, $res[0]); |
|
209
|
|
|
$this->assertEquals(0, $res[1]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Test executeStatementsFile() |
|
214
|
|
|
* |
|
215
|
|
|
* @return void |
|
216
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
217
|
|
|
*/ |
|
218
|
|
|
public function testExecuteStatementsFile(): void { |
|
219
|
|
|
|
|
220
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteStatementsFile.sql"; |
|
221
|
|
|
|
|
222
|
|
|
$values = array_pad([], 2, [ |
|
223
|
|
|
":id" => [1, PDO::PARAM_INT], |
|
224
|
|
|
]); |
|
225
|
|
|
|
|
226
|
|
|
$obj = $this->service; |
|
227
|
|
|
|
|
228
|
|
|
$res = $obj->executeStatementsFile($filename, $values); |
|
229
|
|
|
$this->assertCount(2, $res); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertEquals(0, $res[0]); |
|
232
|
|
|
$this->assertEquals(0, $res[1]); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Test prepareStatement() |
|
237
|
|
|
* |
|
238
|
|
|
* @return void |
|
239
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
240
|
|
|
*/ |
|
241
|
|
|
public function testPrepareStatement(): void { |
|
242
|
|
|
|
|
243
|
|
|
$values = [ |
|
244
|
|
|
":date" => [(new DateTime())->format("Y-m-d"), PDO::PARAM_STR], |
|
245
|
|
|
]; |
|
246
|
|
|
$query = "SELECT :date"; |
|
247
|
|
|
|
|
248
|
|
|
$obj = $this->service; |
|
249
|
|
|
|
|
250
|
|
|
$res = $obj->prepareStatement($query, $values); |
|
251
|
|
|
$this->assertInstanceOf(Statement::class, $res); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Test readStatementFile() |
|
256
|
|
|
* |
|
257
|
|
|
* @return void |
|
258
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
259
|
|
|
*/ |
|
260
|
|
|
public function testReadStatementFile(): void { |
|
261
|
|
|
|
|
262
|
|
|
$obj = $this->service; |
|
263
|
|
|
|
|
264
|
|
|
$res = $obj->readStatementFile(__FILE__); |
|
265
|
|
|
$this->assertNotNull($res); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Test readStatementFile() |
|
270
|
|
|
* |
|
271
|
|
|
* @return void |
|
272
|
|
|
*/ |
|
273
|
|
|
public function testReadStatementFileWithInvalidArgumentException(): void { |
|
274
|
|
|
|
|
275
|
|
|
// Set a filename mock. |
|
276
|
|
|
$filename = __FILE__ . "~"; |
|
277
|
|
|
|
|
278
|
|
|
$obj = $this->service; |
|
279
|
|
|
|
|
280
|
|
|
try { |
|
281
|
|
|
|
|
282
|
|
|
$obj->readStatementFile($filename); |
|
283
|
|
|
} catch (Throwable $ex) { |
|
284
|
|
|
|
|
285
|
|
|
$this->assertInstanceOf(InvalidArgumentException::class, $ex); |
|
286
|
|
|
$this->assertEquals('The file "' . $filename . '" was not found', $ex->getMessage()); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Test splitStatements() |
|
292
|
|
|
* |
|
293
|
|
|
* @return void |
|
294
|
|
|
*/ |
|
295
|
|
|
public function testSplitStatements(): void { |
|
296
|
|
|
|
|
297
|
|
|
$filename = __DIR__ . "/StatementServiceTest.testExecuteQueriesFile.sql"; |
|
298
|
|
|
|
|
299
|
|
|
$queries = $this->service->readStatementFile($filename); |
|
300
|
|
|
|
|
301
|
|
|
$obj = $this->service; |
|
302
|
|
|
|
|
303
|
|
|
$res = $obj->splitStatements($queries); |
|
304
|
|
|
$this->assertCount(2, $res); |
|
305
|
|
|
|
|
306
|
|
|
$this->assertEquals("SELECT :date;", $res[0]); |
|
307
|
|
|
$this->assertEquals("SELECT :date;", $res[1]); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Test __construct() |
|
312
|
|
|
* |
|
313
|
|
|
* @return void |
|
314
|
|
|
*/ |
|
315
|
|
|
public function test__construct(): void { |
|
316
|
|
|
|
|
317
|
|
|
// Set an Entity manager mock. |
|
318
|
|
|
$entityManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
319
|
|
|
|
|
320
|
|
|
$this->assertEquals("wbw.core.service.statement", StatementService::SERVICE_NAME); |
|
321
|
|
|
|
|
322
|
|
|
$obj = new StatementService($entityManager); |
|
323
|
|
|
|
|
324
|
|
|
$this->assertInstanceOf(StatementServiceInterface::class, $obj); |
|
325
|
|
|
|
|
326
|
|
|
$this->assertSame($entityManager, $obj->getEntityManager()); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|