got unit tests up and running

pull/596/head
n0nag0n 4 months ago
parent a12d474006
commit edcd1ad092

@ -740,65 +740,7 @@ class Engine
string $controllerClass, string $controllerClass,
array $options = [] array $options = []
): void { ): void {
// $defaultMapping = [ $this->router()->mapResource($pattern, $controllerClass, $options);
// 'GET ' => 'index',
// 'GET /create' => 'create',
// 'POST ' => 'store',
// 'GET /@id' => 'show',
// 'GET /@id/edit' => 'edit',
// 'PUT /@id' => 'update',
// 'DELETE /@id' => 'destroy'
// ];
$defaultMapping = [
'index' => 'GET ',
'create' => 'GET /create',
'store' => 'POST ',
'show' => 'GET /@id',
'edit' => 'GET /@id/edit',
'update' => 'PUT /@id',
'destroy' => 'DELETE /@id'
];
// Create a custom alias base
$aliasBase = trim(basename($pattern), '/');
if (isset($options['alias_base']) === true) {
$aliasBase = $options['alias_base'];
}
// Only use these controller methods
if (isset($options['only']) === true) {
$only = $options['only'];
$defaultMapping = array_filter($defaultMapping, function ($key) use ($only) {
return in_array($key, $only, true) === true;
}, ARRAY_FILTER_USE_KEY);
// Exclude these controller methods
} elseif (isset($options['except']) === true) {
$except = $options['except'];
$defaultMapping = array_filter($defaultMapping, function ($key) use ($except) {
return in_array($key, $except, true) === false;
}, ARRAY_FILTER_USE_KEY);
}
// Add group middleware
$middleware = [];
if (isset($options['middleware']) === true) {
$middleware = $options['middleware'];
}
$this->group(
$pattern,
function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void {
foreach ($defaultMapping as $controllerMethod => $methodPattern) {
$router->map(
$methodPattern,
$controllerClass . '->' . $controllerMethod
)->setAlias($aliasBase . '.' . $controllerMethod);
}
},
$middleware
);
} }
/** /**

@ -42,7 +42,7 @@ require_once __DIR__ . '/autoload.php';
* Routes a PATCH URL to a callback function. * Routes a PATCH URL to a callback function.
* @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a DELETE URL to a callback function. * Routes a DELETE URL to a callback function.
* @method void resource(string $pattern, string $controllerClass, array $methods = []) * @method static void resource(string $pattern, string $controllerClass, array $methods = [])
* Adds standardized RESTful routes for a controller. * Adds standardized RESTful routes for a controller.
* @method static Router router() Returns Router instance. * @method static Router router() Returns Router instance.
* @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias * @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias

@ -87,7 +87,7 @@ class Router
public function map(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): Route public function map(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): Route
{ {
// This means that the route ies defined in a group, but the defined route is the base // This means that the route is defined in a group, but the defined route is the base
// url path. Note the '' in route() // url path. Note the '' in route()
// Ex: Flight::group('/api', function() { // Ex: Flight::group('/api', function() {
// Flight::route('', function() {}); // Flight::route('', function() {});
@ -276,6 +276,69 @@ class Router
throw new Exception($exception_message); throw new Exception($exception_message);
} }
/**
* Create a resource controller customizing the methods names mapping.
*
* @param class-string $controllerClass
* @param array<string, string|array> $options
*/
public function mapResource(
string $pattern,
string $controllerClass,
array $options = []
): void {
$defaultMapping = [
'index' => 'GET ',
'create' => 'GET /create',
'store' => 'POST ',
'show' => 'GET /@id',
'edit' => 'GET /@id/edit',
'update' => 'PUT /@id',
'destroy' => 'DELETE /@id'
];
// Create a custom alias base
$aliasBase = trim(basename($pattern), '/');
if (isset($options['alias_base']) === true) {
$aliasBase = $options['alias_base'];
}
// Only use these controller methods
if (isset($options['only']) === true) {
$only = $options['only'];
$defaultMapping = array_filter($defaultMapping, function ($key) use ($only) {
return in_array($key, $only, true) === true;
}, ARRAY_FILTER_USE_KEY);
// Exclude these controller methods
} elseif (isset($options['except']) === true) {
$except = $options['except'];
$defaultMapping = array_filter($defaultMapping, function ($key) use ($except) {
return in_array($key, $except, true) === false;
}, ARRAY_FILTER_USE_KEY);
}
// Add group middleware
$middleware = [];
if (isset($options['middleware']) === true) {
$middleware = $options['middleware'];
}
$this->group(
$pattern,
function (Router $router) use ($controllerClass, $defaultMapping, $aliasBase): void {
foreach ($defaultMapping as $controllerMethod => $methodPattern) {
$router->map(
$methodPattern,
[ $controllerClass, $controllerMethod ]
)->setAlias($aliasBase . '.' . $controllerMethod);
}
},
$middleware
);
}
/** /**
* Rewinds the current route index. * Rewinds the current route index.
*/ */

@ -17,85 +17,124 @@ final class FlightRouteCompactSyntaxTest extends TestCase
public function testCanMapMethodsWithVerboseSyntax(): void public function testCanMapMethodsWithVerboseSyntax(): void
{ {
Flight::route('GET /users', [UsersController::class, 'list']); Flight::route('GET /users', [UsersController::class, 'index']);
Flight::route('POST /users', [UsersController::class, 'handleRegister']); Flight::route('DELETE /users/@id', [UsersController::class, 'destroy']);
$routes = Flight::router()->getRoutes(); $routes = Flight::router()->getRoutes();
$this->assertCount(2, $routes); $this->assertCount(2, $routes);
$this->assertSame('/users', $routes[0]->pattern); $this->assertSame('/users', $routes[0]->pattern);
$this->assertSame([UsersController::class, 'list'], $routes[0]->callback); $this->assertSame([UsersController::class, 'index'], $routes[0]->callback);
$this->assertSame('GET', $routes[0]->methods[0]); $this->assertSame('GET', $routes[0]->methods[0]);
$this->assertSame('/users', $routes[1]->pattern); $this->assertSame('/users/@id', $routes[1]->pattern);
$this->assertSame([UsersController::class, 'handleRegister'], $routes[1]->callback); $this->assertSame([UsersController::class, 'destroy'], $routes[1]->callback);
$this->assertSame('POST', $routes[1]->methods[0]); $this->assertSame('DELETE', $routes[1]->methods[0]);
} }
public function testCanMapSomeMethods(): void public function testOptionsOnly(): void
{ {
Flight::resource('/users', UsersController::class, [ Flight::resource('/users', UsersController::class, [
'GET /' => 'list', 'only' => [ 'index', 'destroy' ]
'POST /' => 'handleRegister'
]); ]);
$routes = Flight::router()->getRoutes(); $routes = Flight::router()->getRoutes();
$this->assertCount(2, $routes); $this->assertCount(2, $routes);
$this->assertSame('/users/', $routes[0]->pattern); $this->assertSame('/users', $routes[0]->pattern);
$this->assertSame('GET', $routes[0]->methods[0]); $this->assertSame('GET', $routes[0]->methods[0]);
$this->assertSame([UsersController::class, 'list'], $routes[0]->callback); $this->assertSame([UsersController::class, 'index'], $routes[0]->callback);
$this->assertSame('/users/', $routes[1]->pattern); $this->assertSame('/users/@id', $routes[1]->pattern);
$this->assertSame('POST', $routes[1]->methods[0]); $this->assertSame('DELETE', $routes[1]->methods[0]);
$this->assertSame([UsersController::class, 'handleRegister'], $routes[1]->callback); $this->assertSame([UsersController::class, 'destroy'], $routes[1]->callback);
} }
public function testCanMapDefaultMethods(): void public function testDefaultMethods(): void
{ {
Flight::resource('/posts', PostsController::class); Flight::resource('/posts', PostsController::class);
$routes = Flight::router()->getRoutes(); $routes = Flight::router()->getRoutes();
$this->assertCount(7, $routes); $this->assertCount(7, $routes);
$this->assertSame('/posts/', $routes[0]->pattern); $this->assertSame('/posts', $routes[0]->pattern);
$this->assertSame('GET', $routes[0]->methods[0]); $this->assertSame('GET', $routes[0]->methods[0]);
$this->assertSame([PostsController::class, 'index'], $routes[0]->callback); $this->assertSame([PostsController::class, 'index'], $routes[0]->callback);
$this->assertSame('posts.index', $routes[0]->alias);
$this->assertSame('/posts/@id/', $routes[1]->pattern);
$this->assertSame('/posts/create', $routes[1]->pattern);
$this->assertSame('GET', $routes[1]->methods[0]); $this->assertSame('GET', $routes[1]->methods[0]);
$this->assertSame([PostsController::class, 'show'], $routes[1]->callback); $this->assertSame([PostsController::class, 'create'], $routes[1]->callback);
$this->assertSame('posts.create', $routes[1]->alias);
$this->assertSame('/posts/create/', $routes[2]->pattern);
$this->assertSame('GET', $routes[2]->methods[0]); $this->assertSame('/posts', $routes[2]->pattern);
$this->assertSame([PostsController::class, 'create'], $routes[2]->callback); $this->assertSame('POST', $routes[2]->methods[0]);
$this->assertSame([PostsController::class, 'store'], $routes[2]->callback);
$this->assertSame('/posts/', $routes[3]->pattern); $this->assertSame('posts.store', $routes[2]->alias);
$this->assertSame('POST', $routes[3]->methods[0]);
$this->assertSame([PostsController::class, 'store'], $routes[3]->callback); $this->assertSame('/posts/@id', $routes[3]->pattern);
$this->assertSame('GET', $routes[3]->methods[0]);
$this->assertSame('/posts/@id/edit/', $routes[4]->pattern); $this->assertSame([PostsController::class, 'show'], $routes[3]->callback);
$this->assertSame('posts.show', $routes[3]->alias);
$this->assertSame('/posts/@id/edit', $routes[4]->pattern);
$this->assertSame('GET', $routes[4]->methods[0]); $this->assertSame('GET', $routes[4]->methods[0]);
$this->assertSame([PostsController::class, 'edit'], $routes[4]->callback); $this->assertSame([PostsController::class, 'edit'], $routes[4]->callback);
$this->assertSame('posts.edit', $routes[4]->alias);
$this->assertSame('/posts/@id/', $routes[5]->pattern); $this->assertSame('/posts/@id', $routes[5]->pattern);
$this->assertSame('PUT', $routes[5]->methods[0]); $this->assertSame('PUT', $routes[5]->methods[0]);
$this->assertSame([PostsController::class, 'update'], $routes[5]->callback); $this->assertSame([PostsController::class, 'update'], $routes[5]->callback);
$this->assertSame('posts.update', $routes[5]->alias);
$this->assertSame('/posts/@id/', $routes[6]->pattern); $this->assertSame('/posts/@id', $routes[6]->pattern);
$this->assertSame('DELETE', $routes[6]->methods[0]); $this->assertSame('DELETE', $routes[6]->methods[0]);
$this->assertSame([PostsController::class, 'destroy'], $routes[6]->callback); $this->assertSame([PostsController::class, 'destroy'], $routes[6]->callback);
$this->assertSame('posts.destroy', $routes[6]->alias);
} }
public function testCanMapExistingMethods(): void public function testOptionsExcept(): void
{ {
Flight::resource('/todos', TodosController::class); Flight::resource('/todos', TodosController::class, [
'except' => [ 'create', 'store', 'update', 'destroy', 'edit' ]
]);
$routes = Flight::router()->getRoutes(); $routes = Flight::router()->getRoutes();
$this->assertCount(2, $routes); $this->assertCount(2, $routes);
$this->assertSame('/todos', $routes[0]->pattern);
$this->assertSame('GET', $routes[0]->methods[0]);
$this->assertSame([TodosController::class, 'index'], $routes[0]->callback);
$this->assertSame('/todos/@id', $routes[1]->pattern);
$this->assertSame('GET', $routes[1]->methods[0]);
$this->assertSame([TodosController::class, 'show'], $routes[1]->callback);
} }
public function testOptionsMiddlewareAndAliasBase(): void
{
Flight::resource('/todos', TodosController::class, [
'middleware' => [ 'auth' ],
'alias_base' => 'nothanks'
]);
$routes = Flight::router()->getRoutes();
$this->assertCount(7, $routes);
$this->assertSame('/todos', $routes[0]->pattern);
$this->assertSame('GET', $routes[0]->methods[0]);
$this->assertSame([TodosController::class, 'index'], $routes[0]->callback);
$this->assertSame('auth', $routes[0]->middleware[0]);
$this->assertSame('nothanks.index', $routes[0]->alias);
$this->assertSame('/todos/create', $routes[1]->pattern);
$this->assertSame('GET', $routes[1]->methods[0]);
$this->assertSame([TodosController::class, 'create'], $routes[1]->callback);
$this->assertSame('auth', $routes[1]->middleware[0]);
$this->assertSame('nothanks.create', $routes[1]->alias);
}
} }

@ -4,12 +4,12 @@ namespace tests\groupcompactsyntax;
final class UsersController final class UsersController
{ {
public function list(): void public function index(): void
{ {
echo __METHOD__; echo __METHOD__;
} }
public function handleRegister(): void public function destroy(): void
{ {
echo __METHOD__; echo __METHOD__;
} }

Loading…
Cancel
Save