Visitar URL original
Improve unit tests for Nodes\NodeColumn class · phpmyadmin/phpmyadmin@20f88ec · GitHub
Skip to content

Commit 20f88ec

Browse files
committed
Improve unit tests for Nodes\NodeColumn class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent b060462 commit 20f88ec

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

‎libraries/classes/Navigation/Nodes/NodeColumn.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ private function getColumnIcon(string $key): string
7171
private function getDisplayName(array $item): string
7272
{
7373
$retval = $item['name'];
74-
$flag = 0;
74+
$isFirst = true;
7575
foreach ($item as $key => $value) {
7676
if (empty($value) || $key === 'name') {
7777
continue;
7878
}
7979

80-
$flag == 0 ? $retval .= ' (' : $retval .= ', ';
81-
$flag = 1;
80+
$isFirst ? $retval .= ' (' : $retval .= ', ';
81+
$isFirst = false;
8282
$retval .= $this->getTruncateValue($key, $value);
8383
}
8484

‎psalm-baseline.xml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7546,9 +7546,6 @@
75467546
<PossiblyNullPropertyFetch>
75477547
<code><![CDATA[$this->parent->children]]></code>
75487548
</PossiblyNullPropertyFetch>
7549-
<PossiblyUnusedProperty>
7550-
<code>$displayName</code>
7551-
</PossiblyUnusedProperty>
75527549
<RedundantCastGivenDocblockType>
75537550
<code>(string) $prefix</code>
75547551
</RedundantCastGivenDocblockType>

‎test/classes/Navigation/Nodes/NodeColumnTest.php‎

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,31 @@
44

55
namespace PhpMyAdmin\Tests\Navigation\Nodes;
66

7+
use PhpMyAdmin\Navigation\Nodes\Node;
78
use PhpMyAdmin\Navigation\Nodes\NodeColumn;
89
use PhpMyAdmin\Tests\AbstractTestCase;
910
use PHPUnit\Framework\Attributes\CoversClass;
1011

1112
#[CoversClass(NodeColumn::class)]
12-
class NodeColumnTest extends AbstractTestCase
13+
final class NodeColumnTest extends AbstractTestCase
1314
{
14-
/**
15-
* SetUp for test cases
16-
*/
17-
protected function setUp(): void
15+
public function testColumnNode(): void
1816
{
19-
parent::setUp();
20-
2117
$GLOBALS['dbi'] = $this->createDatabaseInterface();
22-
$GLOBALS['server'] = 0;
23-
}
2418

25-
public function testConstructor(): void
26-
{
27-
$parent = new NodeColumn(['name' => 'name', 'key' => 'key']);
28-
$this->assertEquals(
19+
$nodeColumn = new NodeColumn([
20+
'name' => 'actor_id',
21+
'key' => 'PRI',
22+
'type' => 'smallint',
23+
'default' => null,
24+
'nullable' => '',
25+
]);
26+
$this->assertSame('actor_id', $nodeColumn->name);
27+
$this->assertSame(Node::OBJECT, $nodeColumn->type);
28+
$this->assertFalse($nodeColumn->isGroup);
29+
$this->assertSame('actor_id (PRI, smallint)', $nodeColumn->displayName);
30+
$this->assertSame(['image' => 'b_primary', 'title' => 'Column'], $nodeColumn->icon);
31+
$this->assertSame(
2932
[
3033
'text' => [
3134
'route' => '/table/structure/change',
@@ -37,7 +40,53 @@ public function testConstructor(): void
3740
],
3841
'title' => 'Structure',
3942
],
40-
$parent->links,
43+
$nodeColumn->links,
4144
);
45+
$this->assertSame('field', $nodeColumn->urlParamName);
46+
}
47+
48+
public function testColumnNodeWithTruncatedDefaultValue(): void
49+
{
50+
$GLOBALS['dbi'] = $this->createDatabaseInterface();
51+
52+
$nodeColumn = new NodeColumn([
53+
'name' => 'last_update',
54+
'key' => '',
55+
'type' => 'timestamp',
56+
'default' => 'current_timestamp()',
57+
'nullable' => '',
58+
]);
59+
$this->assertSame('last_update (timestamp, curren...)', $nodeColumn->displayName);
60+
$this->assertSame(['image' => 'pause', 'title' => 'Column'], $nodeColumn->icon);
61+
}
62+
63+
public function testColumnNodeWithTruncatedDefaultValue2(): void
64+
{
65+
$GLOBALS['dbi'] = $this->createDatabaseInterface();
66+
67+
$nodeColumn = new NodeColumn([
68+
'name' => 'email',
69+
'key' => 'UNI',
70+
'type' => 'varchar',
71+
'default' => 'default',
72+
'nullable' => 'nullable',
73+
]);
74+
$this->assertSame('email (UNI, varchar, defaul..., nullable)', $nodeColumn->displayName);
75+
$this->assertSame(['image' => 'bd_primary', 'title' => 'Column'], $nodeColumn->icon);
76+
}
77+
78+
public function testColumnNodeWithoutTruncatedDefaultValue(): void
79+
{
80+
$GLOBALS['dbi'] = $this->createDatabaseInterface();
81+
82+
$nodeColumn = new NodeColumn([
83+
'name' => 'email',
84+
'key' => '',
85+
'type' => 'varchar',
86+
'default' => 'column',
87+
'nullable' => '',
88+
]);
89+
$this->assertSame('email (varchar, column)', $nodeColumn->displayName);
90+
$this->assertSame(['image' => 'pause', 'title' => 'Column'], $nodeColumn->icon);
4291
}
4392
}

0 commit comments

Comments
 (0)