Skip to content

Commit

Permalink
Remove container builder from ReplaceController
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Nov 3, 2023
1 parent 99f7145 commit 550673b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
4 changes: 4 additions & 0 deletions app/services_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,10 @@
'$transformations' => '@transformations',
'$relation' => '@relation',
'$dbi' => '@dbi',
'$sqlController' => '@' . Sql\SqlController::class,
'$databaseSqlController' => '@' . Database\SqlController::class,
'$changeController' => '@' . Table\ChangeController::class,
'$tableSqlController' => '@' . Table\SqlController::class,
],
],
Table\SearchController::class => [
Expand Down
31 changes: 8 additions & 23 deletions src/Controllers/Table/ReplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function __construct(
private Transformations $transformations,
private Relation $relation,
private DatabaseInterface $dbi,
private readonly SqlController $sqlController,
private readonly DatabaseSqlController $databaseSqlController,
private readonly ChangeController $changeController,
private readonly TableSqlController $tableSqlController,
) {
parent::__construct($response, $template);
}
Expand Down Expand Up @@ -475,44 +479,25 @@ private function doTransformations(array $mimeMap, ServerRequest $request): void
private function moveBackToCallingScript(string $gotoInclude, ServerRequest $request): void
{
$GLOBALS['active_page'] = $gotoInclude;
$container = Core::getContainerBuilder();
if ($gotoInclude === '/sql') {
/** @var SqlController $controller */
$controller = $container->get(SqlController::class);
$controller($request);
($this->sqlController)($request);

return;
}

if ($gotoInclude === '/database/sql') {
/** @var DatabaseSqlController $controller */
$controller = $container->get(DatabaseSqlController::class);
$controller($request);

return;
}

if ($gotoInclude === '/table/change') {
/** @var ChangeController $controller */
$controller = $container->get(ChangeController::class);
$controller($request);
($this->databaseSqlController)($request);

return;
}

if ($gotoInclude === '/table/sql') {
/** @var TableSqlController $controller */
$controller = $container->get(TableSqlController::class);
$controller($request);
($this->tableSqlController)($request);

return;
}

/**
* Load target page.
*/
/** @psalm-suppress UnresolvableInclude */
require ROOT_PATH . Core::securePath($gotoInclude);
($this->changeController)($request);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Table/SqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Table SQL executor
*/
final class SqlController extends AbstractController
class SqlController extends AbstractController
{
public function __construct(
ResponseRenderer $response,
Expand Down
43 changes: 26 additions & 17 deletions test/classes/Controllers/Table/ReplaceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\ConfigStorage\RelationCleanup;
use PhpMyAdmin\ConfigStorage\RelationParameters;
use PhpMyAdmin\Controllers\Database\SqlController as DatabaseSqlController;
use PhpMyAdmin\Controllers\Sql\SqlController;
use PhpMyAdmin\Controllers\Table\ChangeController;
use PhpMyAdmin\Controllers\Table\ReplaceController;
use PhpMyAdmin\Controllers\Table\SqlController as TableSqlController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\FileListing;
use PhpMyAdmin\Http\ServerRequest;
Expand All @@ -26,7 +29,6 @@
use PhpMyAdmin\Transformations;
use PHPUnit\Framework\Attributes\CoversClass;
use ReflectionProperty;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[CoversClass(ReplaceController::class)]
class ReplaceControllerTest extends AbstractTestCase
Expand Down Expand Up @@ -99,14 +101,6 @@ public function testReplace(): void
$transformations = new Transformations();
$template = new Template();
$response = new ResponseRenderer();
$replaceController = new ReplaceController(
$response,
$template,
new InsertEdit($dbi, $relation, $transformations, new FileListing(), $template),
$transformations,
$relation,
$dbi,
);

$pageSettings = $this->createStub(PageSettings::class);
$bookmarkRepository = new BookmarkRepository($dbi, $relation);
Expand All @@ -127,8 +121,19 @@ public function testReplace(): void
$pageSettings,
$bookmarkRepository,
);
$GLOBALS['containerBuilder'] = $this->createStub(ContainerBuilder::class);
$GLOBALS['containerBuilder']->method('get')->willReturn($sqlController);

$replaceController = new ReplaceController(
$response,
$template,
new InsertEdit($dbi, $relation, $transformations, new FileListing(), $template),
$transformations,
$relation,
$dbi,
$sqlController,
self::createStub(DatabaseSqlController::class),
self::createStub(ChangeController::class),
self::createStub(TableSqlController::class),
);

$GLOBALS['goto'] = 'index.php?route=/sql';
$dummyDbi->addSelectDb('my_db');
Expand All @@ -155,12 +160,16 @@ public function testGetParamsForUpdateOrInsert(): void
]);

$replaceController = new ReplaceController(
$this->createStub(ResponseRenderer::class),
$this->createStub(Template::class),
$this->createStub(InsertEdit::class),
$this->createStub(Transformations::class),
$this->createStub(Relation::class),
$this->createStub(DatabaseInterface::class),
self::createStub(ResponseRenderer::class),
self::createStub(Template::class),
self::createStub(InsertEdit::class),
self::createStub(Transformations::class),
self::createStub(Relation::class),
self::createStub(DatabaseInterface::class),
self::createStub(SqlController::class),
self::createStub(DatabaseSqlController::class),
self::createStub(ChangeController::class),
self::createStub(TableSqlController::class),
);

/** @var array $result */
Expand Down

0 comments on commit 550673b

Please sign in to comment.