Skip to content

Commit

Permalink
added test for filter same last name part of uri
Browse files Browse the repository at this point in the history
  • Loading branch information
ricventu committed Dec 9, 2024
1 parent 8e076b0 commit 960fbfa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/PendingRouteTransformers/AddDefaultRouteName.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function generateRouteName(PendingRouteAction $pendingRouteAction): st
->reject(fn (string $segment) => str_starts_with($segment, '{'))
->when(
$this->discoverMethodRouteName($pendingRouteAction),
fn(Collection $collection, $name) => $name != $collection->last() ? $collection->push($name): $collection
fn(Collection $collection, $name) => $name != $collection->last() ? $collection->push($name) : $collection
)
->join('.');
}
Expand All @@ -46,6 +46,7 @@ protected function discoverMethodRouteName(PendingRouteAction $pendingRouteActio
return match ($pendingRouteAction->method->name) {
'show' => 'show',
'store' => 'store',
'edit' => 'edit',
'update' => 'update',
'destroy' => 'destroy',
'delete' => 'delete',
Expand Down
69 changes: 68 additions & 1 deletion tests/RouteDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\OverrideHttpMethod\OverrideHttpMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\OverrideUri\OverrideUriController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\ResourceMethods\ResourceMethodsController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\ResourceMethodsWithUri\ResourceMethodsWithUriController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Single\MyController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Where\WhereAttributeController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Middleware\OtherTestMiddleware;
Expand Down Expand Up @@ -147,7 +148,7 @@
);
});

it('will register routes with the correct http verbs for resourceful methods', function () {
it('will register routes with the correct http verbs and names for resourceful methods', function () {
$this
->routeRegistrar
->registerDirectory(controllersPath('ResourceMethods'));
Expand Down Expand Up @@ -205,6 +206,72 @@
);
});

it('will register routes with the correct names for resourceful methods without adding already existing name for uri', function () {
$this
->routeRegistrar
->registerDirectory(controllersPath('ResourceMethodsWithUri'));

$this
->assertRegisteredRoutesCount(8)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'index',
uri: 'resource-methods-with-uri',
httpMethods: ['get'],
name: 'resource-methods-with-uri',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'show',
uri: 'resource-methods-with-uri/show/{user}',
httpMethods: ['get'],
name: 'resource-methods-with-uri.show',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'create',
uri: 'resource-methods-with-uri/create',
httpMethods: ['get'],
name: 'resource-methods-with-uri.create',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'store',
uri: 'resource-methods-with-uri/store',
httpMethods: ['post'],
name: 'resource-methods-with-uri.store',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'edit',
uri: 'resource-methods-with-uri/edit/{user}',
httpMethods: ['get'],
name: 'resource-methods-with-uri.edit',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'update',
uri: 'resource-methods-with-uri/update/{user}',
httpMethods: ['put', 'patch'],
name: 'resource-methods-with-uri.update',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'destroy',
uri: 'resource-methods-with-uri/destroy/{user}',
httpMethods: ['delete'],
name: 'resource-methods-with-uri.destroy',
)
->assertRouteRegistered(
ResourceMethodsWithUriController::class,
controllerMethod: 'delete',
uri: 'resource-methods-with-uri/delete/{user}',
httpMethods: ['delete'],
name: 'resource-methods-with-uri.delete',
);
});


it('can override the http method', function () {
$this
->routeRegistrar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\ResourceMethodsWithUri;

use Illuminate\Http\Request;
use Spatie\RouteDiscovery\Attributes\Route;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\User;

class ResourceMethodsWithUriController
{
public function index()
{
}

#[Route(uri: 'show/{user}')]
public function show(User $user)
{
}

#[Route(uri: 'create')]
public function create()
{
}

#[Route(uri: 'store')]
public function store(Request $request)
{
}

#[Route(uri: '{user}')]
public function edit(User $user)
{
}

#[Route(uri: 'update/{user}')]
public function update(User $user)
{
}

#[Route(uri: 'delete/{user}')]
public function delete(User $user)
{
}

#[Route(uri: 'destroy/{user}')]
public function destroy(User $user)
{
}
}

0 comments on commit 960fbfa

Please sign in to comment.