Skip to content

Commit

Permalink
Bump library berlioz/core to ^2.4
Browse files Browse the repository at this point in the history
`RouterHelperTrait::path()` accepts `RouteInterface` or string instead of only a string
New options: "Berlioz.router" to give parameters to the Router
  • Loading branch information
ElGigi committed Nov 4, 2024
1 parent cbb7837 commit 4717709
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions resources/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"99": {
"redirection": "Berlioz\\Http\\Core\\Http\\Middleware\\RedirectionMiddleware"
}
},
"router": {
}
},
"maintenance": false
Expand Down
4 changes: 2 additions & 2 deletions src/Helper/RouterHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Router/RouterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

abstract class AbstractTestCase extends TestCase
{
protected function setUp(): void
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = null;
}

/**
* Get app.
*
Expand Down
23 changes: 20 additions & 3 deletions tests/Helper/RouterHelperTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests_env/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"Berlioz\\Http\\Core\\TestProject\\Http\\Middleware\\QuxMiddleware"
]
}
},
"router": {
"X-Forwarded-Prefix": true
}
},
"controllers": [
Expand Down

0 comments on commit 4717709

Please sign in to comment.