Skip to content

Commit daa252b

Browse files
committed
Improve unit tests for Navigation\Nodes\Node class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 6a6929c commit daa252b

1 file changed

Lines changed: 65 additions & 91 deletions

File tree

test/classes/Navigation/Nodes/NodeTest.php

Lines changed: 65 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,39 @@ public function testNumChildren(): void
246246
$this->assertSame(3, $parent->numChildren());
247247
}
248248

249-
/**
250-
* Tests private method _getWhereClause()
251-
*/
249+
public function testGetPaths(): void
250+
{
251+
$parent = new Node('parent');
252+
$group = new Node('group', Node::CONTAINER, true);
253+
$childOne = new Node('child one');
254+
$container = new Node('container', Node::CONTAINER);
255+
$childTwo = new Node('child two');
256+
$parent->addChild($group);
257+
$group->addChild($childOne);
258+
$childOne->addChild($container);
259+
$container->addChild($childTwo);
260+
$this->assertSame(
261+
[
262+
'aPath' => 'cGFyZW50.Y2hpbGQgb25l.Y29udGFpbmVy.Y2hpbGQgdHdv',
263+
'aPath_clean' => ['parent', 'child one', 'container', 'child two'],
264+
'vPath' => 'cGFyZW50.Z3JvdXA=.Y2hpbGQgb25l.Y29udGFpbmVy.Y2hpbGQgdHdv',
265+
'vPath_clean' => ['parent', 'group', 'child one', 'container', 'child two'],
266+
],
267+
$childTwo->getPaths(),
268+
);
269+
}
270+
252271
public function testGetWhereClause(): void
253272
{
254273
$GLOBALS['dbi'] = $this->createDatabaseInterface();
255274
$method = new ReflectionMethod(Node::class, 'getWhereClause');
256275

257276
// Vanilla case
258277
$node = new Node('default');
259-
$this->assertEquals(
260-
'WHERE TRUE ',
261-
$method->invoke($node, 'SCHEMA_NAME'),
262-
);
278+
$this->assertSame('WHERE TRUE ', $method->invoke($node, 'SCHEMA_NAME'));
263279

264280
// When a schema names is passed as search clause
265-
$this->assertEquals(
281+
$this->assertSame(
266282
"WHERE TRUE AND `SCHEMA_NAME` LIKE '%schemaName%' ",
267283
$method->invoke($node, 'SCHEMA_NAME', 'schemaName'),
268284
);
@@ -273,40 +289,37 @@ public function testGetWhereClause(): void
273289

274290
// When hide_db regular expression is present
275291
$GLOBALS['cfg']['Server']['hide_db'] = 'regexpHideDb';
276-
$this->assertEquals(
292+
$this->assertSame(
277293
"WHERE TRUE AND `SCHEMA_NAME` NOT REGEXP 'regexpHideDb' ",
278294
$method->invoke($node, 'SCHEMA_NAME'),
279295
);
280296
unset($GLOBALS['cfg']['Server']['hide_db']);
281297

282298
// When only_db directive is present and it's a single db
283299
$GLOBALS['cfg']['Server']['only_db'] = 'stringOnlyDb';
284-
$this->assertEquals(
300+
$this->assertSame(
285301
"WHERE TRUE AND ( `SCHEMA_NAME` LIKE 'stringOnlyDb' ) ",
286302
$method->invoke($node, 'SCHEMA_NAME'),
287303
);
288304
unset($GLOBALS['cfg']['Server']['only_db']);
289305

290306
// When only_db directive is present and it's an array of dbs
291307
$GLOBALS['cfg']['Server']['only_db'] = ['onlyDbOne', 'onlyDbTwo'];
292-
$this->assertEquals(
308+
$this->assertSame(
293309
'WHERE TRUE AND ( `SCHEMA_NAME` LIKE \'onlyDbOne\' OR `SCHEMA_NAME` LIKE \'onlyDbTwo\' ) ',
294310
$method->invoke($node, 'SCHEMA_NAME'),
295311
);
296312
unset($GLOBALS['cfg']['Server']['only_db']);
297313
}
298314

299315
/**
300-
* Tests getData() method when DisableIS is false and navigation tree
301-
* grouping enabled.
316+
* Tests when DisableIS is false and navigation tree grouping enabled.
302317
*/
303318
public function testGetDataWithEnabledISAndGroupingEnabled(): void
304319
{
305-
$pos = 10;
306-
$limit = 20;
307320
$GLOBALS['cfg']['Server']['DisableIS'] = false;
308321
$GLOBALS['cfg']['NavigationTreeEnableGrouping'] = true;
309-
$GLOBALS['cfg']['FirstLevelNavigationItems'] = $limit;
322+
$GLOBALS['cfg']['FirstLevelNavigationItems'] = 20;
310323
$GLOBALS['cfg']['NavigationTreeDbSeparator'] = '_';
311324

312325
$expectedSql = 'SELECT `SCHEMA_NAME` ';
@@ -321,7 +334,7 @@ public function testGetDataWithEnabledISAndGroupingEnabled(): void
321334
$expectedSql .= 'WHERE TRUE ';
322335
$expectedSql .= ') t ';
323336
$expectedSql .= 'ORDER BY DB_first_level ASC ';
324-
$expectedSql .= 'LIMIT ' . $pos . ', ' . $limit;
337+
$expectedSql .= 'LIMIT 10, 20';
325338
$expectedSql .= ') t2 ';
326339
$expectedSql .= "WHERE TRUE AND 1 = LOCATE(CONCAT(DB_first_level, '_'), ";
327340
$expectedSql .= "CONCAT(SCHEMA_NAME, '_')) ";
@@ -333,75 +346,55 @@ public function testGetDataWithEnabledISAndGroupingEnabled(): void
333346
'navigationhiding' => 'navigationhiding',
334347
]);
335348

336-
// It would have been better to mock _getWhereClause method
337-
// but strangely, mocking private methods is not supported in PHPUnit
338-
$node = new Node('default');
349+
$node = new Node('node');
339350

340-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
341-
->disableOriginalConstructor()
342-
->getMock();
343-
$dbi->expects($this->once())
344-
->method('fetchResult')
345-
->with($expectedSql);
346-
$dbi->expects($this->any())->method('escapeString')
347-
->will($this->returnArgument(0));
351+
$dbi = $this->createMock(DatabaseInterface::class);
352+
$dbi->expects($this->once())->method('fetchResult')->with($expectedSql);
353+
$dbi->expects($this->any())->method('escapeString')->will($this->returnArgument(0));
348354
$GLOBALS['dbi'] = $dbi;
349-
$node->getData($relationParameters, '', $pos);
355+
$node->getData($relationParameters, '', 10);
350356
}
351357

352358
/**
353-
* Tests getData() method when DisableIS is false and navigation tree
354-
* grouping disabled.
359+
* Tests when DisableIS is false and navigation tree grouping disabled.
355360
*/
356361
public function testGetDataWithEnabledISAndGroupingDisabled(): void
357362
{
358-
$pos = 10;
359-
$limit = 20;
360363
$GLOBALS['cfg']['Server']['DisableIS'] = false;
361364
$GLOBALS['cfg']['NavigationTreeEnableGrouping'] = false;
362-
$GLOBALS['cfg']['FirstLevelNavigationItems'] = $limit;
365+
$GLOBALS['cfg']['FirstLevelNavigationItems'] = 20;
363366

364367
$expectedSql = 'SELECT `SCHEMA_NAME` ';
365368
$expectedSql .= 'FROM `INFORMATION_SCHEMA`.`SCHEMATA` ';
366369
$expectedSql .= 'WHERE TRUE ';
367370
$expectedSql .= 'ORDER BY `SCHEMA_NAME` ';
368-
$expectedSql .= 'LIMIT ' . $pos . ', ' . $limit;
371+
$expectedSql .= 'LIMIT 10, 20';
369372

370373
$relationParameters = RelationParameters::fromArray([
371374
'db' => 'pmadb',
372375
'navwork' => true,
373376
'navigationhiding' => 'navigationhiding',
374377
]);
375378

376-
// It would have been better to mock _getWhereClause method
377-
// but strangely, mocking private methods is not supported in PHPUnit
378-
$node = new Node('default');
379+
$node = new Node('node');
379380

380-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
381-
->disableOriginalConstructor()
382-
->getMock();
383-
$dbi->expects($this->once())
384-
->method('fetchResult')
385-
->with($expectedSql);
386-
$dbi->expects($this->any())->method('escapeString')
387-
->will($this->returnArgument(0));
381+
$dbi = $this->createMock(DatabaseInterface::class);
382+
$dbi->expects($this->once())->method('fetchResult')->with($expectedSql);
383+
$dbi->expects($this->any())->method('escapeString')->will($this->returnArgument(0));
388384

389385
$GLOBALS['dbi'] = $dbi;
390-
$node->getData($relationParameters, '', $pos);
386+
$node->getData($relationParameters, '', 10);
391387
}
392388

393389
/**
394-
* Tests getData() method when DisableIS is true and navigation tree
395-
* grouping enabled.
390+
* Tests when DisableIS is true and navigation tree grouping enabled.
396391
*/
397392
public function testGetDataWithDisabledISAndGroupingEnabled(): void
398393
{
399-
$pos = 0;
400-
$limit = 10;
401394
$GLOBALS['cfg']['Server']['DisableIS'] = true;
402395
$GLOBALS['dbs_to_test'] = false;
403396
$GLOBALS['cfg']['NavigationTreeEnableGrouping'] = true;
404-
$GLOBALS['cfg']['FirstLevelNavigationItems'] = $limit;
397+
$GLOBALS['cfg']['FirstLevelNavigationItems'] = 10;
405398
$GLOBALS['cfg']['NavigationTreeDbSeparator'] = '_';
406399

407400
$relationParameters = RelationParameters::fromArray([
@@ -410,13 +403,11 @@ public function testGetDataWithDisabledISAndGroupingEnabled(): void
410403
'navigationhiding' => 'navigationhiding',
411404
]);
412405

413-
$node = new Node('default');
406+
$node = new Node('node');
414407

415408
$resultStub = $this->createMock(DummyResult::class);
416409

417-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
418-
->disableOriginalConstructor()
419-
->getMock();
410+
$dbi = $this->createMock(DatabaseInterface::class);
420411
$dbi->expects($this->once())
421412
->method('tryQuery')
422413
->with("SHOW DATABASES WHERE TRUE AND `Database` LIKE '%db%' ")
@@ -440,12 +431,11 @@ public function testGetDataWithDisabledISAndGroupingEnabled(): void
440431
->will($this->returnArgument(0));
441432

442433
$GLOBALS['dbi'] = $dbi;
443-
$node->getData($relationParameters, '', $pos, 'db');
434+
$node->getData($relationParameters, '', 0, 'db');
444435
}
445436

446437
/**
447-
* Tests the getPresence method when DisableIS is false and navigation tree
448-
* grouping enabled.
438+
* Tests when DisableIS is false and navigation tree grouping enabled.
449439
*/
450440
public function testGetPresenceWithEnabledISAndGroupingEnabled(): void
451441
{
@@ -461,23 +451,16 @@ public function testGetPresenceWithEnabledISAndGroupingEnabled(): void
461451
$query .= 'WHERE TRUE ';
462452
$query .= ') t ';
463453

464-
// It would have been better to mock _getWhereClause method
465-
// but strangely, mocking private methods is not supported in PHPUnit
466-
$node = new Node('default');
454+
$node = new Node('node');
467455

468-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
469-
->disableOriginalConstructor()
470-
->getMock();
471-
$dbi->expects($this->once())
472-
->method('fetchValue')
473-
->with($query);
456+
$dbi = $this->createMock(DatabaseInterface::class);
457+
$dbi->expects($this->once())->method('fetchValue')->with($query);
474458
$GLOBALS['dbi'] = $dbi;
475-
$node->getPresence();
459+
$this->assertSame(0, $node->getPresence());
476460
}
477461

478462
/**
479-
* Tests the getPresence method when DisableIS is false and navigation tree
480-
* grouping disabled.
463+
* Tests when DisableIS is false and navigation tree grouping disabled.
481464
*/
482465
public function testGetPresenceWithEnabledISAndGroupingDisabled(): void
483466
{
@@ -488,48 +471,39 @@ public function testGetPresenceWithEnabledISAndGroupingDisabled(): void
488471
$query .= 'FROM INFORMATION_SCHEMA.SCHEMATA ';
489472
$query .= 'WHERE TRUE ';
490473

491-
$node = new Node('default');
492-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
493-
->disableOriginalConstructor()
494-
->getMock();
495-
$dbi->expects($this->once())
496-
->method('fetchValue')
497-
->with($query);
474+
$node = new Node('node');
475+
$dbi = $this->createMock(DatabaseInterface::class);
476+
$dbi->expects($this->once())->method('fetchValue')->with($query);
498477
$GLOBALS['dbi'] = $dbi;
499-
$node->getPresence();
478+
$this->assertSame(0, $node->getPresence());
500479
}
501480

502481
/**
503-
* Tests the getPresence method when DisableIS is true
482+
* Tests when DisableIS is true
504483
*/
505484
public function testGetPresenceWithDisabledIS(): void
506485
{
507486
$GLOBALS['cfg']['Server']['DisableIS'] = true;
508487
$GLOBALS['dbs_to_test'] = false;
509488
$GLOBALS['cfg']['NavigationTreeEnableGrouping'] = true;
510489

511-
$node = new Node('default');
490+
$node = new Node('node');
512491

513492
$resultStub = $this->createMock(DummyResult::class);
514493

515494
// test with no search clause
516-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
517-
->disableOriginalConstructor()
518-
->getMock();
495+
$dbi = $this->createMock(DatabaseInterface::class);
519496
$dbi->expects($this->once())
520497
->method('tryQuery')
521498
->with('SHOW DATABASES WHERE TRUE ')
522499
->will($this->returnValue($resultStub));
523-
$dbi->expects($this->any())->method('escapeString')
524-
->will($this->returnArgument(0));
500+
$dbi->expects($this->any())->method('escapeString')->will($this->returnArgument(0));
525501

526502
$GLOBALS['dbi'] = $dbi;
527-
$node->getPresence();
503+
$this->assertSame(0, $node->getPresence());
528504

529505
// test with a search clause
530-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
531-
->disableOriginalConstructor()
532-
->getMock();
506+
$dbi = $this->createMock(DatabaseInterface::class);
533507
$dbi->expects($this->once())
534508
->method('tryQuery')
535509
->with("SHOW DATABASES WHERE TRUE AND `Database` LIKE '%dbname%' ")
@@ -538,6 +512,6 @@ public function testGetPresenceWithDisabledIS(): void
538512
->will($this->returnArgument(0));
539513

540514
$GLOBALS['dbi'] = $dbi;
541-
$node->getPresence('', 'dbname');
515+
$this->assertSame(0, $node->getPresence('', 'dbname'));
542516
}
543517
}

0 commit comments

Comments
 (0)