This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace FwlibTest\Db; |
||
3 | |||
4 | use Fwlib\Bridge\Adodb; |
||
5 | use Fwlib\Db\DbDiff; |
||
6 | use Fwlib\Test\AbstractDbRelateTest; |
||
7 | use Fwlib\Util\UtilContainer; |
||
8 | use Fwlib\Util\UtilContainerAwareTrait; |
||
9 | |||
10 | /** |
||
11 | * @copyright Copyright 2012-2015 Fwolf |
||
12 | * @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
||
13 | */ |
||
14 | class DbDiffTest extends AbstractDbRelateTest |
||
15 | { |
||
16 | use UtilContainerAwareTrait; |
||
17 | |||
18 | |||
19 | protected static $dbUsing = 'default'; |
||
20 | |||
21 | protected static $uuid1; |
||
22 | protected static $uuid2; |
||
23 | protected static $uuid3; |
||
24 | |||
25 | public static $getErrorCode; |
||
26 | public static $getErrorMessage; |
||
27 | |||
28 | |||
29 | protected function buildMock() |
||
30 | { |
||
31 | $dbDiff = new DbDiff(self::$db); |
||
0 ignored issues
–
show
|
|||
32 | |||
33 | return $dbDiff; |
||
34 | } |
||
35 | |||
36 | |||
37 | /** |
||
38 | * @return DbDiff |
||
39 | */ |
||
40 | View Code Duplication | protected function buildMockWithFakeDb() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
41 | { |
||
42 | $db = $this->getMockBuilder(Adodb::class) |
||
43 | ->disableOriginalConstructor() |
||
44 | ->getMock( |
||
45 | Adodb::class, |
||
0 ignored issues
–
show
The call to
PHPUnit_Framework_MockOb..._MockBuilder::getMock() has too many arguments starting with \Fwlib\Bridge\Adodb::class .
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 ![]() |
|||
46 | [ |
||
47 | 'BeginTrans', 'CommitTrans', 'RollbackTrans', |
||
48 | 'getErrorCode', 'getErrorMessage', |
||
49 | 'execute' |
||
50 | ] |
||
51 | ); |
||
52 | |||
53 | $db->expects($this->any()) |
||
54 | ->method('getErrorCode') |
||
55 | ->will($this->returnCallback(function () { |
||
56 | return DbDiffTest::$getErrorCode; |
||
57 | })); |
||
58 | |||
59 | $db->expects($this->any()) |
||
60 | ->method('getErrorMessage') |
||
61 | ->will($this->returnCallback(function () { |
||
62 | return DbDiffTest::$getErrorMessage; |
||
63 | })); |
||
64 | |||
65 | $dbDiff = new DbDiff($db); |
||
0 ignored issues
–
show
|
|||
66 | |||
67 | return $dbDiff; |
||
68 | } |
||
69 | |||
70 | |||
71 | View Code Duplication | public static function setUpBeforeClass() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
72 | { |
||
73 | parent::setUpBeforeClass(); |
||
74 | |||
75 | $uuidGenerator = UtilContainer::getInstance()->getUuidBase36(); |
||
76 | |||
77 | self::$uuid1 = $uuidGenerator->generate(); |
||
78 | self::$uuid2 = $uuidGenerator->generate(); |
||
79 | self::$uuid3 = $uuidGenerator->generate(); |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @expectedException \Exception |
||
85 | * @expectedExceptionMessage can't commit again |
||
86 | */ |
||
87 | View Code Duplication | public function testCommitAgain() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
88 | { |
||
89 | $dbDiff = $this->buildMock(); |
||
90 | |||
91 | $json = '{ |
||
92 | "rowCount": 0, |
||
93 | "executeStatus": 100, |
||
94 | "diff": { |
||
95 | "' . self::$tableUser . '": [ |
||
96 | { |
||
97 | "mode": "Whatever", |
||
98 | "pk": [], |
||
99 | "column": [] |
||
100 | } |
||
101 | ] |
||
102 | } |
||
103 | }'; |
||
104 | |||
105 | $dbDiff->import($json); |
||
106 | $dbDiff->commit(); |
||
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @expectedException \Exception |
||
112 | * @expectedExceptionMessage Db execute fail |
||
113 | */ |
||
114 | public function testCommitWithDbFail() |
||
115 | { |
||
116 | $dbDiff = $this->buildMockWithFakeDb(); |
||
117 | |||
118 | $dataNew = [ |
||
0 ignored issues
–
show
$dataNew is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
119 | self::$tableUser => [ |
||
120 | 'uuid' => self::$uuid1, |
||
121 | ], |
||
122 | ]; |
||
123 | |||
124 | self::$getErrorCode = -1; |
||
125 | self::$getErrorMessage = 'Db execute fail'; |
||
126 | |||
127 | $json = '{ |
||
128 | "rowCount": 0, |
||
129 | "executeStatus": 0, |
||
130 | "diff": { |
||
131 | "' . self::$tableUser . '": [ |
||
132 | { |
||
133 | "mode": "INSERT", |
||
134 | "pk": { |
||
135 | "uuid": { |
||
136 | "new": "' . self::$uuid1 . '", |
||
137 | "old": null |
||
138 | } |
||
139 | }, |
||
140 | "column": [] |
||
141 | } |
||
142 | ] |
||
143 | } |
||
144 | }'; |
||
145 | $dbDiff->import($json); |
||
146 | |||
147 | $dbDiff->commit(); |
||
148 | } |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @expectedException \Exception |
||
153 | * @expectedExceptionMessage Invalid mode |
||
154 | */ |
||
155 | View Code Duplication | public function testCommitWithInvalidMode() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
156 | { |
||
157 | $dbDiff = $this->buildMock(); |
||
158 | |||
159 | $json = '{ |
||
160 | "rowCount": 0, |
||
161 | "executeStatus": 0, |
||
162 | "diff": { |
||
163 | "' . self::$tableUser . '": [ |
||
164 | { |
||
165 | "mode": "InvalidMode", |
||
166 | "pk": [], |
||
167 | "column": [] |
||
168 | } |
||
169 | ] |
||
170 | } |
||
171 | }'; |
||
172 | |||
173 | $dbDiff->import($json); |
||
174 | $dbDiff->commit(); |
||
175 | } |
||
176 | |||
177 | |||
178 | /** |
||
179 | * @expectedException \Exception |
||
180 | * @expectedExceptionMessage No diff data |
||
181 | */ |
||
182 | public function testCommitWithoutDiffData() |
||
183 | { |
||
184 | $dbDiff = $this->buildMock(); |
||
185 | $dbDiff->commit(); |
||
186 | } |
||
187 | |||
188 | |||
189 | public function testCompareWithDeleteMode() |
||
190 | { |
||
191 | $dbDiff = $this->buildMock(); |
||
192 | $dataNew = [ |
||
193 | self::$tableUser => [ |
||
194 | 'uuid' => null, |
||
195 | ], |
||
196 | ]; |
||
197 | $dataOld = [ |
||
198 | self::$tableUser => [ |
||
199 | 'uuid' => self::$uuid1, |
||
200 | 'title' => 'User Title The Third', |
||
201 | 'age' => 4200, |
||
202 | 'credit' => '42', |
||
203 | 'joindate' => '2012-01-02', |
||
204 | ], |
||
205 | ]; |
||
206 | |||
207 | $dbDiff->compare($dataNew, $dataOld); |
||
208 | $diff = $dbDiff->getDiff(); |
||
209 | $diff = $diff[self::$tableUser][0]; |
||
210 | |||
211 | $this->assertEquals('DELETE', $diff['mode']); |
||
212 | $this->assertEquals(4, count($diff['column'])); |
||
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @expectedException \Exception |
||
218 | * @expectedExceptionMessage all null |
||
219 | */ |
||
220 | public function testCompareWithNullPkInBothDataNewAndOld() |
||
221 | { |
||
222 | $dbDiff = $this->buildMock(); |
||
223 | $dataNew = [ |
||
224 | self::$tableUser => [ |
||
225 | 'uuid' => null, |
||
226 | ], |
||
227 | ]; |
||
228 | |||
229 | $dbDiff->compare($dataNew); |
||
230 | } |
||
231 | |||
232 | |||
233 | public function testCompareWithUpdateModeWithSameDataNewAndOld() |
||
234 | { |
||
235 | $dbDiff = $this->buildMock(); |
||
236 | $dataNew = [ |
||
237 | self::$tableUser => [ |
||
238 | 'uuid' => self::$uuid1, |
||
239 | 'title' => 'User Title The Third', |
||
240 | 'age' => 4200, |
||
241 | 'credit' => '42', |
||
242 | 'joindate' => '2012-01-02', |
||
243 | ], |
||
244 | ]; |
||
245 | |||
246 | $dbDiff->compare($dataNew, $dataNew); |
||
247 | |||
248 | $this->assertEmpty($dbDiff->getDiff()); |
||
249 | } |
||
250 | |||
251 | |||
252 | public function testExecute() |
||
253 | { |
||
254 | $dbDiff = $this->buildMock(); |
||
255 | |||
256 | // Normal insert |
||
257 | $dataNew1 = [ |
||
258 | self::$tableUser => [ |
||
259 | 'uuid' => self::$uuid1, |
||
260 | 'title' => 'User Title', |
||
261 | 'age' => 42, |
||
262 | 'credit' => '0.42', |
||
263 | 'joindate' => '2014-01-02', |
||
264 | ], |
||
265 | ]; |
||
266 | |||
267 | $dbDiff->execute($dataNew1); |
||
268 | $this->assertEquals(1, $dbDiff->getRowCount()); |
||
269 | |||
270 | $diff = $dbDiff->getDiff(); |
||
271 | $this->assertEquals('INSERT', $diff[self::$tableUser][0]['mode']); |
||
272 | $this->assertEquals(1, count($diff[self::$tableUser][0]['pk'])); |
||
273 | $this->assertEquals(4, count($diff[self::$tableUser][0]['column'])); |
||
274 | |||
275 | $this->assertTrue($dbDiff->isCommitted()); |
||
276 | $this->assertTrue($dbDiff->isExecuted()); |
||
277 | |||
278 | |||
279 | // Insert with PK column only |
||
280 | $dataNew2 = [ |
||
281 | self::$tableUser => [ |
||
282 | 'uuid' => self::$uuid2, |
||
283 | ], |
||
284 | ]; |
||
285 | |||
286 | $dbDiff->execute($dataNew2); |
||
287 | $this->assertEquals(1, $dbDiff->getRowCount()); |
||
288 | $diff = $dbDiff->getDiff(); |
||
289 | $this->assertEquals(0, count($diff[self::$tableUser][0]['column'])); |
||
290 | |||
291 | $this->assertEquals(2, self::$db->getRowCount(self::$tableUser)); |
||
292 | |||
293 | |||
294 | // Update row with $uuid1, and delete row with $uuid2 |
||
295 | $dataNewChanged = [ |
||
296 | self::$tableUser => [ |
||
297 | // Modify from $dataNew1 |
||
298 | [ |
||
299 | 'uuid' => self::$uuid1, |
||
300 | 'title' => 'User Title Changed', |
||
301 | 'age' => 420, |
||
302 | 'credit' => '4.2', |
||
303 | 'joindate' => '2013-01-02', |
||
304 | ], |
||
305 | [ |
||
306 | 'uuid' => null, |
||
307 | ] |
||
308 | ], |
||
309 | ]; |
||
310 | $dataOld = [ |
||
311 | self::$tableUser => [ |
||
312 | $dataNew1[self::$tableUser], |
||
313 | $dataNew2[self::$tableUser], |
||
314 | ], |
||
315 | ]; |
||
316 | $dbDiff->execute($dataNewChanged, $dataOld); |
||
317 | |||
318 | $this->assertEquals(2, $dbDiff->getRowCount()); |
||
319 | $this->assertEquals( |
||
320 | 420, |
||
321 | self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid') |
||
322 | ); |
||
323 | $this->assertEquals(1, self::$db->getRowCount(self::$tableUser)); |
||
324 | |||
325 | |||
326 | // Rollback last update and delete |
||
327 | $dbDiff->rollback(); |
||
328 | |||
329 | $this->assertTrue($dbDiff->isRollbacked()); |
||
330 | $this->assertEquals( |
||
331 | 42, |
||
332 | self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid') |
||
333 | ); |
||
334 | $this->assertEquals(2, self::$db->getRowCount(self::$tableUser)); |
||
335 | |||
336 | |||
337 | // Then commit again |
||
338 | $dbDiff->commit(); |
||
339 | |||
340 | $this->assertTrue($dbDiff->isCommitted()); |
||
341 | $this->assertEquals( |
||
342 | 420, |
||
343 | self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid') |
||
344 | ); |
||
345 | $this->assertEquals(1, self::$db->getRowCount(self::$tableUser)); |
||
346 | } |
||
347 | |||
348 | |||
349 | /** |
||
350 | * @expectedException \Exception |
||
351 | * @expectedExceptionMessage can't execute again |
||
352 | */ |
||
353 | View Code Duplication | public function testExecuteAgain() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
354 | { |
||
355 | $dbDiff = $this->buildMock(); |
||
356 | |||
357 | $json = '{ |
||
358 | "rowCount": 0, |
||
359 | "executeStatus": 100, |
||
360 | "diff": { |
||
361 | "' . self::$tableUser . '": [ |
||
362 | { |
||
363 | "mode": "Whatever", |
||
364 | "pk": [], |
||
365 | "column": [] |
||
366 | } |
||
367 | ] |
||
368 | } |
||
369 | }'; |
||
370 | |||
371 | $dbDiff->import($json); |
||
372 | $dbDiff->execute(); |
||
373 | } |
||
374 | |||
375 | |||
376 | public function testExecuteInsertThenRollback() |
||
377 | { |
||
378 | $dbDiff = $this->buildMock(); |
||
379 | |||
380 | $dataNew = [ |
||
381 | self::$tableUser => [ |
||
382 | 'uuid' => self::$uuid3, |
||
383 | ], |
||
384 | ]; |
||
385 | |||
386 | $condition = "WHERE uuid = '" . self::$uuid3 . "'"; |
||
387 | |||
388 | |||
389 | // Insert |
||
390 | $dbDiff->execute($dataNew); |
||
391 | $this->assertEquals( |
||
392 | 1, |
||
393 | self::$db->getRowCount(self::$tableUser, $condition) |
||
394 | ); |
||
395 | |||
396 | |||
397 | // Export to json and import back |
||
398 | $dbDiff->import($dbDiff->export()); |
||
399 | |||
400 | |||
401 | // Rollback |
||
402 | $dbDiff->rollback(); |
||
403 | $this->assertEquals( |
||
404 | 0, |
||
405 | self::$db->getRowCount(self::$tableUser, $condition) |
||
406 | ); |
||
407 | } |
||
408 | |||
409 | |||
410 | /** |
||
411 | * @expectedException \Exception |
||
412 | * @expectedExceptionMessage empty |
||
413 | */ |
||
414 | public function testExecuteWithEmptyDataNew() |
||
415 | { |
||
416 | $dbDiff = $this->buildMock(); |
||
417 | $dbDiff->execute([], null); |
||
418 | } |
||
419 | |||
420 | |||
421 | /** |
||
422 | * @expectedException \Exception |
||
423 | * @expectedExceptionMessage PK not all assigned |
||
424 | */ |
||
425 | View Code Duplication | public function testExecuteWithNotEnoughPkInDataNew() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
426 | { |
||
427 | $dbDiff = $this->buildMock(); |
||
428 | |||
429 | // No PK column uuid |
||
430 | $dataNew = [ |
||
431 | self::$tableUser => [ |
||
432 | 'title' => 'User Title', |
||
433 | ], |
||
434 | ]; |
||
435 | |||
436 | $dbDiff->execute($dataNew); |
||
437 | } |
||
438 | |||
439 | |||
440 | /** |
||
441 | * @expectedException \Exception |
||
442 | * @expectedExceptionMessage must have PK |
||
443 | */ |
||
444 | View Code Duplication | public function testExecuteWithTableHaveNoPk() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
445 | { |
||
446 | $dbDiff = $this->buildMock(); |
||
447 | |||
448 | // No PK column uuid |
||
449 | $dataNew = [ |
||
450 | 'table_not_exist' => [ |
||
451 | 'title' => 'User Title', |
||
452 | ], |
||
453 | ]; |
||
454 | |||
455 | $dbDiff->execute($dataNew); |
||
456 | } |
||
457 | |||
458 | |||
459 | /** |
||
460 | * @expectedException \Exception |
||
461 | * @expectedExceptionMessage Invalid json |
||
462 | */ |
||
463 | public function testImportInvalidJson() |
||
464 | { |
||
465 | $dbDiff = $this->buildMock(); |
||
466 | |||
467 | $dbDiff->import('{}'); |
||
468 | } |
||
469 | |||
470 | |||
471 | /** |
||
472 | * @expectedException \Exception |
||
473 | * @expectedExceptionMessage can't rollback again |
||
474 | */ |
||
475 | View Code Duplication | public function testRollbackAgain() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
476 | { |
||
477 | $dbDiff = $this->buildMock(); |
||
478 | |||
479 | $json = '{ |
||
480 | "rowCount": 0, |
||
481 | "executeStatus": -100, |
||
482 | "diff": { |
||
483 | "' . self::$tableUser . '": [ |
||
484 | { |
||
485 | "mode": "Whatever", |
||
486 | "pk": [], |
||
487 | "column": [] |
||
488 | } |
||
489 | ] |
||
490 | } |
||
491 | }'; |
||
492 | |||
493 | $dbDiff->import($json); |
||
494 | $dbDiff->rollback(); |
||
495 | } |
||
496 | |||
497 | |||
498 | /** |
||
499 | * @expectedException \Exception |
||
500 | * @expectedExceptionMessage Db execute fail |
||
501 | */ |
||
502 | public function testRollbackWithDbFail() |
||
503 | { |
||
504 | $dbDiff = $this->buildMockWithFakeDb(); |
||
505 | |||
506 | $dataNew = [ |
||
0 ignored issues
–
show
$dataNew is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
507 | self::$tableUser => [ |
||
508 | 'uuid' => self::$uuid1, |
||
509 | ], |
||
510 | ]; |
||
511 | |||
512 | self::$getErrorCode = -1; |
||
513 | self::$getErrorMessage = 'Db execute fail'; |
||
514 | |||
515 | $json = '{ |
||
516 | "rowCount": 0, |
||
517 | "executeStatus": 100, |
||
518 | "diff": { |
||
519 | "' . self::$tableUser . '": [ |
||
520 | { |
||
521 | "mode": "INSERT", |
||
522 | "pk": { |
||
523 | "uuid": { |
||
524 | "new": "' . self::$uuid1 . '", |
||
525 | "old": null |
||
526 | } |
||
527 | }, |
||
528 | "column": [] |
||
529 | } |
||
530 | ] |
||
531 | } |
||
532 | }'; |
||
533 | $dbDiff->import($json); |
||
534 | |||
535 | $dbDiff->rollback(); |
||
536 | } |
||
537 | |||
538 | |||
539 | /** |
||
540 | * @expectedException \Exception |
||
541 | * @expectedExceptionMessage Invalid mode |
||
542 | */ |
||
543 | View Code Duplication | public function testRollbackWithInvalidMode() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
544 | { |
||
545 | $dbDiff = $this->buildMock(); |
||
546 | |||
547 | $json = '{ |
||
548 | "rowCount": 0, |
||
549 | "executeStatus": 0, |
||
550 | "diff": { |
||
551 | "' . self::$tableUser . '": [ |
||
552 | { |
||
553 | "mode": "InvalidMode", |
||
554 | "pk": [], |
||
555 | "column": [] |
||
556 | } |
||
557 | ] |
||
558 | } |
||
559 | }'; |
||
560 | |||
561 | $dbDiff->import($json); |
||
562 | $dbDiff->rollback(); |
||
563 | } |
||
564 | |||
565 | |||
566 | /** |
||
567 | * @expectedException \Exception |
||
568 | * @expectedExceptionMessage No diff data |
||
569 | */ |
||
570 | public function testRollbackWithoutDiffData() |
||
571 | { |
||
572 | $dbDiff = $this->buildMock(); |
||
573 | $dbDiff->rollback(); |
||
574 | } |
||
575 | } |
||
576 |
This class, trait or interface has been deprecated.