Skip to content

Commit

Permalink
Fix controller middleware not resolving (#199) (#203)
Browse files Browse the repository at this point in the history
* add more middleware tests

* fix AsJobSerializedTest

* add empty getMiddleware to trigger controller middleware

* Update composer.json

* Fix tests

* Try to downgrade pest

* Try to downgrade orchestra

* Update run-tests.yml

* Update composer.json

* Update run-tests.yml

* Update run-tests.yml

* Update composer.json
  • Loading branch information
lorisleiva authored Oct 20, 2022
1 parent 4090b93 commit 71324b7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ jobs:
include:
- laravel: "^8.67"
testbench: "^6.23"
testbench-core: "^6.27"
- laravel: "^9.0"
testbench: "^7.0"
testbench: "7.0"
testbench-core: "7.0"
exclude:
- php: "8.0"
laravel: "^8.67"
Expand Down Expand Up @@ -45,7 +47,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "orchestra/testbench-core:${{ matrix.testbench-core }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
],
"require": {
"php": "^8.0",
"illuminate/contracts": "^8.15|^9.0",
"illuminate/contracts": "^8.15 || 9.0 - 9.34 || ^9.36",
"lorisleiva/lody": "^0.3.0"
},
"require-dev": {
"orchestra/testbench": "^7.0",
"orchestra/testbench": "7.0.0",
"orchestra/testbench-core": "7.0.0",
"pestphp/pest": "^1.2",
"phpunit/phpunit": "^9.5"
},
Expand Down
10 changes: 10 additions & 0 deletions src/Concerns/AsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ public function __invoke(...$arguments)
{
return $this->handle(...$arguments);
}

/**
* This empty method is required to enable controller middleware on the action.
* @see https://github.com/lorisleiva/laravel-actions/issues/199
* @return void
*/
public function getMiddleware()
{
// ...
}
}
54 changes: 54 additions & 0 deletions tests/AsControllerWithMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ class AsControllerWithMiddlewareTest
{
use AsController;

public static int $middlewareCounter = 0;

public function getControllerMiddleware()
{
return [
function (Request $request, $next) {
static::$middlewareCounter++;

if ($request->get('operation') === 'middleware') {
return response()->json(['caught by middleware']);
}
Expand All @@ -30,6 +34,12 @@ public function handle(ActionRequest $request)
}
}

beforeEach(function () {
// Given we reset the static variables.
AsControllerWithMiddlewareTest::$middlewareCounter = 0;
RouteMiddleware::$counter = 0;
});

it('can register controller middleware', function () {
// Given the action is registered as a controller.
Route::post('/calculator', AsControllerWithMiddlewareTest::class);
Expand All @@ -42,3 +52,47 @@ public function handle(ActionRequest $request)
// Then we receive a successful response.
$response->assertOk()->assertExactJson(['caught by middleware']);
});


it('works with route middleware too', function () {
// Given the action is registered as a controller with a route middleware.
Route::post('/calculator', AsControllerWithMiddlewareTest::class)
->middleware(RouteMiddleware::class);

// When we call that route.
$response = $this->postJson('/calculator', [
'operation' => 'route',
]);

// Then we were intercepted by the route middleware.
$response->assertOk()->assertExactJson(['caught by route middleware']);
});

it('calls route and controller middleware exactly once', function () {
// Given the action is registered as a controller with a route middleware.
Route::post('/calculator', AsControllerWithMiddlewareTest::class)
->middleware(RouteMiddleware::class);

// When we call that route.
$response = $this->postJson('/calculator');

// The we were intercepted by both the route and the controller middleware exactly one.
expect(RouteMiddleware::$counter)->toBe(1);
expect(AsControllerWithMiddlewareTest::$middlewareCounter)->toBe(1);
});

class RouteMiddleware
{
public static int $counter = 0;

public function handle(Request $request, $next)
{
static::$counter++;

if ($request->get('operation') === 'route') {
return response()->json(['caught by route middleware']);
}

return $next($request);
}
}
24 changes: 10 additions & 14 deletions tests/AsJobSerializedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public function handle()

// Then the model parameter has been serialised into a ModelIdentifier.
$firstParameter = (array) data_get($serializedJob, 'parameters.0');
expect($firstParameter)->toBe([
'__PHP_Incomplete_Class_Name' => ModelIdentifier::class,
'class' => get_class($model),
'id' => $model->id,
'relations' => [],
'connection' => 'sqlite',
]);
expect($firstParameter['__PHP_Incomplete_Class_Name'])->toBe(ModelIdentifier::class)
->and($firstParameter['class'])->toBe(get_class($model))
->and($firstParameter['id'])->toBe($model->id)
->and($firstParameter['relations'])->toBe([])
->and($firstParameter['connection'])->toBe("sqlite");
});

it('unserialises Eloquent models within the parameters', function () {
Expand Down Expand Up @@ -93,13 +91,11 @@ public function handle()

// Then the collection parameter has been serialised into a ModelIdentifier.
$firstParameter = (array) data_get($serializedJob, 'parameters.0');
expect($firstParameter)->toBe([
'__PHP_Incomplete_Class_Name' => ModelIdentifier::class,
'class' => get_class($modelA),
'id' => $collection->pluck('id')->toArray(),
'relations' => [],
'connection' => 'sqlite',
]);
expect($firstParameter['__PHP_Incomplete_Class_Name'])->toBe(ModelIdentifier::class)
->and($firstParameter['class'])->toBe(get_class($modelA))
->and($firstParameter['id'])->toBe($collection->pluck('id')->toArray())
->and($firstParameter['relations'])->toBe([])
->and($firstParameter['connection'])->toBe("sqlite");
});

it('unserialises Eloquent collections within the parameters', function () {
Expand Down

0 comments on commit 71324b7

Please sign in to comment.