Skip to content

Commit

Permalink
Fix middleware with route binding
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvalue committed Jun 8, 2023
1 parent c148b69 commit 90a0f47
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-latest",
"spatie/pest-plugin-test-time": "^1.1",
"roave/security-advisories": "dev-latest"
"spatie/ray": "^1.37"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/LaravelDraftsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oddvalue\LaravelDrafts;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -30,6 +31,12 @@ public function packageRegistered()
Schema::useNativeSchemaOperationsIfPossible();
}

$this->app->singleton(LaravelDrafts::class, function () {
return new LaravelDrafts();
});

$this->app[Kernel::class]->prependToMiddlewarePriority(WithDraftsMiddleware::class);

Blueprint::macro('drafts', function (
string $uuid = null,
string $publishedAt = null,
Expand Down
40 changes: 30 additions & 10 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use Illuminate\Support\Facades\Route;
use Oddvalue\LaravelDrafts\Http\Middleware\WithDraftsMiddleware;
use Oddvalue\LaravelDrafts\Tests\Post;
use function Pest\Laravel\get;

beforeEach(function () {
Post::create(['title' => 'Hello World']);
Post::createDraft(['title' => 'Hello World draft']);
test()->post = Post::create(['title' => 'Hello World']);
test()->draftPost = Post::createDraft(['title' => 'Hello World draft']);

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

Route::withDrafts(fn () => Route::get('/with-drafts-macro', function () {
return Post::all();
}));
Route::get('/with-drafts-middleware/{post}', function (Post $post) {
return $post;
})->middleware(WithDraftsMiddleware::class);

Route::withDrafts(function () {
Route::get('/with-drafts-macro', function () {
return Post::all();
});
Route::get('/with-drafts-macro/{post}', function (Post $post) {
return $post;
});
});
});
});

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

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

it('doesnt include drafts by default', function () {
$this->get('/default')->assertJsonCount(1);
get('/default')->assertJsonCount(1);
});

it('can use with draft middleware to include drafts on a model binding', function () {
get('/with-drafts-middleware/' . test()->draftPost->id)
->assertJsonFragment(['title' => 'Hello World draft']);
});

it('can use with draft macro to include drafts on a model binding', function () {
get('/with-drafts-macro/' . test()->draftPost->id)
->assertJsonFragment(['title' => 'Hello World draft']);
});

0 comments on commit 90a0f47

Please sign in to comment.