[TESTS][Router] Add tests for Router and use named paramenters, as we can since PHP8

This commit is contained in:
Hugo Sales 2021-07-28 21:36:24 +00:00
parent 6d22932092
commit 41e4e2de0e
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 62 additions and 8 deletions

View File

@ -83,10 +83,10 @@ class RouteLoader extends Loader
$this->rc->add($id,
new Route(
// path -- URI path
$uri_path,
path: $uri_path,
// defaults = [] -- param default values,
// and special configuration options
array_merge(
defaults: array_merge(
[
'_controller' => is_array($target) ? $target : [$target, '__invoke'],
'_format' => $options['format'] ?? 'html',
@ -97,21 +97,21 @@ class RouteLoader extends Loader
$options['defaults'] ?? []
),
// requirements = [] -- param => regex
$param_reqs,
requirements: $param_reqs,
// options = [] -- possible keys: compiler_class:, utf8
// No need for a special compiler class for now,
// Enforce UTF8
['utf8' => true],
options: ['utf8' => true],
// host = '' -- hostname (subdomain, for instance) to match,
// we don't want this
'',
host: '',
// schemes = [] -- URI schemes (https, ftp and such)
$options['schemes'] ?? [],
schemes: $options['schemes'] ?? [],
// methods = [] -- HTTP methods
$options['http-methods'] ?? $options['methods'] ?? [],
methods: $options['http-methods'] ?? $options['methods'] ?? [],
// condition = '' -- Symfony condition expression,
// see https://symfony.com/doc/current/routing.html#matching-expressions
$options['condition'] ?? ''
condition: $options['condition'] ?? ''
)
);
}
@ -122,6 +122,7 @@ class RouteLoader extends Loader
* `config/routes.php`
*
* @param mixed $resource
* @codeCoverageIgnore
*/
public function supports($resource, ?string $type = null): bool
{

53
tests/Core/RouterTest.php Normal file
View File

@ -0,0 +1,53 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Tests\Core;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
use App\Util\GNUsocialTestCase;
use Symfony\Component\Routing\Route as SRoute;
class RouterTest extends GNUsocialTestCase
{
public function testRouter()
{
parent::bootKernel();
$rl = new RouteLoader();
$rl->load('', null); // parameters ignored
$rl->connect(id: 'test_route', uri_path: '/test/{id<\d+>}', target: []);
$refl = (new \ReflectionClass($rl))->getProperty('rc');
$refl->setAccessible(true);
$routes = $refl->getValue($rl)->all();
static::assertIsArray($routes);
static::assertInstanceOf(SRoute::class, $routes['test_route']);
static::assertSame('/test/{id}', $routes['test_route']->getPath());
}
public function testURLGen()
{
parent::bootKernel();
static::assertSame('/thumbnail/1', Router::url('thumbnail', ['id' => 1]));
}
}