From 4717709e09f7d9fecf149e9733813592333499dc Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Mon, 4 Nov 2024 14:39:36 +0100 Subject: [PATCH] Bump library `berlioz/core` to ^2.4 `RouterHelperTrait::path()` accepts `RouteInterface` or string instead of only a string New options: "Berlioz.router" to give parameters to the Router --- CHANGELOG.md | 8 ++++++++ composer.json | 2 +- resources/config.default.json | 2 ++ src/Helper/RouterHelperTrait.php | 4 ++-- src/Router/RouterBuilder.php | 2 +- tests/AbstractTestCase.php | 5 +++++ tests/Helper/RouterHelperTraitTest.php | 23 ++++++++++++++++++++--- tests_env/config/config.json | 3 +++ 8 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15d4fd0..01319d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.3.0] - 2024-11-04 + +### Changed + +- Bump library `berlioz/core` to ^2.4 +- `RouterHelperTrait::path()` accepts `RouteInterface` or string instead of only a string +- New options: "Berlioz.router" to give parameters to the Router + ## [2.2.7] - 2024-10-04 ### Fixed diff --git a/composer.json b/composer.json index c4bd135..05cacf4 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "berlioz/core": "^2.2", "berlioz/flash-bag": "^1.0", "berlioz/http-message": "^2.0", - "berlioz/router": "^2.0", + "berlioz/router": "^2.4", "berlioz/twig-package": "^2.1", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0" diff --git a/resources/config.default.json b/resources/config.default.json index d1b3c75..81499af 100644 --- a/resources/config.default.json +++ b/resources/config.default.json @@ -19,6 +19,8 @@ "99": { "redirection": "Berlioz\\Http\\Core\\Http\\Middleware\\RedirectionMiddleware" } + }, + "router": { } }, "maintenance": false diff --git a/src/Helper/RouterHelperTrait.php b/src/Helper/RouterHelperTrait.php index d649388..4f66d9b 100644 --- a/src/Helper/RouterHelperTrait.php +++ b/src/Helper/RouterHelperTrait.php @@ -53,13 +53,13 @@ protected function getRoute(): ?RouteInterface /** * Generate path. * - * @param string $name + * @param string|RouteInterface $name * @param array|RouteAttributes $parameters * * @return UriInterface * @throws RoutingException */ - protected function path(string $name, array|RouteAttributes $parameters = []): UriInterface + protected function path(string|RouteInterface $name, array|RouteAttributes $parameters = []): UriInterface { return Uri::createFromString($this->getRouter()->generate($name, $parameters)); } diff --git a/src/Router/RouterBuilder.php b/src/Router/RouterBuilder.php index c15107e..2bc48a4 100644 --- a/src/Router/RouterBuilder.php +++ b/src/Router/RouterBuilder.php @@ -44,7 +44,7 @@ public function __construct(protected Config $config) */ public function reset(): void { - $this->router = new Router(); + $this->router = new Router(options: (array)$this->config->get('berlioz.router', [])); } /** diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index ffe6564..f0c30ca 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -20,6 +20,11 @@ abstract class AbstractTestCase extends TestCase { + protected function setUp(): void + { + $_SERVER['HTTP_X_FORWARDED_PREFIX'] = null; + } + /** * Get app. * diff --git a/tests/Helper/RouterHelperTraitTest.php b/tests/Helper/RouterHelperTraitTest.php index 7c1ae3e..772d226 100644 --- a/tests/Helper/RouterHelperTraitTest.php +++ b/tests/Helper/RouterHelperTraitTest.php @@ -13,16 +13,16 @@ namespace Berlioz\Http\Core\Tests\Helper; use Berlioz\Core\Core; -use Berlioz\Http\Message\ServerRequest; use Berlioz\Http\Core\App\HttpApp; use Berlioz\Http\Core\Helper\RouterHelperTrait; use Berlioz\Http\Core\TestProject\FakeDefaultDirectories; +use Berlioz\Http\Core\Tests\AbstractTestCase; +use Berlioz\Http\Message\ServerRequest; use Berlioz\Router\Exception\RoutingException; use Berlioz\Router\Route; use Berlioz\Router\RouterInterface; -use PHPUnit\Framework\TestCase; -class RouterHelperTraitTest extends TestCase +class RouterHelperTraitTest extends AbstractTestCase { private function getHelper() { @@ -69,6 +69,23 @@ public function testPath() $this->assertEquals('/controller1/method1', (string)$helper->path('c1m1')); } + public function testPath_withPrefix() + { + $_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/super-prefix'; + + $helper = $this->getHelper(); + + $this->assertEquals('/super-prefix/controller1/method1', (string)$helper->path('c1m1')); + } + + public function testPath_withRoute() + { + $helper = $this->getHelper(); + + $router = $helper->getApp()->getRouter(); + $this->assertEquals('/controller1/method1', (string)$helper->path($router->getRoute('c1m1'))); + } + public function testPath_missingAttribute() { $this->expectException(RoutingException::class); diff --git a/tests_env/config/config.json b/tests_env/config/config.json index 4279ac0..d5929b7 100644 --- a/tests_env/config/config.json +++ b/tests_env/config/config.json @@ -15,6 +15,9 @@ "Berlioz\\Http\\Core\\TestProject\\Http\\Middleware\\QuxMiddleware" ] } + }, + "router": { + "X-Forwarded-Prefix": true } }, "controllers": [