diff --git a/tests/Feature/Console/ListCommandTest.php b/tests/Feature/Console/ListCommandTest.php index b3702e4..b5851d8 100644 --- a/tests/Feature/Console/ListCommandTest.php +++ b/tests/Feature/Console/ListCommandTest.php @@ -26,6 +26,7 @@ expect($exitCode)->toBe(0) ->and($output->fetch())->toBe(<<<'EOF' + GET /books ........................................................................................................... books/index.blade.php GET /books/{...book}/detail ........................................................ books/[...Tests.Feature.Fixtures.Book]/detail.blade.php GET /categories/{category} ......................................................... categories/[.Tests.Feature.Fixtures.Category].blade.php GET /deleted-podcasts/{podcast} ............................................... deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php @@ -35,9 +36,10 @@ GET /podcasts/{podcast} ............................................................... podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php GET /podcasts/{podcast}/comments ....................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/index.blade.php GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment:id].blad… + GET /users/nuno ....................................................................................................... users/nuno.blade.php GET /users/{id} ....................................................................................................... users/[id].blade.php - Showing [10] routes + Showing [12] routes EOF); @@ -53,7 +55,7 @@ expect($exitCode)->toBe(0) ->and($output->fetch())->toStartWith(<<<'EOF' - [{"method":"GET","uri":"\/books\/{...book}\/detail","view":"books\/[...Tests.Feature.Fixtures.Book]\/detail.blade.php"},{"method":"GET","uri":"\/categories\/{category}","view":"categ + [{"method":"GET","uri":"\/books","view":"books\/index.blade.php"},{"method":"GET","uri":"\/books\/{...book}\/detail EOF); }); @@ -91,13 +93,15 @@ expect($exitCode)->toBe(0) ->and($output->fetch())->toBe(<<<'EOF' + GET /books ........................................................................................................... books/index.blade.php GET /books/{...book}/detail ........................................................ books/[...Tests.Feature.Fixtures.Book]/detail.blade.php GET /categories/{category} ......................................................... categories/[.Tests.Feature.Fixtures.Category].blade.php GET /flights ....................................................................................................... flights/index.blade.php GET /non-routables/{nonRoutable} ............................................. non-routables/[.Tests.Feature.Fixtures.NonRoutable].blade.php + GET /users/nuno ....................................................................................................... users/nuno.blade.php GET /users/{id} ....................................................................................................... users/[id].blade.php - Showing [5] routes + Showing [7] routes EOF); @@ -180,6 +184,7 @@ expect($exitCode)->toBe(0) ->and($output->fetch())->toBe(<<<'EOF' + GET /books ....................................................................... tests/Feature/resources/views/pages/books/index.blade.php GET / ............................................................................. tests/Feature/resources/views/more-pages/index.blade.php GET /books/{...book}/detail .................... tests/Feature/resources/views/pages/books/[...Tests.Feature.Fixtures.Book]/detail.blade.php GET /categories/{category} ..................... tests/Feature/resources/views/pages/categories/[.Tests.Feature.Fixtures.Category].blade.php @@ -190,11 +195,12 @@ GET /podcasts/{podcast} ........................... tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php GET /podcasts/{podcast}/comments ... tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/index.blade.php GET /podcasts/{podcast}/comments/{comment:id} tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tes… + GET /users/nuno ................................................................... tests/Feature/resources/views/pages/users/nuno.blade.php GET /users/{id} ................................................................... tests/Feature/resources/views/pages/users/[id].blade.php GET /{...user} ................................................................ tests/Feature/resources/views/more-pages/[...User].blade.php GET /{...user}/detail .................................................. tests/Feature/resources/views/more-pages/[...User]/detail.blade.php - Showing [13] routes + Showing [15] routes EOF); diff --git a/tests/Feature/Fixtures/Book.php b/tests/Feature/Fixtures/Book.php index ccd909a..60aef20 100644 --- a/tests/Feature/Fixtures/Book.php +++ b/tests/Feature/Fixtures/Book.php @@ -6,5 +6,5 @@ class Book extends Model { - // + protected $guarded = []; } diff --git a/tests/Feature/Fixtures/User.php b/tests/Feature/Fixtures/User.php new file mode 100644 index 0000000..d0d1a43 --- /dev/null +++ b/tests/Feature/Fixtures/User.php @@ -0,0 +1,16 @@ +hasMany(Book::class); + } +} diff --git a/tests/Feature/ModelBindingTest.php b/tests/Feature/ModelBindingTest.php index f7792a0..4dd5bed 100644 --- a/tests/Feature/ModelBindingTest.php +++ b/tests/Feature/ModelBindingTest.php @@ -30,6 +30,26 @@ $this->get('/podcasts/'.$podcast->id)->assertSee('test-podcast-name'); }); +test('implicit model bindings are accessible via middleware', function () { + $podcast = Podcast::create([ + 'name' => 'test-podcast-name', + ]); + + $this->get('/podcasts/'.$podcast->id); + + expect($_SERVER['__folio_podcasts_inline_middleware'])->id->toBe($podcast->id); +}); + +test('implicit model bindings are accessible via @php tags', function () { + $podcast = Podcast::create([ + 'name' => 'test-podcast-name', + ]); + + $this->get('/podcasts/'.$podcast->id); + + expect($_SERVER['__folio_podcasts_php_blade_block'])->id->toBe($podcast->id); +}); + test('not found error is thrown if implicit binding can not be resolved', function () { $podcast = Podcast::create([ 'name' => 'test-podcast-name', diff --git a/tests/Feature/ViewTest.php b/tests/Feature/ViewTest.php new file mode 100644 index 0000000..a586f67 --- /dev/null +++ b/tests/Feature/ViewTest.php @@ -0,0 +1,55 @@ +get('/users/nuno'); + + $response->assertSee('Hello, Nuno Maduro from PHP block.'); +}); + +it('blade php blocks are only executed when rendering the view', function () { + $_SERVER['__folio_rendered_count'] = 0; + + Folio::route(__DIR__.'/resources/views/pages'); + + $this->get('/users/nuno'); + $response = $this->get('/users/nuno'); + + $response->assertSee('Rendered [2] time from PHP block.'); +}); + +it('may have blade php blocks with authorization logic', function () { + Folio::route(__DIR__.'/resources/views/pages'); + + Schema::create('users', function ($table) { + $table->id(); + $table->timestamps(); + }); + + Schema::create('books', function ($table) { + $table->id(); + $table->string('title'); + $table->foreignId('user_id'); + $table->timestamps(); + }); + + $user = User::create(); + + Book::create([ + 'title' => 'test-book-title', + 'user_id' => $user->id, + ]); + + Gate::define('view-books', fn () => true); + $this->actingAs($user)->get('/books')->assertStatus(200); + + Gate::define('view-books', fn () => false); + $this->actingAs($user)->get('/books')->assertStatus(403); +}); diff --git a/tests/Feature/resources/views/pages/books/index.blade.php b/tests/Feature/resources/views/pages/books/index.blade.php new file mode 100644 index 0000000..6be315c --- /dev/null +++ b/tests/Feature/resources/views/pages/books/index.blade.php @@ -0,0 +1,17 @@ +@php + use Illuminate\Support\Facades\Gate; + + if (! Gate::check('view-books')) { + abort(403); + } + + $user = auth()->user(); + + $books = $user->books; +@endphp + +@foreach ($books as $book) +