Skip to content

Commit

Permalink
[0.x] Adds more tests (#25)
Browse files Browse the repository at this point in the history
* Adds more tests

* Tests around authorization logic
  • Loading branch information
nunomaduro authored Jul 11, 2023
1 parent 31c4bbe commit 51a0cfc
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 5 deletions.
14 changes: 10 additions & 4 deletions tests/Feature/Console/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Fixtures/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class Book extends Model
{
//
protected $guarded = [];
}
16 changes: 16 additions & 0 deletions tests/Feature/Fixtures/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Feature\Fixtures;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as BaseUser;

class User extends BaseUser
{
protected $guarded = [];

public function books(): HasMany
{
return $this->hasMany(Book::class);
}
}
20 changes: 20 additions & 0 deletions tests/Feature/ModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
55 changes: 55 additions & 0 deletions tests/Feature/ViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
use Laravel\Folio\Folio;
use Tests\Feature\Fixtures\Book;
use Tests\Feature\Fixtures\User;

it('may have blade php blocks', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->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);
});
17 changes: 17 additions & 0 deletions tests/Feature/resources/views/pages/books/index.blade.php
Original file line number Diff line number Diff line change
@@ -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)
<div>
{{ $book->title }}
</div>
@endforeach
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
<?php
use function Laravel\Folio\middleware;
middleware(function ($request, $next) {
$_SERVER['__folio_podcasts_inline_middleware'] = $request->route('podcast');
return $next($request);
});
?>

@php
$_SERVER['__folio_podcasts_php_blade_block'] = $podcast;
@endphp


<div>
{{ $podcast->name }}
</div>
8 changes: 8 additions & 0 deletions tests/Feature/resources/views/pages/users/nuno.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@php
$title = 'Nuno Maduro';
$renderedCount = $_SERVER['__folio_rendered_count'] = ($_SERVER['__folio_rendered_count'] ?? 0) + 1;
@endphp

<div>Hello, {{ $title }} from PHP block.</div>
<div>Rendered [{{ $renderedCount }}] time from PHP block.</div>

0 comments on commit 51a0cfc

Please sign in to comment.