Skip to content

Commit d67c8a3

Browse files
committed
[Tests] Add custom action middleware test
See #90
1 parent afc7de6 commit d67c8a3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/lib/Integration/Routing/ActionsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
namespace LaravelJsonApi\Laravel\Tests\Integration\Routing;
2121

22+
use App\Http\Controllers\Api\V1\PostController;
2223
use Illuminate\Contracts\Routing\Registrar;
24+
use Illuminate\Support\Facades\Route;
2325
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
2426

2527
class ActionsTest extends TestCase
@@ -244,4 +246,29 @@ public function testIdConstraintWorks(string $method): void
244246

245247
$this->assertNotFound($method, '/api/v1/posts/123abc/-actions/foo-bar');
246248
}
249+
250+
/**
251+
* @see https://github.com/laravel-json-api/laravel/issues/90
252+
*/
253+
public function testWithMiddleware(): void
254+
{
255+
$server = $this->createServer('v1');
256+
$this->createSchema($server, 'posts', '\d+');
257+
258+
$this->defaultApiRoutes(function () {
259+
Route::name('api.')->middleware('my-middleware1')->group(function () {
260+
JsonApiRoute::server('v1')->prefix('v1')->resources(function ($server) {
261+
$server->resource('posts', PostController::class)
262+
->actions(function ($actions) {
263+
$actions->withId()->get('image');
264+
})->middleware('my-middleware2');
265+
});
266+
});
267+
});
268+
269+
$route = $this->assertMatch('GET', '/api/v1/posts/123/image');
270+
$this->assertSame("App\Http\Controllers\Api\V1\PostController@image", $route->action['controller']);
271+
$this->assertSame("api.v1.posts.image", $route->getName());
272+
$this->assertSame(['api', 'my-middleware1', 'jsonapi:v1', 'my-middleware2'], $route->action['middleware']);
273+
}
247274
}

0 commit comments

Comments
 (0)