diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-08-02 20:43:13 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-08-02 20:43:13 +0300 |
commit | 97c5c01a8764eafa2b4016bf3fee63090b084e97 (patch) | |
tree | b3970a26e6efcfa460e09417480eb7588a7f6728 | |
parent | c9bccf3b3d4ea402e0accd5ae2625fe2b107f81a (diff) |
Extract normalization 3NF create new tables action
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | js/src/normalization.js | 6 | ||||
-rw-r--r-- | libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php | 32 | ||||
-rw-r--r-- | libraries/classes/Controllers/NormalizationController.php | 9 | ||||
-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 | 16 | ||||
-rw-r--r-- | test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php | 64 | ||||
-rw-r--r-- | test/classes/Controllers/NormalizationControllerTest.php | 39 |
9 files changed, 124 insertions, 61 deletions
diff --git a/js/src/normalization.js b/js/src/normalization.js index 27d7639f5a..c5334e2d08 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -265,11 +265,11 @@ function goTo3NFFinish (newTables) { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'server': window.CommonParams.get('server'), - 'newTables':JSON.stringify(newTables), - 'createNewTables3NF':1 }; + 'newTables': JSON.stringify(newTables), + }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/3nf/create-new-tables', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php b/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php new file mode 100644 index 0000000000..bd3e0b2c4a --- /dev/null +++ b/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Controllers\Normalization\ThirdNormalForm; + +use PhpMyAdmin\Controllers\AbstractController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Normalization; +use PhpMyAdmin\ResponseRenderer; +use PhpMyAdmin\Template; + +use function json_decode; + +final class CreateNewTablesController 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 + { + $newtables = json_decode($_POST['newTables'], true); + $res = $this->normalization->createNewTablesFor3NF($newtables, $GLOBALS['db']); + $this->response->addJSON($res); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index f32c11a1cc..2f2a981816 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -13,7 +13,6 @@ use PhpMyAdmin\Url; use function __; use function _pgettext; use function intval; -use function json_decode; use function min; /** @@ -82,14 +81,6 @@ class NormalizationController extends AbstractController $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); - if (isset($_POST['createNewTables3NF'])) { - $newtables = json_decode($_POST['newTables'], true); - $res = $this->normalization->createNewTablesFor3NF($newtables, $GLOBALS['db']); - $this->response->addJSON($res); - - return; - } - if (isset($_POST['repeatingColumns'])) { $repeatingColumns = $_POST['repeatingColumns']; $newTable = $_POST['newTable']; diff --git a/libraries/routes.php b/libraries/routes.php index 7fcdfdc00d..fe0cfb7fae 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -139,6 +139,7 @@ return static function (RouteCollector $routes): void { $routes->post('/2nf/create-new-tables', Normalization\SecondNormalForm\CreateNewTablesController::class); $routes->post('/2nf/new-tables', Normalization\SecondNormalForm\NewTablesController::class); $routes->post('/2nf/step1', Normalization\SecondNormalForm\FirstStepController::class); + $routes->post('/3nf/create-new-tables', Normalization\ThirdNormalForm\CreateNewTablesController::class); $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); }); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index b87ac7fbad..d38f99f167 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -641,6 +641,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\ThirdNormalForm\CreateNewTablesController::class => [ + 'class' => Normalization\ThirdNormalForm\CreateNewTablesController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\ThirdNormalForm\FirstStepController::class => [ 'class' => Normalization\ThirdNormalForm\FirstStepController::class, 'arguments' => [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 34fb2434e7..371bfa983e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1236,19 +1236,19 @@ parameters: path: libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php - - message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" + message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" count: 1 - path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php + path: libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php - - message: "#^Parameter \\#2 \\$tables of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects array, mixed given\\.$#" + message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" count: 1 path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php - - message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" + message: "#^Parameter \\#2 \\$tables of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects array, mixed given\\.$#" count: 1 - path: libraries/classes/Controllers/NormalizationController.php + path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php - message: "#^Property PhpMyAdmin\\\\Controllers\\\\Server\\\\BinlogController\\:\\:\\$binaryLogs type has no value type specified in iterable type array\\.$#" diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 03849e79ea..2e48ec18a3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2428,6 +2428,15 @@ <code>$partialDependencies</code> </MixedAssignment> </file> + <file src="libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php"> + <MixedArgument occurrences="2"> + <code>$_POST['newTables']</code> + <code>$newtables</code> + </MixedArgument> + <MixedAssignment occurrences="1"> + <code>$newtables</code> + </MixedAssignment> + </file> <file src="libraries/classes/Controllers/Normalization/ThirdNormalForm/FirstStepController.php"> <MixedArgument occurrences="1"> <code>$tables</code> @@ -2449,18 +2458,15 @@ </MixedAssignment> </file> <file src="libraries/classes/Controllers/NormalizationController.php"> - <MixedArgument occurrences="6"> - <code>$_POST['newTables']</code> + <MixedArgument occurrences="4"> <code>$newColumn</code> <code>$newTable</code> - <code>$newtables</code> <code>$primary_columns</code> <code>$repeatingColumns</code> </MixedArgument> - <MixedAssignment occurrences="5"> + <MixedAssignment occurrences="4"> <code>$newColumn</code> <code>$newTable</code> - <code>$newtables</code> <code>$primary_columns</code> <code>$repeatingColumns</code> </MixedAssignment> diff --git a/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php b/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php new file mode 100644 index 0000000000..a6980ce7ee --- /dev/null +++ b/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php @@ -0,0 +1,64 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Normalization\ThirdNormalForm; + +use PhpMyAdmin\ConfigStorage\Relation; +use PhpMyAdmin\Controllers\Normalization\ThirdNormalForm\CreateNewTablesController; +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\ThirdNormalForm\CreateNewTablesController + */ +class CreateNewTablesControllerTest extends AbstractTestCase +{ + public function testDefault(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $_POST['newTables'] = json_encode([ + 'test_table' => [ + 'event' => [ + 'pk' => 'eventID', + 'nonpk' => 'Start_time, DateOfEvent, NumberOfGuests, NameOfVenue, LocationOfVenue', + ], + 'table2' => ['pk' => 'Start_time', 'nonpk' => 'TypeOfEvent, period'], + ], + ]); + + // phpcs:disable Generic.Files.LineLength.TooLong + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('CREATE TABLE `event` SELECT DISTINCT `eventID`, `Start_time`, `DateOfEvent`, `NumberOfGuests`, `NameOfVenue`, `LocationOfVenue` FROM `test_table`;', []); + $dbiDummy->addResult('CREATE TABLE `table2` SELECT DISTINCT `Start_time`, `TypeOfEvent`, `period` FROM `test_table`;', []); + $dbiDummy->addResult('DROP TABLE `test_table`', []); + // phpcs:enable + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new CreateNewTablesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $this->assertSame([ + 'legendText' => 'End of step', + 'headText' => '<h3>The third step of normalization is complete.</h3>', + 'queryError' => false, + 'extra' => '', + ], $response->getJSONResult()); + } +} diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/NormalizationControllerTest.php index e608c822dc..e884b255f7 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/NormalizationControllerTest.php @@ -16,7 +16,6 @@ use PhpMyAdmin\Tests\Stubs\ResponseRenderer; use PhpMyAdmin\Transformations; use function in_array; -use function json_encode; /** * @covers \PhpMyAdmin\Controllers\NormalizationController @@ -46,44 +45,6 @@ class NormalizationControllerTest extends AbstractTestCase $GLOBALS['table'] = 'test_tbl'; } - public function testCreateNewTables3NF(): void - { - $_POST['createNewTables3NF'] = 1; - $_POST['newTables'] = json_encode([ - 'test_tbl' => [ - 'event' => [ - 'pk' => 'eventID', - 'nonpk' => 'Start_time, DateOfEvent, NumberOfGuests, NameOfVenue, LocationOfVenue', - ], - 'table2' => [ - 'pk' => 'Start_time', - 'nonpk' => 'TypeOfEvent, period', - ], - ], - ]); - - $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); - $this->dummyDbi->addSelectDb('my_db'); - $normalizationController($this->createStub(ServerRequest::class)); - $this->dummyDbi->assertAllSelectsConsumed(); - - $this->assertResponseWasSuccessfull(); - - $this->assertSame( - [ - 'legendText' => 'End of step', - 'headText' => '<h3>The third step of normalization is complete.</h3>', - 'queryError' => false, - 'extra' => '', - ], - $this->getResponseJsonResult() - ); - } - public function testNormalization(): void { $GLOBALS['db'] = 'test_db'; |