Skip to content

Commit

Permalink
Add Config\Settings\Console::asArray() method
Browse files Browse the repository at this point in the history
Refactor the test to test each property individually.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 28, 2023
1 parent 2a4eb5a commit 7f5a1b7
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 143 deletions.
4 changes: 2 additions & 2 deletions libraries/classes/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2604,11 +2604,11 @@ public function __construct(array $settings)
$this->FirstDayOfCalendar = $this->setFirstDayOfCalendar($settings);
}

/** @return array<string, array|bool|int|string|null> */
/** @return array<string, array<mixed>|bool|int|string|null> */
public function toArray(): array
{
$settings = get_object_vars($this);
$settings['Console'] = get_object_vars($this->Console);
$settings['Console'] = $this->Console->asArray();
$settings['DBG'] = get_object_vars($this->DBG);
$settings['SQLQuery'] = get_object_vars($this->SQLQuery);
$settings['Export'] = get_object_vars($this->Export);
Expand Down
17 changes: 17 additions & 0 deletions libraries/classes/Config/Settings/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ public function __construct(array $console = [])
$this->Order = $this->setOrder($console);
}

/** @return array<string, string|bool|int> */
public function asArray(): array
{
return [
'StartHistory' => $this->StartHistory,
'AlwaysExpand' => $this->AlwaysExpand,
'CurrentQuery' => $this->CurrentQuery,
'EnterExecutes' => $this->EnterExecutes,
'DarkTheme' => $this->DarkTheme,
'Mode' => $this->Mode,
'Height' => $this->Height,
'GroupQueries' => $this->GroupQueries,
'OrderBy' => $this->OrderBy,
'Order' => $this->Order,
];
}

/** @param mixed[] $console */
private function setStartHistory(array $console): bool
{
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,6 @@ parameters:
count: 44
path: libraries/classes/Config/Settings.php

-
message: "#^Method PhpMyAdmin\\\\Config\\\\Settings\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Config/Settings.php

-
message: "#^Method PhpMyAdmin\\\\Config\\\\Settings\\:\\:toArray\\(\\) should return array\\<string, array\\|bool\\|int\\|string\\|null\\> but returns array\\<string, mixed\\>\\.$#"
count: 1
Expand Down Expand Up @@ -9610,11 +9605,6 @@ parameters:
count: 1
path: test/classes/Config/ServerConfigChecksTest.php

-
message: "#^Variable property access on PhpMyAdmin\\\\Config\\\\Settings\\\\Console\\.$#"
count: 1
path: test/classes/Config/Settings/ConsoleTest.php

-
message: "#^Variable property access on PhpMyAdmin\\\\Config\\\\Settings\\\\Debug\\.$#"
count: 1
Expand Down
14 changes: 0 additions & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -861,20 +861,6 @@
<code>$maxRowPlotLimit</code>
</PossiblyUnusedProperty>
</file>
<file src="libraries/classes/Config/Settings/Console.php">
<PossiblyUnusedProperty>
<code>$AlwaysExpand</code>
<code>$CurrentQuery</code>
<code>$DarkTheme</code>
<code>$EnterExecutes</code>
<code>$GroupQueries</code>
<code>$Height</code>
<code>$Mode</code>
<code>$Order</code>
<code>$OrderBy</code>
<code>$StartHistory</code>
</PossiblyUnusedProperty>
</file>
<file src="libraries/classes/Config/Settings/Debug.php">
<PossiblyUnusedProperty>
<code>$demo</code>
Expand Down
281 changes: 164 additions & 117 deletions test/classes/Config/Settings/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,125 +7,172 @@
use PhpMyAdmin\Config\Settings\Console;
use PHPUnit\Framework\TestCase;

use function array_keys;
use function array_merge;

/** @covers \PhpMyAdmin\Config\Settings\Console */
class ConsoleTest extends TestCase
{
/** @var array<string, bool|int|string> */
private array $defaultValues = [
'StartHistory' => false,
'AlwaysExpand' => false,
'CurrentQuery' => true,
'EnterExecutes' => false,
'DarkTheme' => false,
'Mode' => 'info',
'Height' => 92,
'GroupQueries' => false,
'OrderBy' => 'exec',
'Order' => 'asc',
];

/**
* @param mixed[][] $values
* @psalm-param (array{0: string, 1: mixed, 2: mixed})[] $values
*
* @dataProvider providerForTestConstructor
*/
public function testConstructor(array $values): void
{
$actualValues = [];
$expectedValues = [];
/** @psalm-suppress MixedAssignment */
foreach ($values as $value) {
$actualValues[$value[0]] = $value[1];
$expectedValues[$value[0]] = $value[2];
}

$expected = array_merge($this->defaultValues, $expectedValues);
$settings = new Console($actualValues);

foreach (array_keys($expectedValues) as $key) {
$this->assertSame($expected[$key], $settings->$key);
}
}

/**
* [setting key, actual value, expected value]
*
* @return mixed[][][][]
* @psalm-return (array{0: string, 1: mixed, 2: mixed})[][][]
*/
public static function providerForTestConstructor(): array
{
return [
'null values' => [
[
['StartHistory', null, false],
['AlwaysExpand', null, false],
['CurrentQuery', null, true],
['EnterExecutes', null, false],
['DarkTheme', null, false],
['Mode', null, 'info'],
['Height', null, 92],
['GroupQueries', null, false],
['OrderBy', null, 'exec'],
['Order', null, 'asc'],
],
],
'valid values' => [
[
['StartHistory', false, false],
['AlwaysExpand', false, false],
['CurrentQuery', true, true],
['EnterExecutes', false, false],
['DarkTheme', false, false],
['Mode', 'info', 'info'],
['Height', 1, 1],
['GroupQueries', false, false],
['OrderBy', 'exec', 'exec'],
['Order', 'asc', 'asc'],
],
],
'valid values 2' => [
[
['StartHistory', true, true],
['AlwaysExpand', true, true],
['CurrentQuery', false, false],
['EnterExecutes', true, true],
['DarkTheme', true, true],
['Mode', 'show', 'show'],
['GroupQueries', true, true],
['OrderBy', 'time', 'time'],
['Order', 'desc', 'desc'],
],
],
'valid values 3' => [
[
['Mode', 'collapse', 'collapse'],
['OrderBy', 'count', 'count'],
],
],
'valid values with type coercion' => [
[
['StartHistory', 1, true],
['AlwaysExpand', 1, true],
['CurrentQuery', 0, false],
['EnterExecutes', 1, true],
['DarkTheme', 1, true],
['Height', '2', 2],
['GroupQueries', 1, true],
],
],
'invalid values' => [
[
['Mode', 'invalid', 'info'],
['Height', 0, 92],
['OrderBy', 'invalid', 'exec'],
['Order', 'invalid', 'asc'],
],
],
];
/** @dataProvider booleanWithDefaultFalseProvider */
public function testStartHistory(mixed $actual, bool $expected): void
{
$console = new Console(['StartHistory' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->StartHistory);
$this->assertArrayHasKey('StartHistory', $consoleArray);
$this->assertSame($expected, $consoleArray['StartHistory']);
}

/** @return iterable<string, array{mixed, bool}> */
public static function booleanWithDefaultFalseProvider(): iterable
{
yield 'null value' => [null, false];
yield 'valid value' => [false, false];
yield 'valid value 2' => [true, true];
yield 'valid value with type coercion' => [1, true];
}

/** @dataProvider booleanWithDefaultFalseProvider */
public function testAlwaysExpand(mixed $actual, bool $expected): void
{
$console = new Console(['AlwaysExpand' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->AlwaysExpand);
$this->assertArrayHasKey('AlwaysExpand', $consoleArray);
$this->assertSame($expected, $consoleArray['AlwaysExpand']);
}

/** @dataProvider booleanWithDefaultTrueProvider */
public function testCurrentQuery(mixed $actual, bool $expected): void
{
$console = new Console(['CurrentQuery' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->CurrentQuery);
$this->assertArrayHasKey('CurrentQuery', $consoleArray);
$this->assertSame($expected, $consoleArray['CurrentQuery']);
}

/** @return iterable<string, array{mixed, bool}> */
public static function booleanWithDefaultTrueProvider(): iterable
{
yield 'null value' => [null, true];
yield 'valid value' => [true, true];
yield 'valid value 2' => [false, false];
yield 'valid value with type coercion' => [0, false];
}

/** @dataProvider booleanWithDefaultFalseProvider */
public function testEnterExecutes(mixed $actual, bool $expected): void
{
$console = new Console(['EnterExecutes' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->EnterExecutes);
$this->assertArrayHasKey('EnterExecutes', $consoleArray);
$this->assertSame($expected, $consoleArray['EnterExecutes']);
}

/** @dataProvider booleanWithDefaultFalseProvider */
public function testDarkTheme(mixed $actual, bool $expected): void
{
$console = new Console(['DarkTheme' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->DarkTheme);
$this->assertArrayHasKey('DarkTheme', $consoleArray);
$this->assertSame($expected, $consoleArray['DarkTheme']);
}

/** @dataProvider valuesForModeProvider */
public function testMode(mixed $actual, string $expected): void
{
$console = new Console(['Mode' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->Mode);
$this->assertArrayHasKey('Mode', $consoleArray);
$this->assertSame($expected, $consoleArray['Mode']);
}

/** @return iterable<string, array{mixed, string}> */
public static function valuesForModeProvider(): iterable
{
yield 'null value' => [null, 'info'];
yield 'valid value' => ['info', 'info'];
yield 'valid value 2' => ['show', 'show'];
yield 'valid value 3' => ['collapse', 'collapse'];
yield 'invalid value' => ['invalid', 'info'];
}

/** @dataProvider valuesForHeightProvider */
public function testHeight(mixed $actual, int $expected): void
{
$console = new Console(['Height' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->Height);
$this->assertArrayHasKey('Height', $consoleArray);
$this->assertSame($expected, $consoleArray['Height']);
}

/** @return iterable<string, array{mixed, int}> */
public static function valuesForHeightProvider(): iterable
{
yield 'null value' => [null, 92];
yield 'valid value' => [1, 1];
yield 'valid value with type coercion' => ['2', 2];
yield 'invalid value' => [0, 92];
}

/** @dataProvider booleanWithDefaultFalseProvider */
public function testGroupQueries(mixed $actual, bool $expected): void
{
$console = new Console(['GroupQueries' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->GroupQueries);
$this->assertArrayHasKey('GroupQueries', $consoleArray);
$this->assertSame($expected, $consoleArray['GroupQueries']);
}

/** @dataProvider valuesForOrderByProvider */
public function testOrderBy(mixed $actual, string $expected): void
{
$console = new Console(['OrderBy' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->OrderBy);
$this->assertArrayHasKey('OrderBy', $consoleArray);
$this->assertSame($expected, $consoleArray['OrderBy']);
}

/** @return iterable<string, array{mixed, string}> */
public static function valuesForOrderByProvider(): iterable
{
yield 'null value' => [null, 'exec'];
yield 'valid value' => ['exec', 'exec'];
yield 'valid value 2' => ['time', 'time'];
yield 'valid value 3' => ['count', 'count'];
yield 'invalid value' => ['invalid', 'exec'];
}

/** @dataProvider valuesForOrderProvider */
public function testOrder(mixed $actual, string $expected): void
{
$console = new Console(['Order' => $actual]);
$consoleArray = $console->asArray();
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$this->assertSame($expected, $console->Order);
$this->assertArrayHasKey('Order', $consoleArray);
$this->assertSame($expected, $consoleArray['Order']);
}

/** @return iterable<string, array{mixed, string}> */
public static function valuesForOrderProvider(): iterable
{
yield 'null value' => [null, 'asc'];
yield 'valid value' => ['asc', 'asc'];
yield 'valid value 2' => ['desc', 'desc'];
yield 'invalid value' => ['invalid', 'asc'];
}
}

0 comments on commit 7f5a1b7

Please sign in to comment.