Files
delta_framework/tests/Routing/RouterTest.php
T
rsgrinko 12e910249b Этап 3 рефакторинга: HTTP-ядро и закрытие загрузки файлов
Запрос превращается в ответ в одном месте — Kernel::handle(Request): Response.

- Delta\Http\Request: запрос как объект, capture() из суперглобалей,
  типизированные аксессоры вместо обращений к $_GET/$_POST/$_FILES
- Delta\Http\Response: статус, заголовки, тело, единственный send()
- Delta\Http\Kernel: ловит любое исключение из экшена
- Delta\Routing\Router: экземплярный, dispatch(Request): Response.
  Старый Core\Models\Router удалён. Обработчики старого стиля
  продолжают работать: их вывод перехватывается буферизацией, поэтому
  маршруты переводятся на новый стиль по одному
- Delta\Error\ErrorHandler: центральный обработчик ошибок с записью
  в журнал вместо зависимости от display_errors

Безопасность:
- Delta\Http\UploadedFile + File::saveUploadedFile() закрывают
  неограниченную загрузку файлов: расширение выводится из реального
  MIME-типа содержимого, а не из имени файла от клиента, проверяются
  тип и размер. Переведены все три точки загрузки. Прежняя защита
  uploads/.htaccess не работала: сервер nginx, .htaccess не читается
- Файлы журналов пишутся с расширением .php и заглушкой в первой
  строке: каталог логов внутри корня сайта, иначе стектрейсы, IP
  и логины читались бы по URL

Тесты: Request, Response, Router, UploadedFile — всего 47 в проекте.

Перенос docroot в public/ не выполнен: требует правки конфига nginx
на сервере. Инструкция в docs/ARCHITECTURE_PLAN.md.
2026-08-14 00:59:52 +03:00

137 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Routing;
use Delta\Http\Request;
use Delta\Http\Response;
use Delta\Routing\Router;
use PHPUnit\Framework\TestCase;
use RuntimeException;
/**
* Тесты маршрутизатора
*/
class RouterTest extends TestCase
{
/**
* Запрос к указанному пути
*
* @param string $path Путь
* @param string $method HTTP-метод
*
* @return Request Запрос
*/
private function request(string $path, string $method = 'GET'): Request
{
return new Request($method, $path);
}
public function testDispatchesToHandlerReturningResponse(): void
{
$router = new Router();
$router->route('/', static fn(): Response => Response::html('главная'));
$response = $router->dispatch($this->request('/'));
$this->assertSame('главная', $response->body());
$this->assertSame(200, $response->status());
}
public function testWrapsLegacyHandlerOutputIntoResponse(): void
{
$router = new Router();
$router->route('/legacy', static function (): void {
echo 'напечатано в вывод';
});
$response = $router->dispatch($this->request('/legacy'));
$this->assertSame('напечатано в вывод', $response->body());
}
public function testPassesRouteParametersToHandler(): void
{
$router = new Router();
$router->route('/users/(\d+)', static fn(string $id): Response => Response::html('пользователь ' . $id));
$response = $router->dispatch($this->request('/users/42'));
$this->assertSame('пользователь 42', $response->body());
}
public function testFallsBackToErrorRoute(): void
{
$router = new Router();
$router->route('/404', static fn(): Response => Response::notFound('не найдено'));
$response = $router->dispatch($this->request('/нет-такого'));
$this->assertSame(404, $response->status());
$this->assertSame('не найдено', $response->body());
}
public function testReturnsNotFoundWithoutErrorRoute(): void
{
$router = new Router();
$this->assertSame(404, $router->dispatch($this->request('/нет'))->status());
}
public function testPaginationPatternMatchesBothForms(): void
{
$router = new Router();
$router->route('/users', static fn(): Response => Response::html('список'), true);
$this->assertSame('список', $router->dispatch($this->request('/users'))->body());
$this->assertSame('список', $router->dispatch($this->request('/users/page/3'))->body());
}
public function testOnMatchReceivesHandler(): void
{
$matched = null;
$router = new Router();
$router->onMatch(static function (callable|string $handler) use (&$matched): void {
$matched = $handler;
});
$router->route('/info', '\Tests\Routing\RouterTest::infoHandler');
$router->dispatch($this->request('/info'));
$this->assertSame('\Tests\Routing\RouterTest::infoHandler', $matched);
}
public function testQueryStringIsNotPartOfMatching(): void
{
$router = new Router();
$router->route('/users', static fn(): Response => Response::html('список'));
// path() уже не содержит строку запроса, маршрут обязан совпасть
$this->assertSame('список', $router->dispatch($this->request('/users'))->body());
}
public function testExceptionFromHandlerIsNotSwallowed(): void
{
$router = new Router();
$router->route('/boom', static function (): void {
echo 'частичный вывод';
throw new RuntimeException('сломалось');
});
$this->expectException(RuntimeException::class);
$router->dispatch($this->request('/boom'));
}
/**
* Обработчик для проверки onMatch
*
* @return Response Ответ
*/
public static function infoHandler(): Response
{
return Response::html('инфо');
}
}