phpmyadmin/test/classes/Controllers/HomeControllerTest.php
Maurício Meneghini Fauth 93fa48e286
Refactor the ServerRequest::getRoute() method
Extracts the redirection to database and table pages to the
HomeController for an actual redirection instead of an implicit one.

Removes the Application::getRequest() method as the ServerRequest object
should be an immutable object.

Adds a Routing::$route static variable to be used at the places that the
ServerRequest object is not available.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2023-08-12 01:45:12 -03:00

60 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Controllers;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\HomeController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Theme\ThemeManager;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(HomeController::class)]
final class HomeControllerTest extends AbstractTestCase
{
public function testRedirectToDatabasePage(): void
{
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/')
->withQueryParams(['db' => 'test_db']);
$controller = new HomeController(
$this->createStub(ResponseRenderer::class),
$this->createStub(Template::class),
$this->createStub(Config::class),
$this->createStub(ThemeManager::class),
$this->createStub(DatabaseInterface::class),
ResponseFactory::create(),
);
$response = $controller($request);
$this->assertNotNull($response);
$this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
$this->assertSame('./index.php?route=/database/structure&db=test_db', $response->getHeaderLine('Location'));
$this->assertSame('', (string) $response->getBody());
}
public function testRedirectToTablePage(): void
{
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/')
->withQueryParams(['db' => 'test_db', 'table' => 'test_table']);
$controller = new HomeController(
$this->createStub(ResponseRenderer::class),
$this->createStub(Template::class),
$this->createStub(Config::class),
$this->createStub(ThemeManager::class),
$this->createStub(DatabaseInterface::class),
ResponseFactory::create(),
);
$response = $controller($request);
$this->assertNotNull($response);
$this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
$this->assertSame('./index.php?route=/sql&db=test_db&table=test_table', $response->getHeaderLine('Location'));
$this->assertSame('', (string) $response->getBody());
}
}