Skip to content

Commit

Permalink
Remove containerBuilder global variable
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 1514117 commit 09e7604
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 117 deletions.
55 changes: 0 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19415,21 +19415,6 @@ parameters:
count: 2
path: test/classes/AbstractTestCase.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 4
path: test/classes/AbstractTestCase.php

-
message: "#^Cannot call method set\\(\\) on mixed\\.$#"
count: 2
path: test/classes/AbstractTestCase.php

-
message: "#^Cannot call method setAlias\\(\\) on mixed\\.$#"
count: 2
path: test/classes/AbstractTestCase.php

-
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\) with 'formula' and array\\{id\\: non\\-empty\\-string, name\\: string, precondition\\?\\: non\\-empty\\-string, formula\\: non\\-empty\\-string, test\\: non\\-empty\\-string, issue\\: string, recommendation\\: string, justification\\: string, \\.\\.\\.\\} will always evaluate to true\\.$#"
count: 1
Expand Down Expand Up @@ -19760,11 +19745,6 @@ parameters:
count: 1
path: test/classes/ConfigTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: test/classes/Controllers/Database/MultiTableQuery/TablesControllerTest.php

-
message: "#^Parameter \\#1 \\$needle of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -19810,11 +19790,6 @@ parameters:
count: 1
path: test/classes/Controllers/Database/StructureControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: test/classes/Controllers/Database/StructureControllerTest.php

-
message: "#^Cannot use array destructuring on mixed\\.$#"
count: 8
Expand All @@ -19835,16 +19810,6 @@ parameters:
count: 1
path: test/classes/Controllers/Database/StructureControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: test/classes/Controllers/Import/ImportControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 2
path: test/classes/Controllers/NavigationControllerTest.php

-
message: "#^Parameter \\#2 \\$haystack of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, mixed given\\.$#"
count: 4
Expand Down Expand Up @@ -19915,21 +19880,6 @@ parameters:
count: 7
path: test/classes/Controllers/Server/VariablesControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 2
path: test/classes/Controllers/Sql/EnumValuesControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 2
path: test/classes/Controllers/Sql/SetValuesControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: test/classes/Controllers/Table/OperationsControllerTest.php

-
message: "#^Property PhpMyAdmin\\\\Config\\:\\:\\$settings \\(array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\) does not accept array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\.$#"
count: 1
Expand Down Expand Up @@ -19965,11 +19915,6 @@ parameters:
count: 1
path: test/classes/Controllers/Table/SearchControllerTest.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: test/classes/Controllers/Table/SearchControllerTest.php

-
message: "#^Cannot call method method\\(\\) on mixed\\.$#"
count: 1
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
compression: 'none'|'zip'|'gzip',
conn_error: string,
ConfigFile: PhpMyAdmin\Config\ConfigFile,
containerBuilder: Symfony\Component\DependencyInjection\ContainerBuilder,
csv_columns: bool,
csv_enclosed: string,
csv_escaped: string,
Expand Down
15 changes: 7 additions & 8 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
*/
class Core
{
public static ContainerBuilder|null $containerBuilder = null;

/**
* Removes insecure parts in a path; used before include() or
* require() when a part of the path comes from an insecure source
Expand Down Expand Up @@ -762,18 +764,15 @@ public static function checkSqlQuerySignature(string $sqlQuery, string $signatur

public static function getContainerBuilder(): ContainerBuilder
{
$containerBuilder = $GLOBALS['containerBuilder'] ?? null;
if ($containerBuilder instanceof ContainerBuilder) {
return $containerBuilder;
if (self::$containerBuilder !== null) {
return self::$containerBuilder;
}

$containerBuilder = new ContainerBuilder();
$loader = new PhpFileLoader($containerBuilder, new FileLocator(ROOT_PATH . 'app'));
self::$containerBuilder = new ContainerBuilder();
$loader = new PhpFileLoader(self::$containerBuilder, new FileLocator(ROOT_PATH . 'app'));
$loader->load('services_loader.php');

$GLOBALS['containerBuilder'] = $containerBuilder;

return $containerBuilder;
return self::$containerBuilder;
}

public static function populateRequestWithEncryptedQueryParams(ServerRequest $request): ServerRequest
Expand Down
25 changes: 12 additions & 13 deletions test/classes/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,28 @@ protected function setUp(): void
Cache::purge();

(new ReflectionProperty(Relation::class, 'cache'))->setValue(null, null);
}

protected function loadContainerBuilder(): void
{
$GLOBALS['containerBuilder'] = Core::getContainerBuilder();
Core::$containerBuilder = null;
}

protected function loadDbiIntoContainerBuilder(): void
{
$GLOBALS['containerBuilder']->set(DatabaseInterface::class, DatabaseInterface::getInstance());
$GLOBALS['containerBuilder']->setAlias('dbi', DatabaseInterface::class);
$containerBuilder = Core::getContainerBuilder();
$containerBuilder->set(DatabaseInterface::class, DatabaseInterface::getInstance());
$containerBuilder->setAlias('dbi', DatabaseInterface::class);
}

protected function loadResponseIntoContainerBuilder(): void
{
$response = new ResponseRenderer();
$GLOBALS['containerBuilder']->set(ResponseRenderer::class, $response);
$GLOBALS['containerBuilder']->setAlias('response', ResponseRenderer::class);
$containerBuilder = Core::getContainerBuilder();
$containerBuilder->set(ResponseRenderer::class, $response);
$containerBuilder->setAlias('response', ResponseRenderer::class);
}

protected function getResponseHtmlResult(): string
{
/** @var ResponseRenderer $response */
$response = $GLOBALS['containerBuilder']->get(ResponseRenderer::class);
$response = Core::getContainerBuilder()->get(ResponseRenderer::class);

return $response->getHTMLResult();
}
Expand All @@ -119,23 +117,23 @@ protected function getResponseHtmlResult(): string
protected function getResponseJsonResult(): array
{
/** @var ResponseRenderer $response */
$response = $GLOBALS['containerBuilder']->get(ResponseRenderer::class);
$response = Core::getContainerBuilder()->get(ResponseRenderer::class);

return $response->getJSONResult();
}

protected function assertResponseWasNotSuccessfull(): void
{
/** @var ResponseRenderer $response */
$response = $GLOBALS['containerBuilder']->get(ResponseRenderer::class);
$response = Core::getContainerBuilder()->get(ResponseRenderer::class);

$this->assertFalse($response->hasSuccessState(), 'expected the request to fail');
}

protected function assertResponseWasSuccessfull(): void
{
/** @var ResponseRenderer $response */
$response = $GLOBALS['containerBuilder']->get(ResponseRenderer::class);
$response = Core::getContainerBuilder()->get(ResponseRenderer::class);

$this->assertTrue($response->hasSuccessState(), 'expected the request not to fail');
}
Expand Down Expand Up @@ -199,6 +197,7 @@ protected function setProxySettings(): void
*/
protected function tearDown(): void
{
Core::$containerBuilder = null;
DatabaseInterface::$instance = null;
Config::$instance = null;
(new ReflectionProperty(Template::class, 'twig'))->setValue(null, null);
Expand Down
2 changes: 0 additions & 2 deletions test/classes/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ final class ApplicationTest extends AbstractTestCase
{
public function testInit(): void
{
$GLOBALS['containerBuilder'] = null;
$application = Core::getContainerBuilder()->get(Application::class);
self::assertInstanceOf(Application::class, $application);
self::assertSame($application, Application::init());
$GLOBALS['containerBuilder'] = null;
}

#[BackupStaticProperties(true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Tests\Controllers\Database\MultiTableQuery;

use PhpMyAdmin\Controllers\Database\MultiTableQuery\TablesController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Tests\AbstractTestCase;
Expand All @@ -28,8 +29,6 @@ protected function setUp(): void
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
DatabaseInterface::$instance = $this->dbi;

parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

$GLOBALS['server'] = 1;
Expand All @@ -43,7 +42,7 @@ public function testGetForeignKeyConstrainsForTable(): void
$_GET['db'] = 'test';

/** @var TablesController $multiTableQueryController */
$multiTableQueryController = $GLOBALS['containerBuilder']->get(TablesController::class);
$multiTableQueryController = Core::getContainerBuilder()->get(TablesController::class);
$request = $this->createStub(ServerRequest::class);
$request->method('getQueryParam')->willReturn($_GET['tables'], $_GET['db']);
$multiTableQueryController($request);
Expand Down
5 changes: 2 additions & 3 deletions test/classes/Controllers/Database/StructureControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\Config\PageSettings;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\Database\StructureController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\DbTableExists;
use PhpMyAdmin\Http\ServerRequest;
Expand Down Expand Up @@ -372,15 +373,13 @@ public function testDisplayTableList(): void
*/
public function testGetValuesForMroongaTable(): void
{
parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

$GLOBALS['db'] = 'testdb';
$GLOBALS['table'] = 'mytable';

/** @var StructureController $structureController */
$structureController = $GLOBALS['containerBuilder']->get(StructureController::class);
$structureController = Core::getContainerBuilder()->get(StructureController::class);

$this->assertSame(
[[], '', '', 0],
Expand Down
1 change: 0 additions & 1 deletion test/classes/Controllers/Export/ExportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected function setUp(): void
{
parent::setUp();

$this->loadContainerBuilder();
$this->dummyDbi = $this->createDbiDummy();
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
DatabaseInterface::$instance = $this->dbi;
Expand Down
5 changes: 2 additions & 3 deletions test/classes/Controllers/Import/ImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\Import\ImportController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Tests\AbstractTestCase;
Expand All @@ -30,8 +31,6 @@ protected function setUp(): void

public function testIndexParametrized(): void
{
parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

parent::setLanguage();
Expand Down Expand Up @@ -82,7 +81,7 @@ public function testIndexParametrized(): void
);

/** @var ImportController $importController */
$importController = $GLOBALS['containerBuilder']->get(ImportController::class);
$importController = Core::getContainerBuilder()->get(ImportController::class);
$this->dummyDbi->addSelectDb('pma_test');
$this->dummyDbi->addSelectDb('pma_test');
$importController($request);
Expand Down
9 changes: 3 additions & 6 deletions test/classes/Controllers/NavigationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\NavigationController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Tests\AbstractTestCase;
Expand All @@ -32,8 +33,6 @@ protected function setUp(): void

public function testIndex(): void
{
parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

parent::setLanguage();
Expand Down Expand Up @@ -125,7 +124,7 @@ public function testIndex(): void
);

/** @var NavigationController $navigationController */
$navigationController = $GLOBALS['containerBuilder']->get(NavigationController::class);
$navigationController = Core::getContainerBuilder()->get(NavigationController::class);
$_POST['full'] = '1';

$request = $this->createStub(ServerRequest::class);
Expand Down Expand Up @@ -184,8 +183,6 @@ public function testIndex(): void

public function testIndexWithPosAndValue(): void
{
parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

parent::setLanguage();
Expand Down Expand Up @@ -277,7 +274,7 @@ public function testIndexWithPosAndValue(): void
);

/** @var NavigationController $navigationController */
$navigationController = $GLOBALS['containerBuilder']->get(NavigationController::class);
$navigationController = Core::getContainerBuilder()->get(NavigationController::class);
$_POST['full'] = '1';

$request = $this->createStub(ServerRequest::class);
Expand Down
2 changes: 0 additions & 2 deletions test/classes/Controllers/Normalization/MainControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ protected function setUp(): void
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
DatabaseInterface::$instance = $this->dbi;

parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

$GLOBALS['server'] = 1;
Expand Down
7 changes: 3 additions & 4 deletions test/classes/Controllers/Sql/EnumValuesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Tests\Controllers\Sql;

use PhpMyAdmin\Controllers\Sql\EnumValuesController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Tests\AbstractTestCase;
Expand All @@ -26,8 +27,6 @@ protected function setUp(): void
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
DatabaseInterface::$instance = $this->dbi;

parent::loadContainerBuilder();

parent::loadDbiIntoContainerBuilder();

$GLOBALS['server'] = 1;
Expand All @@ -53,7 +52,7 @@ public function testGetEnumValuesError(): void
]);

/** @var EnumValuesController $sqlController */
$sqlController = $GLOBALS['containerBuilder']->get(EnumValuesController::class);
$sqlController = Core::getContainerBuilder()->get(EnumValuesController::class);
$sqlController($request);

$this->assertResponseWasNotSuccessfull();
Expand Down Expand Up @@ -94,7 +93,7 @@ public function testGetEnumValuesSuccess(): void
]);

/** @var EnumValuesController $sqlController */
$sqlController = $GLOBALS['containerBuilder']->get(EnumValuesController::class);
$sqlController = Core::getContainerBuilder()->get(EnumValuesController::class);
$sqlController($request);

$this->assertResponseWasSuccessfull();
Expand Down

0 comments on commit 09e7604

Please sign in to comment.