Skip to content

Commit 90a0f47

Browse files
committed
Fix middleware with route binding
1 parent c148b69 commit 90a0f47

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
"phpstan/phpstan-deprecation-rules": "^1.0",
3434
"phpstan/phpstan-phpunit": "^1.0",
3535
"phpunit/phpunit": "^9.5",
36+
"roave/security-advisories": "dev-latest",
3637
"spatie/pest-plugin-test-time": "^1.1",
37-
"roave/security-advisories": "dev-latest"
38+
"spatie/ray": "^1.37"
3839
},
3940
"autoload": {
4041
"psr-4": {

Diff for: src/LaravelDraftsServiceProvider.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oddvalue\LaravelDrafts;
44

5+
use Illuminate\Contracts\Http\Kernel;
56
use Illuminate\Database\Schema\Blueprint;
67
use Illuminate\Support\Facades\Route;
78
use Illuminate\Support\Facades\Schema;
@@ -30,6 +31,12 @@ public function packageRegistered()
3031
Schema::useNativeSchemaOperationsIfPossible();
3132
}
3233

34+
$this->app->singleton(LaravelDrafts::class, function () {
35+
return new LaravelDrafts();
36+
});
37+
38+
$this->app[Kernel::class]->prependToMiddlewarePriority(WithDraftsMiddleware::class);
39+
3340
Blueprint::macro('drafts', function (
3441
string $uuid = null,
3542
string $publishedAt = null,

Diff for: tests/MiddlewareTest.php

+30-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use Illuminate\Support\Facades\Route;
44
use Oddvalue\LaravelDrafts\Http\Middleware\WithDraftsMiddleware;
55
use Oddvalue\LaravelDrafts\Tests\Post;
6+
use function Pest\Laravel\get;
67

78
beforeEach(function () {
8-
Post::create(['title' => 'Hello World']);
9-
Post::createDraft(['title' => 'Hello World draft']);
9+
test()->post = Post::create(['title' => 'Hello World']);
10+
test()->draftPost = Post::createDraft(['title' => 'Hello World draft']);
1011

1112
Route::middleware(['web'])->group(function () {
1213
Route::get('/default', function () {
@@ -17,20 +18,39 @@
1718
return Post::all();
1819
})->middleware(WithDraftsMiddleware::class);
1920

20-
Route::withDrafts(fn () => Route::get('/with-drafts-macro', function () {
21-
return Post::all();
22-
}));
21+
Route::get('/with-drafts-middleware/{post}', function (Post $post) {
22+
return $post;
23+
})->middleware(WithDraftsMiddleware::class);
24+
25+
Route::withDrafts(function () {
26+
Route::get('/with-drafts-macro', function () {
27+
return Post::all();
28+
});
29+
Route::get('/with-drafts-macro/{post}', function (Post $post) {
30+
return $post;
31+
});
32+
});
2333
});
2434
});
2535

26-
it('can use with drsft middleware to include drafts on a route', function () {
27-
$this->get('/with-drafts-middleware')->assertJsonCount(2);
36+
it('can use with draft middleware to include drafts on a route', function () {
37+
get('/with-drafts-middleware')->assertJsonCount(2);
2838
});
2939

30-
it('can use with drsft macro to include drafts on a route', function () {
31-
$this->get('/with-drafts-macro')->assertJsonCount(2);
40+
it('can use with draft macro to include drafts on a route', function () {
41+
get('/with-drafts-macro')->assertJsonCount(2);
3242
});
3343

3444
it('doesnt include drafts by default', function () {
35-
$this->get('/default')->assertJsonCount(1);
45+
get('/default')->assertJsonCount(1);
46+
});
47+
48+
it('can use with draft middleware to include drafts on a model binding', function () {
49+
get('/with-drafts-middleware/' . test()->draftPost->id)
50+
->assertJsonFragment(['title' => 'Hello World draft']);
51+
});
52+
53+
it('can use with draft macro to include drafts on a model binding', function () {
54+
get('/with-drafts-macro/' . test()->draftPost->id)
55+
->assertJsonFragment(['title' => 'Hello World draft']);
3656
});

0 commit comments

Comments
 (0)