Skip to content

Commit

Permalink
Separate chain calls (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed Jun 17, 2022
1 parent fe05ff9 commit 62af22f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 deletions.
70 changes: 38 additions & 32 deletions tests/GroupTest.php
Expand Up @@ -315,14 +315,16 @@ public function testDispatcherInjected(): void

public function testWithCors(): void
{
$group = Group::create()->routes(
Route::get('/info')->action(static fn () => 'info'),
Route::post('/info')->action(static fn () => 'info'),
)->withCors(
static function () {
return new Response(204);
}
);
$group = Group::create()
->routes(
Route::get('/info')->action(static fn () => 'info'),
Route::post('/info')->action(static fn () => 'info'),
)
->withCors(
static function () {
return new Response(204);
}
);

$collector = new RouteCollector();
$collector->addGroup($group);
Expand All @@ -333,21 +335,23 @@ static function () {

public function testWithCorsDoesntDuplicateRoutes(): void
{
$group = Group::create()->routes(
Route::get('/info')
->action(static fn () => 'info')
->host('yii.dev'),
Route::post('/info')
->action(static fn () => 'info')
->host('yii.dev'),
Route::put('/info')
->action(static fn () => 'info')
->host('yii.test'),
)->withCors(
static function () {
return new Response(204);
}
);
$group = Group::create()
->routes(
Route::get('/info')
->action(static fn () => 'info')
->host('yii.dev'),
Route::post('/info')
->action(static fn () => 'info')
->host('yii.dev'),
Route::put('/info')
->action(static fn () => 'info')
->host('yii.test'),
)
->withCors(
static function () {
return new Response(204);
}
);

$collector = new RouteCollector();
$collector->addGroup($group);
Expand All @@ -361,15 +365,17 @@ public function testWithCorsWithNestedGroups(): void
$group = Group::create()->routes(
Route::get('/info')->action(static fn () => 'info'),
Route::post('/info')->action(static fn () => 'info'),
Group::create('/v1')->routes(
Route::get('/post')->action(static fn () => 'post'),
Route::post('/post')->action(static fn () => 'post'),
Route::options('/options')->action(static fn () => 'options'),
)->withCors(
static function () {
return new Response(201);
}
)
Group::create('/v1')
->routes(
Route::get('/post')->action(static fn () => 'post'),
Route::post('/post')->action(static fn () => 'post'),
Route::options('/options')->action(static fn () => 'options'),
)
->withCors(
static function () {
return new Response(201);
}
)
)->withCors(
static function () {
return new Response(204);
Expand Down
12 changes: 8 additions & 4 deletions tests/RouteCollectionTest.php
Expand Up @@ -124,19 +124,22 @@ public function testGetRouterTree(): void
->routes(
Route::get('/posts', $this->getDispatcher())->name('/posts'),
Route::get('/post/{sile}')->name('/post/view')
)->namePrefix('/v1'),
)
->namePrefix('/v1'),
Group::create('/v1')
->routes(
Route::get('/tags', $this->getDispatcher())->name('/tags'),
Route::get('/tag/{slug}')->name('/tag/view'),
)->namePrefix('/v1'),
)
->namePrefix('/v1'),
)->namePrefix('/api');

$group2 = Group::create('/api')
->routes(
Route::get('/posts', $this->getDispatcher())->name('/posts'),
Route::get('/post/{sile}')->name('/post/view'),
)->namePrefix('/api');
)
->namePrefix('/api');

$collector = new RouteCollector();
$collector->addGroup($group1);
Expand Down Expand Up @@ -192,7 +195,8 @@ public function testGroupHost(): void
)
->host('https://yiipowered.com/'),
Route::get('/images/{name}')->name('image')
)->host('https://yiiframework.com/');
)
->host('https://yiiframework.com/');

$collector = new RouteCollector();
$collector->addGroup($group);
Expand Down
24 changes: 13 additions & 11 deletions tests/RouteTest.php
Expand Up @@ -258,17 +258,19 @@ public function testGetDispatcherWithMiddlewares(): void
{
$request = new ServerRequest('GET', '/');

$injectDispatcher = $this->getDispatcher(
$this->getContainer([
TestMiddleware1::class => new TestMiddleware1(),
TestMiddleware2::class => new TestMiddleware2(),
TestController::class => new TestController(),
])
)->withMiddlewares([
TestMiddleware1::class,
TestMiddleware2::class,
[TestController::class, 'index'],
]);
$injectDispatcher = $this
->getDispatcher(
$this->getContainer([
TestMiddleware1::class => new TestMiddleware1(),
TestMiddleware2::class => new TestMiddleware2(),
TestController::class => new TestController(),
])
)
->withMiddlewares([
TestMiddleware1::class,
TestMiddleware2::class,
[TestController::class, 'index'],
]);

$route = Route::get('/');
$route->injectDispatcher($injectDispatcher);
Expand Down

0 comments on commit 62af22f

Please sign in to comment.