diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-09 23:58:00 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-09 23:58:00 +0300 |
commit | 94e714a49ba306461d2935906e6fc1a7b8f4addb (patch) | |
tree | 46b8eec7e9fb543a43016829ec5c184089c3d210 | |
parent | dd0f63f18cd47ea359ad33b426b9d0faf5cbc2ce (diff) |
Extract the `Server\ShowEngineController` class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | libraries/classes/Controllers/Server/EnginesController.php | 41 | ||||
-rw-r--r-- | libraries/classes/Controllers/Server/ShowEngineController.php | 69 | ||||
-rw-r--r-- | libraries/routes.php | 4 | ||||
-rw-r--r-- | libraries/services_controllers.php | 8 | ||||
-rw-r--r-- | psalm-baseline.xml | 10 | ||||
-rw-r--r-- | test/classes/Controllers/Server/EnginesControllerTest.php | 62 | ||||
-rw-r--r-- | test/classes/Controllers/Server/ShowEngineControllerTest.php | 85 |
7 files changed, 166 insertions, 113 deletions
diff --git a/libraries/classes/Controllers/Server/EnginesController.php b/libraries/classes/Controllers/Server/EnginesController.php index e911c722e5..a18998a506 100644 --- a/libraries/classes/Controllers/Server/EnginesController.php +++ b/libraries/classes/Controllers/Server/EnginesController.php @@ -6,7 +6,6 @@ namespace PhpMyAdmin\Controllers\Server; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\DatabaseInterface; -use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\StorageEngine; use PhpMyAdmin\Template; @@ -30,7 +29,7 @@ class EnginesController extends AbstractController $this->dbi = $dbi; } - public function index(): void + public function __invoke(): void { global $errorUrl; @@ -44,42 +43,4 @@ class EnginesController extends AbstractController 'engines' => StorageEngine::getStorageEngines(), ]); } - - /** - * Displays details about a given Storage Engine - * - * @param array $params Request params - */ - public function show(ServerRequest $request, array $params): void - { - global $errorUrl; - - $errorUrl = Url::getFromRoute('/'); - - if ($this->dbi->isSuperUser()) { - $this->dbi->selectDb('mysql'); - } - - $page = $params['page'] ?? ''; - - $engine = []; - if (StorageEngine::isValid($params['engine'])) { - $storageEngine = StorageEngine::getEngine($params['engine']); - $engine = [ - 'engine' => $params['engine'], - 'title' => $storageEngine->getTitle(), - 'help_page' => $storageEngine->getMysqlHelpPage(), - 'comment' => $storageEngine->getComment(), - 'info_pages' => $storageEngine->getInfoPages(), - 'support' => $storageEngine->getSupportInformationMessage(), - 'variables' => $storageEngine->getHtmlVariables(), - 'page' => ! empty($page) ? $storageEngine->getPage($page) : '', - ]; - } - - $this->render('server/engines/show', [ - 'engine' => $engine, - 'page' => $page, - ]); - } } diff --git a/libraries/classes/Controllers/Server/ShowEngineController.php b/libraries/classes/Controllers/Server/ShowEngineController.php new file mode 100644 index 0000000000..3057f1615d --- /dev/null +++ b/libraries/classes/Controllers/Server/ShowEngineController.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Controllers\Server; + +use PhpMyAdmin\Controllers\AbstractController; +use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\ResponseRenderer; +use PhpMyAdmin\StorageEngine; +use PhpMyAdmin\Template; +use PhpMyAdmin\Url; + +/** + * Displays details about a given Storage Engine. + */ +final class ShowEngineController extends AbstractController +{ + /** @var DatabaseInterface */ + private $dbi; + + /** + * @param ResponseRenderer $response + * @param DatabaseInterface $dbi + */ + public function __construct($response, Template $template, $dbi) + { + parent::__construct($response, $template); + $this->dbi = $dbi; + } + + /** + * @param array $params + * @psalm-param array{engine: string, page?: string} $params + */ + public function __invoke(ServerRequest $request, array $params): void + { + global $errorUrl; + + $errorUrl = Url::getFromRoute('/'); + + if ($this->dbi->isSuperUser()) { + $this->dbi->selectDb('mysql'); + } + + $page = $params['page'] ?? ''; + + $engine = []; + if (StorageEngine::isValid($params['engine'])) { + $storageEngine = StorageEngine::getEngine($params['engine']); + $engine = [ + 'engine' => $params['engine'], + 'title' => $storageEngine->getTitle(), + 'help_page' => $storageEngine->getMysqlHelpPage(), + 'comment' => $storageEngine->getComment(), + 'info_pages' => $storageEngine->getInfoPages(), + 'support' => $storageEngine->getSupportInformationMessage(), + 'variables' => $storageEngine->getHtmlVariables(), + 'page' => ! empty($page) ? $storageEngine->getPage($page) : '', + ]; + } + + $this->render('server/engines/show', [ + 'engine' => $engine, + 'page' => $page, + ]); + } +} diff --git a/libraries/routes.php b/libraries/routes.php index 191d19c7f8..895b3bf86e 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -148,8 +148,8 @@ return static function (RouteCollector $routes): void { $routes->post('/destroy', Server\Databases\DestroyController::class); }); $routes->addGroup('/engines', static function (RouteCollector $routes): void { - $routes->get('', [Server\EnginesController::class, 'index']); - $routes->get('/{engine}[/{page}]', [Server\EnginesController::class, 'show']); + $routes->get('', Server\EnginesController::class); + $routes->get('/{engine}[/{page}]', Server\ShowEngineController::class); }); $routes->addRoute(['GET', 'POST'], '/export', Server\ExportController::class); $routes->addRoute(['GET', 'POST'], '/import', Server\ImportController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index f27dbbb705..d58196e5ea 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -833,6 +833,14 @@ return [ '$dbi' => '@dbi', ], ], + Server\ShowEngineController::class => [ + 'class' => Server\ShowEngineController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$dbi' => '@dbi', + ], + ], Server\SqlController::class => [ 'class' => Server\SqlController::class, 'arguments' => [ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index cb59c0d914..fb41c248d1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2639,16 +2639,6 @@ <code>0</code> </TypeDoesNotContainNull> </file> - <file src="libraries/classes/Controllers/Server/EnginesController.php"> - <MixedArgument occurrences="3"> - <code>$page</code> - <code>$params['engine']</code> - <code>$params['engine']</code> - </MixedArgument> - <MixedAssignment occurrences="1"> - <code>$page</code> - </MixedAssignment> - </file> <file src="libraries/classes/Controllers/Server/ExportController.php"> <MixedArgument occurrences="6"> <code>$db</code> diff --git a/test/classes/Controllers/Server/EnginesControllerTest.php b/test/classes/Controllers/Server/EnginesControllerTest.php index dd92c9929e..e246a958ee 100644 --- a/test/classes/Controllers/Server/EnginesControllerTest.php +++ b/test/classes/Controllers/Server/EnginesControllerTest.php @@ -5,16 +5,10 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests\Controllers\Server; use PhpMyAdmin\Controllers\Server\EnginesController; -use PhpMyAdmin\Html\MySQLDocumentation; -use PhpMyAdmin\Http\ServerRequest; -use PhpMyAdmin\StorageEngine; use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; -use function __; -use function htmlspecialchars; - /** * @covers \PhpMyAdmin\Controllers\Server\EnginesController */ @@ -46,7 +40,7 @@ class EnginesControllerTest extends AbstractTestCase $controller = new EnginesController($response, new Template(), $dbi); $this->dummyDbi->addSelectDb('mysql'); - $controller->index(); + $controller->__invoke(); $this->assertAllSelectsConsumed(); $actual = $response->getHTMLResult(); @@ -86,58 +80,4 @@ class EnginesControllerTest extends AbstractTestCase $actual ); } - - public function testShow(): void - { - global $dbi; - - $response = new ResponseRenderer(); - - $controller = new EnginesController($response, new Template(), $dbi); - - $request = $this->createMock(ServerRequest::class); - - $this->dummyDbi->addSelectDb('mysql'); - $controller->show($request, [ - 'engine' => 'Pbxt', - 'page' => 'page', - ]); - $this->assertAllSelectsConsumed(); - $actual = $response->getHTMLResult(); - - $enginePlugin = StorageEngine::getEngine('Pbxt'); - - $this->assertStringContainsString( - htmlspecialchars($enginePlugin->getTitle()), - $actual - ); - - $this->assertStringContainsString( - MySQLDocumentation::show($enginePlugin->getMysqlHelpPage()), - $actual - ); - - $this->assertStringContainsString( - htmlspecialchars($enginePlugin->getComment()), - $actual - ); - - $this->assertStringContainsString( - __('Variables'), - $actual - ); - $this->assertStringContainsString( - 'index.php?route=/server/engines/Pbxt/Documentation', - $actual - ); - $this->assertStringContainsString( - $enginePlugin->getSupportInformationMessage(), - $actual - ); - $this->assertStringContainsString( - 'There is no detailed status information available for this ' - . 'storage engine.', - $actual - ); - } } diff --git a/test/classes/Controllers/Server/ShowEngineControllerTest.php b/test/classes/Controllers/Server/ShowEngineControllerTest.php new file mode 100644 index 0000000000..6b67bd2ed2 --- /dev/null +++ b/test/classes/Controllers/Server/ShowEngineControllerTest.php @@ -0,0 +1,85 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Server; + +use PhpMyAdmin\Controllers\Server\ShowEngineController; +use PhpMyAdmin\Html\MySQLDocumentation; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\StorageEngine; +use PhpMyAdmin\Template; +use PhpMyAdmin\Tests\AbstractTestCase; +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; + +use function __; +use function htmlspecialchars; + +/** + * @covers \PhpMyAdmin\Controllers\Server\ShowEngineController + */ +class ShowEngineControllerTest extends AbstractTestCase +{ + public function testShowEngine(): void + { + parent::setUp(); + $GLOBALS['text_dir'] = 'ltr'; + parent::setGlobalConfig(); + parent::setTheme(); + + global $dbi; + + $GLOBALS['server'] = 1; + $GLOBALS['db'] = 'db'; + $GLOBALS['table'] = 'table'; + $GLOBALS['PMA_PHP_SELF'] = 'index.php'; + $GLOBALS['cfg']['Server']['DisableIS'] = false; + + $response = new ResponseRenderer(); + $request = $this->createMock(ServerRequest::class); + $this->dummyDbi->addSelectDb('mysql'); + + (new ShowEngineController($response, new Template(), $dbi))($request, [ + 'engine' => 'Pbxt', + 'page' => 'page', + ]); + + $this->assertAllSelectsConsumed(); + $actual = $response->getHTMLResult(); + + $enginePlugin = StorageEngine::getEngine('Pbxt'); + + $this->assertStringContainsString( + htmlspecialchars($enginePlugin->getTitle()), + $actual + ); + + $this->assertStringContainsString( + MySQLDocumentation::show($enginePlugin->getMysqlHelpPage()), + $actual + ); + + $this->assertStringContainsString( + htmlspecialchars($enginePlugin->getComment()), + $actual + ); + + $this->assertStringContainsString( + __('Variables'), + $actual + ); + $this->assertStringContainsString( + 'index.php?route=/server/engines/Pbxt/Documentation', + $actual + ); + $this->assertStringContainsString( + $enginePlugin->getSupportInformationMessage(), + $actual + ); + $this->assertStringContainsString( + 'There is no detailed status information available for this ' + . 'storage engine.', + $actual + ); + } +} |