diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-07-30 06:36:58 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-07-30 06:36:58 +0300 |
commit | dd25695569bc86f66e6be393bf92394229639549 (patch) | |
tree | 33c01835e897b2caace218b8bcd339e76acd2acc | |
parent | 51db9db3ddc06e0bfc2909abb7585325885d2e7d (diff) |
Extract normalization 2NF new tables action
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | js/src/normalization.js | 4 | ||||
-rw-r--r-- | libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php | 32 | ||||
-rw-r--r-- | libraries/classes/Controllers/NormalizationController.php | 8 | ||||
-rw-r--r-- | libraries/routes.php | 1 | ||||
-rw-r--r-- | libraries/services_controllers.php | 8 | ||||
-rw-r--r-- | phpstan-baseline.neon | 10 | ||||
-rw-r--r-- | psalm-baseline.xml | 18 | ||||
-rw-r--r-- | test/classes/Controllers/Normalization/SecondNormalForm/NewTablesControllerTest.php | 48 | ||||
-rw-r--r-- | test/classes/Controllers/NormalizationControllerTest.php | 22 |
9 files changed, 108 insertions, 43 deletions
diff --git a/js/src/normalization.js b/js/src/normalization.js index 005fcd33c5..4e734e240e 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -318,10 +318,10 @@ function goTo2NFStep2 (pd, primaryKey) { 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), 'pd': JSON.stringify(pd), - 'getNewTables2NF':1 }; + }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/2nf/new-tables', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php b/libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php new file mode 100644 index 0000000000..07232fa380 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Controllers\Normalization\SecondNormalForm; + +use PhpMyAdmin\Controllers\AbstractController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Normalization; +use PhpMyAdmin\ResponseRenderer; +use PhpMyAdmin\Template; + +use function json_decode; + +final class NewTablesController extends AbstractController +{ + /** @var Normalization */ + private $normalization; + + public function __construct(ResponseRenderer $response, Template $template, Normalization $normalization) + { + parent::__construct($response, $template); + $this->normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $partialDependencies = json_decode($_POST['pd'], true); + $html = $this->normalization->getHtmlForNewTables2NF($partialDependencies, $GLOBALS['table']); + $this->response->addHTML($html); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index 7886423ce7..02451e26fa 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -82,14 +82,6 @@ class NormalizationController extends AbstractController return; } - if (isset($_POST['getNewTables2NF'])) { - $partialDependencies = json_decode($_POST['pd'], true); - $html = $this->normalization->getHtmlForNewTables2NF($partialDependencies, $GLOBALS['table']); - echo $html; - - return; - } - if (isset($_POST['getNewTables3NF'])) { $dependencies = json_decode($_POST['pd']); $tables = json_decode($_POST['tables'], true); diff --git a/libraries/routes.php b/libraries/routes.php index dad41d8b24..d150421a99 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -136,6 +136,7 @@ return static function (RouteCollector $routes): void { $routes->post('/1nf/step2', Normalization\FirstNormalForm\SecondStepController::class); $routes->post('/1nf/step3', Normalization\FirstNormalForm\ThirdStepController::class); $routes->post('/1nf/step4', Normalization\FirstNormalForm\FourthStepController::class); + $routes->post('/2nf/new-tables', Normalization\SecondNormalForm\NewTablesController::class); $routes->post('/2nf/step1', Normalization\SecondNormalForm\FirstStepController::class); }); $routes->get('/phpinfo', PhpInfoController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 9009548946..dc7fa99985 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -625,6 +625,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\SecondNormalForm\NewTablesController::class => [ + 'class' => Normalization\SecondNormalForm\NewTablesController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], NormalizationController::class => [ 'class' => NormalizationController::class, 'arguments' => [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index aa0ed2f75c..b223fbc395 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1221,22 +1221,22 @@ parameters: path: libraries/classes/Controllers/HomeController.php - - message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" + message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables2NF\\(\\) expects array, mixed given\\.$#" count: 1 - path: libraries/classes/Controllers/NormalizationController.php + path: libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php - - message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" + message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" count: 1 path: libraries/classes/Controllers/NormalizationController.php - - message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects array, mixed given\\.$#" + message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" count: 1 path: libraries/classes/Controllers/NormalizationController.php - - message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables2NF\\(\\) expects array, mixed given\\.$#" + message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects array, mixed given\\.$#" count: 1 path: libraries/classes/Controllers/NormalizationController.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 76ea2a44ae..91aa5a2050 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<files psalm-version="4.24.0@06dd975cb55d36af80f242561738f16c5f58264f"> +<files psalm-version="v4.25.0@d7cd84c4ebca74ba3419b9601f81d177bcbe2aac"> <file src="libraries/classes/Advisory/Advisor.php"> <MixedArgument occurrences="8"> <code>$matches[1]</code> @@ -2407,33 +2407,39 @@ <code>$normalForm</code> </MixedAssignment> </file> + <file src="libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php"> + <MixedArgument occurrences="2"> + <code>$_POST['pd']</code> + <code>$partialDependencies</code> + </MixedArgument> + <MixedAssignment occurrences="1"> + <code>$partialDependencies</code> + </MixedAssignment> + </file> <file src="libraries/classes/Controllers/NormalizationController.php"> - <MixedArgument occurrences="17"> + <MixedArgument occurrences="15"> <code>$_POST['newTables']</code> <code>$_POST['newTablesName']</code> <code>$_POST['pd']</code> <code>$_POST['pd']</code> - <code>$_POST['pd']</code> <code>$_POST['tables']</code> <code>$dependencies</code> <code>$newColumn</code> <code>$newTable</code> <code>$newtables</code> <code>$partialDependencies</code> - <code>$partialDependencies</code> <code>$primary_columns</code> <code>$repeatingColumns</code> <code>$tables</code> <code>$tables</code> <code>$tablesName</code> </MixedArgument> - <MixedAssignment occurrences="11"> + <MixedAssignment occurrences="10"> <code>$dependencies</code> <code>$newColumn</code> <code>$newTable</code> <code>$newtables</code> <code>$partialDependencies</code> - <code>$partialDependencies</code> <code>$primary_columns</code> <code>$repeatingColumns</code> <code>$tables</code> diff --git a/test/classes/Controllers/Normalization/SecondNormalForm/NewTablesControllerTest.php b/test/classes/Controllers/Normalization/SecondNormalForm/NewTablesControllerTest.php new file mode 100644 index 0000000000..e019257735 --- /dev/null +++ b/test/classes/Controllers/Normalization/SecondNormalForm/NewTablesControllerTest.php @@ -0,0 +1,48 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Normalization\SecondNormalForm; + +use PhpMyAdmin\ConfigStorage\Relation; +use PhpMyAdmin\Controllers\Normalization\SecondNormalForm\NewTablesController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Normalization; +use PhpMyAdmin\Template; +use PhpMyAdmin\Tests\AbstractTestCase; +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; +use PhpMyAdmin\Transformations; + +use function json_encode; + +/** + * @covers \PhpMyAdmin\Controllers\Normalization\SecondNormalForm\NewTablesController + */ +class NewTablesControllerTest extends AbstractTestCase +{ + public function testDefault(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $_POST['pd'] = json_encode(['ID, task' => [], 'task' => ['timestamp']]); + + $dbi = $this->createDatabaseInterface(); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new NewTablesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + // phpcs:disable Generic.Files.LineLength.TooLong + $this->assertSame( + '<p><b>In order to put the original table \'test_table\' into Second normal form we need to create the following tables:</b></p><p><input type="text" name="ID, task" value="test_table">( <u>ID, task</u> )<p><input type="text" name="task" value="table2">( <u>task</u>, timestamp )', + $response->getHTMLResult() + ); + // phpcs:enable + } +} diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/NormalizationControllerTest.php index 31a87b0676..6e8ca6e76d 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/NormalizationControllerTest.php @@ -117,28 +117,6 @@ class NormalizationControllerTest extends AbstractTestCase $this->expectOutputString($data); } - public function testGetNewTables2NF(): void - { - $_POST['getNewTables2NF'] = 1; - $_POST['pd'] = json_encode([ - 'ID, task' => [], - 'task' => ['timestamp'], - ]); - - $GLOBALS['goto'] = 'index.php?route=/sql'; - $GLOBALS['containerBuilder']->setParameter('db', $GLOBALS['db']); - $GLOBALS['containerBuilder']->setParameter('table', $GLOBALS['table']); - /** @var NormalizationController $normalizationController */ - $normalizationController = $GLOBALS['containerBuilder']->get(NormalizationController::class); - $normalizationController($this->createStub(ServerRequest::class)); - $this->expectOutputString( - '<p><b>In order to put the original table \'test_tbl\' into Second normal' - . ' form we need to create the following tables:</b></p><p><input type="text" ' - . 'name="ID, task" value="test_tbl">( <u>ID, task</u> )<p><input type="text" name="task"' - . ' value="table2">( <u>task</u>, timestamp )' - ); - } - public function testCreateNewTables2NF(): void { $_POST['createNewTables2NF'] = 1; |