Skip to content

Commit

Permalink
Extract loadTableInfo() from SearchController constructor
Browse files Browse the repository at this point in the history
Refactors Table\SearchController tests.
Makes private methods private.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Mar 9, 2024
1 parent f6a8b78 commit 21fc3a4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 97 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4970,11 +4970,6 @@ parameters:
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Method PhpMyAdmin\\\\Controllers\\\\Table\\\\SearchController\\:\\:getColumnMinMax\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Only booleans are allowed in &&, array given on the left side\\.$#"
count: 1
Expand Down
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13116,11 +13116,6 @@
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/SearchControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/SqlControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
Expand Down
14 changes: 8 additions & 6 deletions src/Controllers/Table/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public function __construct(
private readonly DbTableExists $dbTableExists,
) {
parent::__construct($response, $template);

$this->loadTableInfo();
}

/**
Expand Down Expand Up @@ -198,6 +196,8 @@ public function __invoke(ServerRequest $request): void
return;
}

$this->loadTableInfo();

$this->addScriptFiles([
'makegrid.js',
'sql.js',
Expand Down Expand Up @@ -226,7 +226,7 @@ public function __invoke(ServerRequest $request): void
/**
* Do selection action
*/
public function doSelectionAction(): void
private function doSelectionAction(): void
{
/**
* Selection criteria have been submitted -> do the work
Expand Down Expand Up @@ -266,7 +266,7 @@ public function doSelectionAction(): void
/**
* Display selection form action
*/
public function displaySelectionFormAction(): void
private function displaySelectionFormAction(): void
{
$config = Config::getInstance();
if (! isset($GLOBALS['goto'])) {
Expand Down Expand Up @@ -295,7 +295,7 @@ public function displaySelectionFormAction(): void
/**
* Range search action
*/
public function rangeSearchAction(): void
private function rangeSearchAction(): void
{
$minMax = $this->getColumnMinMax($_POST['column']);
$this->response->addJSON('column_data', $minMax);
Expand All @@ -305,8 +305,10 @@ public function rangeSearchAction(): void
* Finds minimum and maximum value of a given column.
*
* @param string $column Column name
*
* @return mixed[]|null
*/
public function getColumnMinMax(string $column): array|null
private function getColumnMinMax(string $column): array|null
{
$sqlQuery = 'SELECT MIN(' . Util::backquote($column) . ') AS `min`, '
. 'MAX(' . Util::backquote($column) . ') AS `max` '
Expand Down
112 changes: 31 additions & 81 deletions tests/unit/Controllers/Table/SearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,50 @@

namespace PhpMyAdmin\Tests\Controllers\Table;

use PhpMyAdmin\ColumnFull;
use PhpMyAdmin\Config;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\Table\SearchController;
use PhpMyAdmin\Current;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\DbTableExists;
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
use PhpMyAdmin\Table\Search;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer as ResponseStub;
use PhpMyAdmin\Types;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;

#[CoversClass(SearchController::class)]
class SearchControllerTest extends AbstractTestCase
final class SearchControllerTest extends AbstractTestCase
{
private DatabaseInterface&MockObject $mockedDbi;

private ResponseStub $response;

private Template $template;

/**
* Setup function for test cases
*/
protected function setUp(): void
public function testRangeSearch(): void
{
parent::setUp();

/**
* SET these to avoid undefined index error
*/
$_POST['zoom_submit'] = 'zoom';

Current::$database = 'PMA';
Current::$table = 'PMA_BookMark';
Config::getInstance()->selectedServer['DisableIS'] = false;

$this->mockedDbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$this->mockedDbi->types = new Types($this->mockedDbi);

$columns = [
new ColumnFull('Field1', 'Type1', 'Collation1', false, '', null, '', '', ''),
new ColumnFull('Field2', 'Type2', 'Collation2', false, '', null, '', '', ''),
];
$this->mockedDbi->expects(self::any())->method('getColumns')
->willReturn($columns);

$showCreateTable = "CREATE TABLE `pma_bookmark` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dbase` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`user` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`label` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
`query` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`),
KEY `foreign_field` (`foreign_db`,`foreign_table`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin "
. "COMMENT='Bookmarks'";

$this->mockedDbi->expects(self::any())->method('fetchValue')
->willReturn($showCreateTable);

DatabaseInterface::$instance = $this->mockedDbi;

$this->response = new ResponseStub();
$this->template = new Template();
}

/**
* Tests for getColumnMinMax()
*/
public function testGetColumnMinMax(): void
{
$expected = 'SELECT MIN(`column`) AS `min`, MAX(`column`) AS `max` FROM `PMA`.`PMA_BookMark`';

$this->mockedDbi->expects(self::any())
->method('fetchSingleRow')
->with($expected)
->willReturn([$expected]);

$ctrl = new SearchController(
$this->response,
$this->template,
new Search($this->mockedDbi),
new Relation($this->mockedDbi),
$this->mockedDbi,
new DbTableExists($this->mockedDbi),
Current::$database = 'test_db';
Current::$table = 'test_table';

$dbiDummy = $this->createDbiDummy();
$dbiDummy->addSelectDb('test_db');
$dbiDummy->addResult('SELECT 1 FROM `test_db`.`test_table` LIMIT 1;', [['1']]);
$dbiDummy->addResult(
'SELECT MIN(`column`) AS `min`, MAX(`column`) AS `max` FROM `test_db`.`test_table`',
[['1', '2']],
);
$dbi = $this->createDatabaseInterface($dbiDummy);

$_POST['range_search'] = '1';
$_POST['column'] = 'column';
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com')
->withParsedBody(['db' => 'test_db', 'table' => 'test_table']);

$responseRenderer = new ResponseRenderer();
$controller = new SearchController(
$responseRenderer,
new Template(),
new Search($dbi),
new Relation($dbi),
$dbi,
new DbTableExists($dbi),
);
$controller($request);

$result = $ctrl->getColumnMinMax('column');
self::assertSame([$expected], $result);
self::assertSame(['column_data' => ['1', '2']], $responseRenderer->getJSONResult());
}
}

0 comments on commit 21fc3a4

Please sign in to comment.