-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19151 from MauricioFauth/stmt-history-middleware
Extract Footer::setHistory() into a middleware
- Loading branch information
Showing
6 changed files
with
174 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Http\Middleware; | ||
|
||
use PhpMyAdmin\Config; | ||
use PhpMyAdmin\ConfigStorage\Relation; | ||
use PhpMyAdmin\Current; | ||
use PhpMyAdmin\DatabaseInterface; | ||
use PhpMyAdmin\Http\ServerRequest; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function assert; | ||
|
||
final class StatementHistory implements MiddlewareInterface | ||
{ | ||
private readonly Relation $relation; | ||
private readonly DatabaseInterface $dbi; | ||
|
||
public function __construct( | ||
private readonly Config $config, | ||
DatabaseInterface|null $dbi = null, | ||
Relation|null $relation = null, | ||
) { | ||
$this->dbi = $dbi ?? DatabaseInterface::getInstance(); | ||
$this->relation = $relation ?? new Relation($this->dbi, $this->config); | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
assert($request instanceof ServerRequest); | ||
$response = $handler->handle($request); | ||
|
||
if ( | ||
! $request->has('no_history') | ||
&& empty($GLOBALS['error_message']) | ||
&& $GLOBALS['sql_query'] !== '' | ||
&& $this->dbi->isConnected() | ||
) { | ||
$this->relation->setHistory( | ||
Current::$database, | ||
Current::$table, | ||
$this->config->selectedServer['user'], | ||
$GLOBALS['sql_query'], | ||
); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Tests\Http\Middleware; | ||
|
||
use PhpMyAdmin\Config; | ||
use PhpMyAdmin\ConfigStorage\Relation; | ||
use PhpMyAdmin\Current; | ||
use PhpMyAdmin\DatabaseInterface; | ||
use PhpMyAdmin\Http\Factory\ServerRequestFactory; | ||
use PhpMyAdmin\Http\Middleware\StatementHistory; | ||
use PhpMyAdmin\Tests\AbstractTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
#[CoversClass(StatementHistory::class)] | ||
final class StatementHistoryTest extends AbstractTestCase | ||
{ | ||
public function testStatementHistory(): void | ||
{ | ||
Current::$database = 'test_db'; | ||
Current::$table = 'test_table'; | ||
$GLOBALS['sql_query'] = 'SELECT 1;'; | ||
|
||
$config = new Config(); | ||
$config->selectedServer['user'] = 'test_user'; | ||
$dbi = self::createStub(DatabaseInterface::class); | ||
$dbi->method('isConnected')->willReturn(true); | ||
$relation = self::createMock(Relation::class); | ||
$relation->expects(self::once())->method('setHistory')->with( | ||
self::identicalTo('test_db'), | ||
self::identicalTo('test_table'), | ||
self::identicalTo('test_user'), | ||
self::identicalTo('SELECT 1;'), | ||
); | ||
$statementHistory = new StatementHistory($config, $dbi, $relation); | ||
|
||
$response = self::createStub(ResponseInterface::class); | ||
$handler = self::createStub(RequestHandlerInterface::class); | ||
$handler->method('handle')->willReturn($response); | ||
|
||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/'); | ||
|
||
self::assertSame($response, $statementHistory->process($request, $handler)); | ||
} | ||
|
||
#[TestWith(['true', 'SELECT 1;', true])] | ||
#[TestWith([null, '', true])] | ||
#[TestWith([null, 'SELECT 1;', false])] | ||
public function testSkipHistory(string|null $noHistoryParam, string $sqlQuery, bool $isConnected): void | ||
{ | ||
$GLOBALS['sql_query'] = $sqlQuery; | ||
|
||
$dbi = self::createStub(DatabaseInterface::class); | ||
$dbi->method('isConnected')->willReturn($isConnected); | ||
$relation = self::createMock(Relation::class); | ||
$relation->expects(self::never())->method('setHistory'); | ||
$statementHistory = new StatementHistory(new Config(), $dbi, $relation); | ||
|
||
$response = self::createStub(ResponseInterface::class); | ||
$handler = self::createStub(RequestHandlerInterface::class); | ||
$handler->method('handle')->willReturn($response); | ||
|
||
$parsedBody = $noHistoryParam !== null ? ['no_history' => $noHistoryParam] : []; | ||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/') | ||
->withParsedBody($parsedBody); | ||
|
||
self::assertSame($response, $statementHistory->process($request, $handler)); | ||
} | ||
} |