Skip to content

Commit

Permalink
PoC array like routes
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 18, 2024
1 parent 81b34db commit 3bce83c
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions tests/ArrayLikeRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Http\Method;
use Yiisoft\Router\Route;
use Yiisoft\Router\Tests\Support\TestController;
use Yiisoft\Router\Tests\Support\TestMiddleware1;
use Yiisoft\Router\Tests\Support\TestMiddleware2;

final class ArrayLikeRouteTest extends TestCase
{
/**
* @dataProvider dataArrayDefinition
*/
public function testName(array $definition, Route $route): void
{
$collection = $this->fromArray($definition);

$keys = [
'name',
'pattern',
'host',
'hosts',
'methods',
'defaults',
'override',
'hasMiddlewares',
'enabledMiddlewares',
];
foreach ($keys as $key) {
$this->assertSame($collection[0]->getData($key), $route->getData($key));
}
}

public static function dataArrayDefinition(): iterable
{
yield 'GET' => [
[
'/' => [],
],
Route::get('/'),
];
yield 'POST' => [
[
'/' => [
'method' => Method::POST,
],
],
Route::post('/'),
];
yield 'GET, POST, DELETE' => [
[
'/' => [
'methods' => [Method::GET, Method::POST, Method::DELETE],
],
],
Route::methods([Method::GET, Method::POST, Method::DELETE], '/'),
];

yield 'name' => [
[
'/' => [
'name' => 'test.route',
],
],
Route::get('/')->name('test.route'),
];
yield 'action' => [
[
'/' => [
'action' => [TestController::class, 'action'],
],
],
Route::get('/')
->action([TestController::class, 'action']),
];
yield 'middlewares' => [
[
'/' => [
'action' => [TestController::class, 'action'],
'middlewares' => [TestMiddleware1::class, TestMiddleware2::class],
],
],
Route::get('/')
->middleware(TestMiddleware1::class, TestMiddleware2::class)
->action([TestController::class, 'action']),
];
}

/**
* @param array $routes
* @return Route[]
*/
private function fromArray(array $routes): array
{
$collection = [];
foreach ($routes as $pattern => $data) {
if (isset($data['method'])) {
$route = Route::methods([$data['method']], $pattern);
} elseif (isset($data['methods'])) {
$route = Route::methods($data['methods'], $pattern);
} else {
$route = Route::get($pattern);
}
if (isset($data['name'])) {
$route = $route->name($data['name']);
}
if (isset($data['host'])) {
$route = $route->host($data['host']);
}
if (isset($data['defaults'])) {
$route = $route->defaults($data['defaults']);
}
if (isset($data['middlewares'])) {
$route = $route->middleware(...$data['middlewares']);
}
if (isset($data['action'])) {
$route = $route->action($data['action']);
}

$collection[] = $route;
}

return $collection;
}
}

0 comments on commit 3bce83c

Please sign in to comment.