/
jagepard
/
Rudra-Router
Обзор
Документация
Войти
/
jagepard
/
Rudra-Router
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/RouterTest.php
164 строки
5 KB
Jagepard
fix
03 июл 2026, 21:22
03 июл 2026, 21:22
7f4fb20
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> * @license https://mozilla.org/MPL/2.0/ MPL-2.0 */ namespace Rudra\Router\Tests; use Rudra\Container\Facades\Rudra; use Rudra\Container\Interfaces\RudraInterface; use Rudra\Container\Rudra as R; use Rudra\Router\RouterFacade as Router; use Rudra\Router\Tests\Stub\Controllers\MainController; class RouterTest extends \PHPUnit\Framework\TestCase { protected function setContainer() { R::$rudra = null; Rudra::binding()->set([RudraInterface::class => Rudra::run()]); Rudra::config()->set(["environment" => "test"]); } protected function setRouteEnvironment(string $requestUri, string $requestMethod, string $pattern, string $controller = MainController::class): void { $_SERVER["REQUEST_URI"] = $requestUri; $_SERVER["REQUEST_METHOD"] = $requestMethod; $method = strtolower($requestMethod); $action = "action" . ucfirst($method); $this->setContainer(); \Rudra\Router\RouterFacade::$method($pattern, [$controller, "action" . ucfirst($method)]); $this->assertEquals($requestMethod, Rudra::config()->get($action)); } public function testGetWithFullQualifiedNamespace() { $this->setRouteEnvironment( "test/page", "GET", "/test/page", MainController::class ); } public function testGet(): void { $this->setRouteEnvironment("test/page?id=98", "GET", "/test/page"); } public function testPost(): void { $this->setRouteEnvironment("test/page?some=123", "POST", "/test/page"); } public function testPut(): void { $this->setRouteEnvironment("test/page", 'PUT', "/test/page"); } public function testPatch(): void { $this->setRouteEnvironment("test/page", 'PATCH', "/test/page"); } public function testDelete(): void { $this->setRouteEnvironment("test/page", 'DELETE', "/test/page"); } public function testAny(): void { $_SERVER["REQUEST_URI"] = "test/page"; $_SERVER["REQUEST_METHOD"] = "PATCH"; $this->setContainer(); Router::any('/test/page',[MainController::class, 'actionAny']); $this->assertEquals("ANY", Rudra::config()->get("actionAny")); } protected function setRouteResourceEnvironment( string $requestMethod, string $action, string $requestUri ): void { $_SERVER["REQUEST_URI"] = $requestUri; $_SERVER["REQUEST_METHOD"] = $requestMethod; $this->setContainer(); Router::resource( "api/users", "api/user", MainController::class, ['index', 'read', 'create', 'update', 'delete'] ); $this->assertEquals($action, Rudra::config()->get($action)); } public function testResource(): void { // Collection routes (plural URL, no :id) $this->setRouteResourceEnvironment("GET", "index", "api/users"); $this->setRouteResourceEnvironment("POST", "create", "api/users"); // Single-item routes (singular URL + /:id) $this->setRouteResourceEnvironment("GET", "read", "api/user/123"); $this->setRouteResourceEnvironment("PUT", "update", "api/user/123"); $this->setRouteResourceEnvironment("PATCH", "update", "api/user/123"); $this->setRouteResourceEnvironment("DELETE", "delete", "api/user/123"); } protected function setRouteResourcePostEnvironment(string $spoofedMethod, string $action): void { $_SERVER["REQUEST_URI"] = "api/user/123"; $_SERVER["REQUEST_METHOD"] = "POST"; $_POST["_method"] = $spoofedMethod; $this->setContainer(); Router::resource( "api/users", "api/user", MainController::class, ['index', 'read', 'create', 'update', 'delete'] ); $this->assertEquals($action, Rudra::config()->get($action)); } public function testResourcePost(): void { $this->setRouteResourcePostEnvironment("PUT", "update"); $this->setRouteResourcePostEnvironment("PATCH", "update"); $this->setRouteResourcePostEnvironment("DELETE", "delete"); } public function testClosure() { $_SERVER["REQUEST_URI"] = "test/page"; $_SERVER["REQUEST_METHOD"] = "GET"; $this->setContainer(); Router::get("test/page", function () { Rudra::config()->set(["closure" => "closure"]); }); $this->assertEquals("closure", Rudra::config()->get("closure")); } public function testRegex(): void { $_SERVER["REQUEST_URI"] = "test/12"; $_SERVER["REQUEST_METHOD"] = "GET"; $this->setContainer(); Router::get("test/:[\d]{1,3}", [MainController::class, 'actionRegexGet']); $this->assertEquals('regex', Rudra::config()->get("regex")); } }