Skip to content

Commit

Permalink
Added filter to remove extra dot in nested route names
Browse files Browse the repository at this point in the history
  • Loading branch information
ricventu committed Dec 27, 2024
1 parent 4ac33f4 commit c89e787
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/PendingRouteTransformers/AddDefaultRouteName.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function generateRouteName(PendingRouteAction $pendingRouteAction): st
{
return collect(explode('/', $pendingRouteAction->uri))
->reject(fn (string $segment) => str_starts_with($segment, '{'))
->filter()
->when(
$this->discoverMethodRouteName($pendingRouteAction),
fn (Collection $collection, $name) => $name != $collection->last() ? $collection->push($name) : $collection

Check failure on line 35 in src/PendingRouteTransformers/AddDefaultRouteName.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 ...$values of method Illuminate\Support\Collection<int,string>::push() expects non-falsy-string, string given.

Check failure on line 35 in src/PendingRouteTransformers/AddDefaultRouteName.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $callback of method Illuminate\Support\Collection<int,string>::when() expects (callable(Illuminate\Support\Collection<int, non-falsy-string>, string): Illuminate\Support\Collection<int, string>)|null, Closure(Illuminate\Support\Collection, mixed): Illuminate\Support\Collection<int, non-falsy-string> given.
Expand Down
41 changes: 38 additions & 3 deletions tests/RouteDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,70 @@
->routeRegistrar
->registerDirectory(controllersPath('NestedWithMultipleParametersController'));

$this->assertRegisteredRoutesCount(5);
$this->assertRegisteredRoutesCount(9);

$this->assertRouteRegistered(
MpPhotosController::class,
controllerMethod: 'show',
uri: 'photos/{photo}',
name: 'photos.show',
);

$this->assertRouteRegistered(
MpPhotosController::class,
controllerMethod: 'edit',
uri: 'photos/edit/{photo}',
name: 'photos.edit',
);

$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'index',
httpMethods: 'get',
uri: 'photos/{photo}/comments',
name: 'photos.comments',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'show',
httpMethods: 'get',
uri: 'photos/{photo}/comments/{comment}',
name: 'photos.comments.show',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'edit',
uri: 'photos/{photo}/comments/edit/{comment}',
controllerMethod: 'create',
httpMethods: 'get',
uri: 'photos/{photo}/comments/create',
name: 'photos.comments.create',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'store',
httpMethods: 'post',
uri: 'photos/{photo}/comments',
name: 'photos.comments.store',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'edit',
httpMethods: 'get',
uri: 'photos/{photo}/comments/edit/{comment}',
name: 'photos.comments.edit',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'update',
httpMethods: 'patch',
uri: 'photos/{photo}/comments/{comment}',
name: 'photos.comments.update',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'destroy',
httpMethods: ['delete'],
uri: 'photos/{photo}/comments/{comment}',
name: 'photos.comments.destroy',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithMultipleParametersController\Photos;

use Illuminate\Http\Request;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Comment;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Photo;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\User;

class CommentsController
{
public function index(Photo $photo)
{
}

public function show(Photo $photo, Comment $comment)
{
}

public function create(Photo $photo)
{
}

public function store(Photo $photo, Request $request)
{
}

public function edit(Photo $photo, Comment $comment)
{
}

public function store(Photo $photo, Comment $comment)
public function update(Photo $photo, Comment $comment, Request $request)
{
}

public function destroy(Photo $photo, Comment $comment)
{
}
}

0 comments on commit c89e787

Please sign in to comment.