-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from flightphp/feat/route-compact-syntax
Group compact syntax
- Loading branch information
Showing
7 changed files
with
296 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use tests\groupcompactsyntax\PostsController; | ||
use tests\groupcompactsyntax\TodosController; | ||
use tests\groupcompactsyntax\UsersController; | ||
|
||
require_once __DIR__ . '/UsersController.php'; | ||
require_once __DIR__ . '/PostsController.php'; | ||
|
||
final class FlightRouteCompactSyntaxTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
Flight::router()->clear(); | ||
} | ||
|
||
public function testCanMapMethodsWithVerboseSyntax(): void | ||
{ | ||
Flight::route('GET /users', [UsersController::class, 'index']); | ||
Flight::route('DELETE /users/@id', [UsersController::class, 'destroy']); | ||
|
||
$routes = Flight::router()->getRoutes(); | ||
|
||
$this->assertCount(2, $routes); | ||
|
||
$this->assertSame('/users', $routes[0]->pattern); | ||
$this->assertSame([UsersController::class, 'index'], $routes[0]->callback); | ||
$this->assertSame('GET', $routes[0]->methods[0]); | ||
|
||
$this->assertSame('/users/@id', $routes[1]->pattern); | ||
$this->assertSame([UsersController::class, 'destroy'], $routes[1]->callback); | ||
$this->assertSame('DELETE', $routes[1]->methods[0]); | ||
} | ||
|
||
public function testOptionsOnly(): void | ||
{ | ||
Flight::resource('/users', UsersController::class, [ | ||
'only' => [ 'index', 'destroy' ] | ||
]); | ||
|
||
$routes = Flight::router()->getRoutes(); | ||
|
||
$this->assertCount(2, $routes); | ||
|
||
$this->assertSame('/users', $routes[0]->pattern); | ||
$this->assertSame('GET', $routes[0]->methods[0]); | ||
$this->assertSame([UsersController::class, 'index'], $routes[0]->callback); | ||
|
||
$this->assertSame('/users/@id', $routes[1]->pattern); | ||
$this->assertSame('DELETE', $routes[1]->methods[0]); | ||
$this->assertSame([UsersController::class, 'destroy'], $routes[1]->callback); | ||
} | ||
|
||
public function testDefaultMethods(): void | ||
{ | ||
Flight::resource('/posts', PostsController::class); | ||
|
||
$routes = Flight::router()->getRoutes(); | ||
$this->assertCount(7, $routes); | ||
|
||
$this->assertSame('/posts', $routes[0]->pattern); | ||
$this->assertSame('GET', $routes[0]->methods[0]); | ||
$this->assertSame([PostsController::class, 'index'], $routes[0]->callback); | ||
$this->assertSame('posts.index', $routes[0]->alias); | ||
|
||
$this->assertSame('/posts/create', $routes[1]->pattern); | ||
$this->assertSame('GET', $routes[1]->methods[0]); | ||
$this->assertSame([PostsController::class, 'create'], $routes[1]->callback); | ||
$this->assertSame('posts.create', $routes[1]->alias); | ||
|
||
$this->assertSame('/posts', $routes[2]->pattern); | ||
$this->assertSame('POST', $routes[2]->methods[0]); | ||
$this->assertSame([PostsController::class, 'store'], $routes[2]->callback); | ||
$this->assertSame('posts.store', $routes[2]->alias); | ||
|
||
$this->assertSame('/posts/@id', $routes[3]->pattern); | ||
$this->assertSame('GET', $routes[3]->methods[0]); | ||
$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([PostsController::class, 'edit'], $routes[4]->callback); | ||
$this->assertSame('posts.edit', $routes[4]->alias); | ||
|
||
$this->assertSame('/posts/@id', $routes[5]->pattern); | ||
$this->assertSame('PUT', $routes[5]->methods[0]); | ||
$this->assertSame([PostsController::class, 'update'], $routes[5]->callback); | ||
$this->assertSame('posts.update', $routes[5]->alias); | ||
|
||
$this->assertSame('/posts/@id', $routes[6]->pattern); | ||
$this->assertSame('DELETE', $routes[6]->methods[0]); | ||
$this->assertSame([PostsController::class, 'destroy'], $routes[6]->callback); | ||
$this->assertSame('posts.destroy', $routes[6]->alias); | ||
} | ||
|
||
public function testOptionsExcept(): void | ||
{ | ||
Flight::resource('/todos', TodosController::class, [ | ||
'except' => [ 'create', 'store', 'update', 'destroy', 'edit' ] | ||
]); | ||
|
||
$routes = Flight::router()->getRoutes(); | ||
|
||
$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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\groupcompactsyntax; | ||
|
||
final class PostsController | ||
{ | ||
public function index(): void | ||
{ | ||
} | ||
|
||
public function show(string $id): void | ||
{ | ||
} | ||
|
||
public function create(): void | ||
{ | ||
} | ||
|
||
public function store(): void | ||
{ | ||
} | ||
|
||
public function edit(string $id): void | ||
{ | ||
} | ||
|
||
public function update(string $id): void | ||
{ | ||
} | ||
|
||
public function destroy(string $id): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\groupcompactsyntax; | ||
|
||
final class TodosController | ||
{ | ||
public function index(): void | ||
{ | ||
} | ||
|
||
public function show(string $id): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\groupcompactsyntax; | ||
|
||
final class UsersController | ||
{ | ||
public function index(): void | ||
{ | ||
echo __METHOD__; | ||
} | ||
|
||
public function destroy(): void | ||
{ | ||
echo __METHOD__; | ||
} | ||
} |