Skip to content

Commit

Permalink
[1.x] Fixes Wildcard Directories Modifying State for Literal Views in…
Browse files Browse the repository at this point in the history
… the Base Folder (#115)

* Fixes non-wildcard directories modifying state

* Fix code styling

* Improves testing

* Fix code styling

* Removes non needed test line

---------

Co-authored-by: nunomaduro <[email protected]>
  • Loading branch information
nunomaduro and nunomaduro authored Oct 9, 2023
1 parent 57b46da commit bba13fb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Pipeline/MatchWildcardDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MatchWildcardDirectories
public function __invoke(State $state, Closure $next): mixed
{
if ($directory = $this->findWildcardDirectory($state->currentDirectory())) {
$state = $state->withData(
$currentState = $state->withData(
Str::of($directory)
->basename()
->match('/\[(.*)\]/')->value(),
Expand All @@ -23,12 +23,12 @@ public function __invoke(State $state, Closure $next): mixed
Str::of($directory)->basename()
);

if (! $state->onLastUriSegment()) {
return new ContinueIterating($state);
if (! $currentState->onLastUriSegment()) {
return new ContinueIterating($currentState);
}

if (file_exists($path = $state->currentUriSegmentDirectory().'/index.blade.php')) {
return new MatchedView($path, $state->data);
if (file_exists($path = $currentState->currentUriSegmentDirectory().'/index.blade.php')) {
return new MatchedView($path, $currentState->data);
}
}

Expand Down
15 changes: 10 additions & 5 deletions tests/Feature/Console/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
GET /podcasts/list ................................................................................................. podcasts/list.blade.php
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/3 ......................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/3.blade.php
GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment-id].blad…
GET /posts/{lowerCase}/{upperCase}/{podcast}/{user:email}/show posts.show › posts/[lowerCase]/[UpperCase]/[Podcast]/[User-email]/show.bla…
GET /users/articles/{user:wrongColumn} ........................................ user.articles › users/articles/[User-wrong_column].blade.php
GET /users/movies/{user}/{movie} ............... users/movies/[.Tests.Feature.Fixtures.User]/[.Tests.Feature.Fixtures.Movie]/index.blade.php
GET /users/nuno .......................................................................................... users.nuno › users/nuno.blade.php
GET /users/{id} .......................................................................................... users.show › users/[id].blade.php
Showing [18] routes
Showing [19] routes


EOF);
Expand Down Expand Up @@ -80,9 +81,10 @@
GET /podcasts/list ................................................................................................. podcasts/list.blade.php
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/3 ......................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/3.blade.php
GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment-id].blad…
Showing [5] routes
Showing [6] routes


EOF);
Expand Down Expand Up @@ -171,11 +173,12 @@
GET /deleted-podcasts/{podcast} ............................................... deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php
GET /podcasts/{podcast} ............................................................... podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php
GET /podcasts/{podcast}/comments/3 ......................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/3.blade.php
GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment-id].blad…
GET /podcasts/{podcast}/comments ....................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/index.blade.php
GET /podcasts/list ................................................................................................. podcasts/list.blade.php
Showing [5] routes
Showing [6] routes


EOF);
Expand All @@ -194,12 +197,13 @@
->and($output->fetch())->toOutput(<<<'EOF'
GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment-id].blad…
GET /podcasts/{podcast}/comments/3 ......................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/3.blade.php
GET /podcasts/{podcast}/comments ....................................... podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/index.blade.php
GET /podcasts/{podcast} ............................................................... podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php
GET /podcasts/list ................................................................................................. podcasts/list.blade.php
GET /deleted-podcasts/{podcast} ............................................... deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php
Showing [5] routes
Showing [6] routes


EOF);
Expand Down Expand Up @@ -229,6 +233,7 @@
GET /podcasts/list ............................................................. tests/Feature/resources/views/pages/podcasts/list.blade.php
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/3 ..... tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/3.blade.php
GET /podcasts/{podcast}/comments/{comment:id} tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tes…
GET /posts/{lowerCase}/{upperCase}/{podcast}/{user:email}/show posts.show › tests/Feature/resources/views/pages/posts/[lowerCase]/[UpperC…
GET /users/articles/{user:wrongColumn} .... user.articles › tests/Feature/resources/views/pages/users/articles/[User-wrong_column].blade.php
Expand All @@ -238,7 +243,7 @@
GET /{...user} ................................................................ tests/Feature/resources/views/more-pages/[...User].blade.php
GET /{...user}/detail ......................... more-pages.user.detail › tests/Feature/resources/views/more-pages/[...User]/detail.blade.php
Showing [21] routes
Showing [22] routes


EOF);
Expand Down
41 changes: 41 additions & 0 deletions tests/Feature/ModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,47 @@
$this->get('/podcasts/missing-id')->assertNotFound();
});

test('regular routes may be used if implicit binding can not be resolved', function () {
$this->get('/podcasts/list')
->assertStatus(200)
->assertSee('podcast');

$podcast = Podcast::create([
'name' => 'test-podcast-name',
]);

$this->get('/podcasts/'.$podcast->id.'/comments/1')
->assertNotFound();

$podcast->comments()->create([
'content' => 'test-comment-content-1',
])->fresh();

$this->get('/podcasts/'.$podcast->id.'/comments/1')
->assertStatus(200)
->assertSee('test-comment-content-1');

$podcast->comments()->create([
'content' => 'test-comment-content-2',
])->fresh();

$this->get('/podcasts/'.$podcast->id.'/comments/2')
->assertStatus(200)
->assertSee('test-comment-content-2');

$this->get('/podcasts/'.$podcast->id.'/comments/3')
->assertStatus(200)
->assertSee('literal-comment');

$podcast->comments()->create([
'content' => 'test-comment-content-3',
])->fresh();

$this->get('/podcasts/'.$podcast->id.'/comments/3')
->assertStatus(200)
->assertSee('literal-comment');
});

test('child implicit bindings are resolved', function () {
$user = User::create([
'name' => 'test-name',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'literal-comment' }}

0 comments on commit bba13fb

Please sign in to comment.