Skip to content

Commit

Permalink
Merge callProtectedMethod and callPrivateFunction into callFunction
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 21, 2020
1 parent 5c270d8 commit 7e81b0b
Show file tree
Hide file tree
Showing 19 changed files with 1,306 additions and 1,025 deletions.
13 changes: 6 additions & 7 deletions test/classes/AbstractTestCase.php
Expand Up @@ -183,21 +183,20 @@ protected function tearDown(): void
}
}


/**
* Call protected functions by setting visibility to public.
*
* @param mixed $object The object to inspect
* @param string $className The class name
* @param string $name method name
* @param array $params parameters for the invocation
* @param object|null $object The object to inspect, pass null for static objects()
* @param class-string $className The class name
* @param string $methodName The method name
* @param array $params The parameters for the invocation
*
* @return mixed the output from the protected method.
*/
protected function callProtectedFunction($object, string $className, string $name, array $params)
protected function callFunction(?object $object, string $className, string $methodName, array $params)
{
$class = new ReflectionClass($className);
$method = $class->getMethod($name);
$method = $class->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $params);
Expand Down
45 changes: 16 additions & 29 deletions test/classes/BrowseForeignersTest.php
Expand Up @@ -9,7 +9,6 @@
use PhpMyAdmin\BrowseForeigners;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use ReflectionClass;

/**
* Tests for PhpMyAdmin\BrowseForeigners
Expand All @@ -32,27 +31,6 @@ protected function setUp(): void
$this->browseForeigners = new BrowseForeigners(new Template());
}

/**
* Call protected functions by setting visibility to public.
*
* @param string $name method name
* @param array $params parameters for the invocation
* @param BrowseForeigners $object BrowseForeigners instance object
*
* @return mixed the output from the protected method.
*/
private function callProtectedMethod($name, $params, BrowseForeigners $object = null)
{
$class = new ReflectionClass(BrowseForeigners::class);
$method = $class->getMethod($name);
$method->setAccessible(true);

return $method->invokeArgs(
$object ?? $this->browseForeigners,
$params
);
}

/**
* Test for BrowseForeigners::getForeignLimit
*
Expand Down Expand Up @@ -99,7 +77,9 @@ public function testGetHtmlForGotoPage()
{
$this->assertEquals(
'',
$this->callProtectedMethod(
$this->callFunction(
$this->browseForeigners,
BrowseForeigners::class,
'getHtmlForGotoPage',
[null]
)
Expand All @@ -112,14 +92,18 @@ public function testGetHtmlForGotoPage()

$this->assertEquals(
'',
$this->callProtectedMethod(
$this->callFunction(
$this->browseForeigners,
BrowseForeigners::class,
'getHtmlForGotoPage',
[$foreignData]
)
);

$foreignData['the_total'] = 30;
$result = $this->callProtectedMethod(
$result = $this->callFunction(
$this->browseForeigners,
BrowseForeigners::class,
'getHtmlForGotoPage',
[$foreignData]
);
Expand Down Expand Up @@ -165,7 +149,9 @@ public function testGetDescriptionAndTitle()
'foobar&lt;baz',
'',
],
$this->callProtectedMethod(
$this->callFunction(
$this->browseForeigners,
BrowseForeigners::class,
'getDescriptionAndTitle',
[$desc]
)
Expand All @@ -179,10 +165,11 @@ public function testGetDescriptionAndTitle()
'fooba...',
'foobar&lt;baz',
],
$this->callProtectedMethod(
$this->callFunction(
$browseForeigners,
BrowseForeigners::class,
'getDescriptionAndTitle',
[$desc],
$browseForeigners
[$desc]
)
);
}
Expand Down
45 changes: 14 additions & 31 deletions test/classes/CentralColumnsTest.php
Expand Up @@ -15,7 +15,6 @@
use PhpMyAdmin\Util;
use PhpMyAdmin\Message;
use PhpMyAdmin\Tests\AbstractTestCase;
use ReflectionClass;
use function array_slice;
use function ceil;

Expand Down Expand Up @@ -166,30 +165,6 @@ protected function setUp(): void
$this->centralColumns = new CentralColumns($dbi);
}

/**
* Call protected functions by setting visibility to public.
*
* @param string $name method name
* @param array $params parameters for the invocation
* @param CentralColumns $object CentralColumns instance object
*
* @return mixed the output from the protected method.
*/
private function callProtectedMethod(
$name,
array $params = [],
CentralColumns $object = null
) {
$class = new ReflectionClass(CentralColumns::class);
$method = $class->getMethod($name);
$method->setAccessible(true);

return $method->invokeArgs(
$object ?? $this->centralColumns,
$params
);
}

/**
* Test for getParams
*
Expand Down Expand Up @@ -551,13 +526,17 @@ public function testGetHtmlForEditingPage()
__('A_I'),
];
$this->assertStringContainsString(
$this->callProtectedMethod(
$this->callFunction(
$this->centralColumns,
CentralColumns::class,
'getEditTableHeader',
[$header_cells]
),
$result
);
$list_detail_cols = $this->callProtectedMethod(
$list_detail_cols = $this->callFunction(
$this->centralColumns,
CentralColumns::class,
'findExistingColNames',
[
'phpmyadmin',
Expand All @@ -566,7 +545,9 @@ public function testGetHtmlForEditingPage()
]
);
$this->assertStringContainsString(
$this->callProtectedMethod(
$this->callFunction(
$this->centralColumns,
CentralColumns::class,
'getHtmlForEditTableRow',
[
$list_detail_cols[0],
Expand All @@ -576,7 +557,7 @@ public function testGetHtmlForEditingPage()
$result
);
$this->assertStringContainsString(
$this->callProtectedMethod('getEditTableFooter'),
$this->callFunction($this->centralColumns, CentralColumns::class, 'getEditTableFooter', []),
$result
);
}
Expand Down Expand Up @@ -734,7 +715,7 @@ public function testConfigErrorMessage()
{
$this->assertInstanceOf(
Message::class,
$this->callProtectedMethod('configErrorMessage')
$this->callFunction($this->centralColumns, CentralColumns::class, 'configErrorMessage', [])
);
}

Expand All @@ -759,7 +740,9 @@ public function testFindExistingColNames()
);
$this->assertEquals(
array_slice($this->modifiedColumnData, 1, 1),
$this->callProtectedMethod(
$this->callFunction(
$this->centralColumns,
CentralColumns::class,
'findExistingColNames',
[
'phpmyadmin',
Expand Down
27 changes: 18 additions & 9 deletions test/classes/Controllers/Server/VariablesControllerTest.php
Expand Up @@ -6,15 +6,13 @@

namespace PhpMyAdmin\Tests\Controllers\Server;

use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\Server\VariablesController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\Stubs\Response as ResponseStub;
use PhpMyAdmin\Tests\AbstractTestCase;
use ReflectionClass;
use Williamdes\MariaDBMySQLKBS\Search as KBSearch;
use Williamdes\MariaDBMySQLKBS\SlimData as KBSlimData;
use function htmlspecialchars;
Expand Down Expand Up @@ -140,10 +138,6 @@ public function testIndex(): void
*/
public function testFormatVariable(): void
{
$class = new ReflectionClass(VariablesController::class);
$method = $class->getMethod('formatVariable');
$method->setAccessible(true);

$controller = new VariablesController(
Response::getInstance(),
$GLOBALS['dbi'],
Expand All @@ -163,7 +157,12 @@ public function testFormatVariable(): void
$nameForValueByte,
'3',
];
[$formattedValue, $isHtmlFormatted] = $method->invokeArgs($controller, $args);
[$formattedValue, $isHtmlFormatted] = $this->callFunction(
$controller,
VariablesController::class,
'formatVariable',
$args
);
$this->assertEquals(
'<abbr title="3">3 B</abbr>',
$formattedValue
Expand All @@ -175,7 +174,12 @@ public function testFormatVariable(): void
$nameForValueNotByte,
'3',
];
[$formattedValue, $isHtmlFormatted] = $method->invokeArgs($controller, $args);
[$formattedValue, $isHtmlFormatted] = $this->callFunction(
$controller,
VariablesController::class,
'formatVariable',
$args
);
$this->assertEquals(
'3',
$formattedValue
Expand All @@ -187,7 +191,12 @@ public function testFormatVariable(): void
$nameForValueNotByte,
'value',
];
[$formattedValue, $isHtmlFormatted] = $method->invokeArgs($controller, $args);
[$formattedValue, $isHtmlFormatted] = $this->callFunction(
$controller,
VariablesController::class,
'formatVariable',
$args
);
$this->assertEquals(
'value',
$formattedValue
Expand Down

0 comments on commit 7e81b0b

Please sign in to comment.