diff --git a/composer.json b/composer.json index 6adb8a4..07488a7 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/LaravelDraftsServiceProvider.php b/src/LaravelDraftsServiceProvider.php index 0364fde..b02301d 100644 --- a/src/LaravelDraftsServiceProvider.php +++ b/src/LaravelDraftsServiceProvider.php @@ -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; @@ -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, diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index ac03a29..71cf5b5 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -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 () { @@ -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']); });