/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Routing/RouteRegistrarTest.php
1 822 строки
69 KB
Jason McCreary
[13.x] Mockery cleanup (#61117)
10 авг 2026, 20:23
Не верифицирован
10 авг 2026, 20:23
2ee40ee
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Routing; use BadMethodCallException; use FooController; use Illuminate\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Http\Request; use Illuminate\Routing\Router; use Illuminate\Routing\RouteRegistrar; use InvalidArgumentException; use Mockery; use PHPUnit\Framework\TestCase; use Stringable; include_once 'Enums.php'; class RouteRegistrarTest extends TestCase { /** * @var \Illuminate\Routing\Router */ protected $router; protected function setUp(): void { $this->router = new Router(Mockery::mock(Dispatcher::class), Container::getInstance()); } public function testMiddlewareFluentRegistration() { $this->router->middleware(['one', 'two'])->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['one', 'two'], $this->getRoute()->middleware()); $this->router->middleware('three', 'four')->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['three', 'four'], $this->getRoute()->middleware()); $this->router->get('users', function () { return 'all-users'; })->middleware('five', 'six'); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['five', 'six'], $this->getRoute()->middleware()); $this->router->middleware('seven')->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['seven'], $this->getRoute()->middleware()); } public function testNullNamespaceIsRespected() { $this->router->middleware(['one'])->namespace(null)->get('users', function () { return 'all-users'; }); $this->assertNull($this->getRoute()->getAction()['namespace']); } public function testMiddlewareAsStringableObject() { $one = new class implements Stringable { public function __toString() { return 'one'; } }; $this->router->middleware($one)->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame(['one'], $this->getRoute()->middleware()); } public function testMiddlewareAsStringableObjectOnRouteInstance() { $one = new class implements Stringable { public function __toString() { return 'one'; } }; $this->router->get('users', function () { return 'all-users'; })->middleware($one); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame(['one'], $this->getRoute()->middleware()); } public function testMiddlewareAsArrayWithStringables() { $one = new class implements Stringable { public function __toString() { return 'one'; } }; $this->router->middleware([$one, 'two'])->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame(['one', 'two'], $this->getRoute()->middleware()); } public function testMiddlewareAsNull() { $this->router->middleware(null)->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame([], $this->getRoute()->middleware()); $this->router->get('users', function () { return 'all-users'; })->middleware(null); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame([], $this->getRoute()->middleware()); } public function testWithoutMiddlewareRegistration() { $this->router->middleware(['one', 'two'])->get('users', function () { return 'all-users'; })->withoutMiddleware('one'); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); } public function testGetRouteWithTrashed() { $route = $this->router->get('users', [RouteRegistrarControllerStub::class, 'index'])->withTrashed(); $this->assertTrue($route->allowsTrashedBindings()); } public function testResourceWithTrashed() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->only(['index', 'destroy']) ->withTrashed([ 'index', 'destroy', ]); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertTrue($route->allowsTrashedBindings()); } } public function testFallbackRoute() { $route = $this->router->fallback(function () { return 'milwad'; }); $this->assertTrue($route->isFallback); } public function testSetFallbackRoute() { $route = $this->router->fallback(function () { return 'milwad'; }); $route->setFallback(false); $this->assertFalse($route->isFallback); $route->setFallback(true); $this->assertTrue($route->isFallback); } public function testCanRegisterGetRouteWithClosureAction() { $this->router->middleware('get-middleware')->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->seeMiddleware('get-middleware'); } public function testCanRegisterPostRouteWithClosureAction() { $this->router->middleware('post-middleware')->post('users', function () { return 'saved'; }); $this->seeResponse('saved', Request::create('users', 'POST')); $this->seeMiddleware('post-middleware'); } public function testCanRegisterAnyRouteWithClosureAction() { $this->router->middleware('test-middleware')->any('users', function () { return 'anything'; }); $this->seeResponse('anything', Request::create('users', 'PUT')); $this->seeMiddleware('test-middleware'); } public function testCanRegisterMatchRouteWithClosureAction() { $this->router->middleware('match-middleware')->match(['DELETE'], 'users', function () { return 'deleted'; }); $this->seeResponse('deleted', Request::create('users', 'DELETE')); $this->seeMiddleware('match-middleware'); } public function testCanRegisterRouteWithArrayAndClosureAction() { $this->router->middleware('patch-middleware')->patch('users', [function () { return 'updated'; }]); $this->seeResponse('updated', Request::create('users', 'PATCH')); $this->seeMiddleware('patch-middleware'); } public function testCanRegisterRouteWithArrayAndClosureUsesAction() { $this->router->middleware('put-middleware')->put('users', ['uses' => function () { return 'replaced'; }]); $this->seeResponse('replaced', Request::create('users', 'PUT')); $this->seeMiddleware('put-middleware'); } public function testCanRegisterRouteWithControllerAction() { $this->router->middleware('controller-middleware') ->get('users', RouteRegistrarControllerStub::class.'@index'); $this->seeResponse('controller', Request::create('users', 'GET')); $this->seeMiddleware('controller-middleware'); } public function testCanRegisterRouteWithControllerActionArray() { $this->router->middleware('controller-middleware') ->get('users', [RouteRegistrarControllerStub::class, 'index']); $this->seeResponse('controller', Request::create('users', 'GET')); $this->seeMiddleware('controller-middleware'); } public function testCanRegisterNamespacedGroupRouteWithControllerActionArray() { $this->router->group(['namespace' => 'WhatEver'], function () { $this->router->middleware('controller-middleware') ->get('users', [RouteRegistrarControllerStub::class, 'index']); }); $this->seeResponse('controller', Request::create('users', 'GET')); $this->seeMiddleware('controller-middleware'); $this->router->group(['namespace' => 'WhatEver'], function () { $this->router->middleware('controller-middleware') ->get('users', ['\\'.RouteRegistrarControllerStub::class, 'index']); }); $this->seeResponse('controller', Request::create('users', 'GET')); $this->seeMiddleware('controller-middleware'); } public function testCanRegisterRouteWithArrayAndControllerAction() { $this->router->middleware('controller-middleware')->put('users', [ 'uses' => RouteRegistrarControllerStub::class.'@index', ]); $this->seeResponse('controller', Request::create('users', 'PUT')); $this->seeMiddleware('controller-middleware'); } public function testCanRegisterGroupWithMiddleware() { $this->router->middleware('group-middleware')->group(function ($router) { $router->get('users', function () { return 'all-users'; }); }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->seeMiddleware('group-middleware'); } public function testCanRegisterGroupWithoutMiddleware() { $this->router->withoutMiddleware('one')->group(function ($router) { $router->get('users', function () { return 'all-users'; })->middleware(['one', 'two']); }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); } public function testCanRegisterGroupWithStringableMiddleware() { $one = new class implements Stringable { public function __toString() { return 'one'; } }; $this->router->middleware($one)->group(function ($router) { $router->get('users', function () { return 'all-users'; }); }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->seeMiddleware('one'); } public function testCanRegisterGroupWithNamespace() { $this->router->namespace('App\Http\Controllers')->group(function ($router) { $router->get('users', 'UsersController@index'); }); $this->assertSame( 'App\Http\Controllers\UsersController@index', $this->getRoute()->getAction()['uses'] ); $this->router->namespace('App\Http\Controllers')->group(function ($router) { $router->redirect('users', '/'); }); $this->assertSame( '\Illuminate\Routing\RedirectController@__invoke', $this->getRoute()->getAction()['uses'] ); } public function testCanRegisterGroupWithPrefix() { $this->router->prefix('api')->group(function ($router) { $router->get('users', 'UsersController@index'); }); $this->assertSame('api/users', $this->getRoute()->uri()); } public function testCanRegisterGroupWithPrefixAndWhere() { $this->router->prefix('foo/{bar}')->where(['bar' => '[0-9]+'])->group(function ($router) { $router->get('here', function () { return 'good'; }); }); $this->seeResponse('good', Request::create('foo/12345/here', 'GET')); } public function testCanRegisterGroupWithNamePrefix() { $this->router->name('api.')->group(function ($router) { $router->get('users', 'UsersController@index')->name('users'); }); $this->assertSame('api.users', $this->getRoute()->getName()); } public function testCanRegisterGroupWithDomain() { $this->router->domain('{account}.myapp.com')->group(function ($router) { $router->get('users', 'UsersController@index'); }); $this->assertSame('{account}.myapp.com', $this->getRoute()->getDomain()); } public function testCanRegisterGroupWithDomainAndNamePrefix() { $this->router->domain('{account}.myapp.com')->name('api.')->group(function ($router) { $router->get('users', 'UsersController@index')->name('users'); }); $this->assertSame('{account}.myapp.com', $this->getRoute()->getDomain()); $this->assertSame('api.users', $this->getRoute()->getName()); } public function testCanRegisterGroupWithController() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->get('users', 'index'); }); $this->assertSame( RouteRegistrarControllerStub::class.'@index', $this->getRoute()->getAction()['uses'] ); } public function testCanOverrideGroupControllerWithStringSyntax() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->get('users', 'UserController@index'); }); $this->assertSame( 'UserController@index', $this->getRoute()->getAction()['uses'] ); } public function testCanOverrideGroupControllerWithClosureSyntax() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->get('users', function () { return 'hello world'; }); }); $this->seeResponse('hello world', Request::create('users', 'GET')); } public function testCanOverrideGroupControllerWithInvokableControllerSyntax() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->get('users', InvokableRouteRegistrarControllerStub::class); }); $this->assertSame( InvokableRouteRegistrarControllerStub::class.'@__invoke', $this->getRoute()->getAction()['uses'] ); } public function testWillUseTheLatestGroupController() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->group(['controller' => FooController::class], function ($router) { $router->get('users', 'index'); }); }); $this->assertSame( FooController::class.'@index', $this->getRoute()->getAction()['uses'] ); } public function testCanOverrideGroupControllerWithArraySyntax() { $this->router->controller(RouteRegistrarControllerStub::class)->group(function ($router) { $router->get('users', [FooController::class, 'index']); }); $this->assertSame( FooController::class.'@index', $this->getRoute()->getAction()['uses'] ); } public function testRouteGroupingWithoutPrefix() { $this->router->group([], function ($router) { $router->prefix('bar')->get('baz', ['as' => 'baz', function () { return 'hello'; }]); }); $this->seeResponse('hello', Request::create('bar/baz', 'GET')); } public function testRouteGroupChaining() { $this->router ->group([], function ($router) { $router->get('foo', function () { return 'hello'; }); }) ->group([], function ($router) { $router->get('bar', function () { return 'goodbye'; }); }); $routeCollection = $this->router->getRoutes(); $this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('foo', 'GET'))); $this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('bar', 'GET'))); } public function testRegisteringNonApprovedAttributesThrows() { $this->expectExceptionObject(new BadMethodCallException('Method Illuminate\Routing\RouteRegistrar::unsupportedMethod does not exist.')); $this->router->domain('foo')->unsupportedMethod('bar')->group(function ($router) { // }); } public function testCanSetWithoutScopedBindings() { $route = $this->router->withoutScopedBindings()->get('users', function () { return 'all-users'; }); $this->assertTrue($route->preventsScopedBindings()); } public function testCanSetWithoutScopedBindingsOnGroup() { $this->router->withoutScopedBindings()->group(function ($router) { $router->get('foo', function () { return 'hello'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertTrue($route->preventsScopedBindings()); } public function testCanSetScopeBindings() { $route = $this->router->scopeBindings()->get('users', function () { return 'all-users'; }); $this->assertTrue($route->enforcesScopedBindings()); } public function testCanSetScopeBindingsOnGroup() { $this->router->scopeBindings()->group(function ($router) { $router->get('foo', function () { return 'hello'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertTrue($route->enforcesScopedBindings()); } public function testCanSetRouteMetadata() { $route = $this->router ->metadata(['head' => ['title' => 'Users']]) ->get('users', function () { return 'all-users'; }) ->metadata(['head' => ['description' => 'All users.']]); $this->assertSame([ 'title' => 'Users', 'description' => 'All users.', ], $route->getMetadata('head')); $this->assertSame('Users', $route->getMetadata('head.title')); } public function testCanSetRouteMetadataOnGroup() { $this->router ->metadata(['head' => ['robots' => ['noindex', 'nofollow']]]) ->group(function ($router) { $router ->metadata(['head' => ['title' => 'Users']]) ->get('users', function () { return 'all-users'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertSame([ 'robots' => ['noindex', 'nofollow'], 'title' => 'Users', ], $route->getMetadata('head')); } public function testRouteMetadataListValuesReplaceParentValues() { $this->router ->metadata(['head' => ['robots' => ['index', 'follow']]]) ->group(function ($router) { $router ->metadata(['head' => ['robots' => ['noindex']]]) ->get('users', function () { return 'all-users'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertSame(['noindex'], $route->getMetadata('head.robots')); } public function testCanSetRouteMetadataOnGroupUsingArraySyntax() { $this->router->group(['metadata' => ['head' => ['title' => 'Users']]], function ($router) { $router->get('users', function () { return 'all-users'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertSame(['title' => 'Users'], $route->getMetadata('head')); } public function testEmptyRouteMetadataArrayReplacesParentValue() { $this->router ->metadata(['head' => ['title' => 'Users']]) ->group(function ($router) { $router ->metadata(['head' => []]) ->get('users', function () { return 'all-users'; }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertSame([], $route->getMetadata('head')); } public function testRouteMetadataAttributeRequiresArray() { $this->expectExceptionObject(new InvalidArgumentException('Attribute [metadata] expects an array.')); (new RouteRegistrar($this->router))->attribute('metadata', 'invalid'); } public function testRouteMetadataDoesNotCollideWithRouteActions() { $route = $this->router ->middleware('web') ->metadata(['middleware' => 'metadata']) ->get('users', function () { return 'all-users'; }); $this->assertSame('metadata', $route->getMetadata('middleware')); $this->assertSame(['web'], $route->getAction('middleware')); } public function testRouteMetadataMergesThroughDeeplyNestedGroups() { $this->router ->metadata(['head' => ['title' => 'Outer', 'description' => 'Outer description']]) ->group(function ($router) { $router ->metadata(['head' => ['title' => 'Middle', 'author' => 'Taylor']]) ->group(function ($router) { $router ->metadata(['head' => ['title' => 'Inner']]) ->get('users', function () { return 'all-users'; }); }); }); $route = $this->router->getRoutes()->getRoutes()[0]; $this->assertSame([ 'title' => 'Inner', 'description' => 'Outer description', 'author' => 'Taylor', ], $route->getMetadata('head')); } public function testSetMetadataReplacesExistingMetadata() { $route = $this->router ->metadata(['head' => ['title' => 'Original', 'description' => 'Goes away']]) ->get('users', function () { return 'all-users'; }); $route->setMetadata(['head' => ['title' => 'Replaced']]); $this->assertSame(['head' => ['title' => 'Replaced']], $route->getMetadata()); } public function testCanRegisterResource() { $this->router->middleware('resource-middleware') ->resource('users', RouteRegistrarControllerStub::class); $this->seeResponse('deleted', Request::create('users/1', 'DELETE')); $this->seeMiddleware('resource-middleware'); } public function testCanSetRouteMetadataOnResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->metadata(['head' => ['title' => 'Users']]); $this->assertSame( ['title' => 'Users'], $this->router->getRoutes()->getByName('users.index')->getMetadata('head') ); } public function testCanSetRouteMetadataOnResourceGroup() { $this->router ->metadata(['head' => ['title' => 'Users']]) ->resource('users', RouteRegistrarControllerStub::class); $this->assertSame( ['title' => 'Users'], $this->router->getRoutes()->getByName('users.index')->getMetadata('head') ); } public function testCanSetRouteMetadataOnApiResource() { $this->router->apiResource('users', RouteRegistrarControllerStub::class) ->metadata(['head' => ['title' => 'Users']]); $this->assertSame( ['title' => 'Users'], $this->router->getRoutes()->getByName('users.index')->getMetadata('head') ); } public function testCanRegisterResourcesWithExceptOption() { $this->router->resources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ], ['except' => ['create', 'show']]); $this->assertCount(15, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); } } public function testCanRegisterResourcesWithOnlyOption() { $this->router->resources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ], ['only' => ['create', 'show']]); $this->assertCount(6, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); } } public function testCanRegisterResourcesWithoutOption() { $this->router->resources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ]); $this->assertCount(21, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); } } public function testCanRegisterResourceWithMissingOption() { $this->router->middleware('resource-middleware') ->resource('users', RouteRegistrarControllerStub::class) ->missing(function () { return 'missing'; }); $this->assertIsCallable($this->router->getRoutes()->getByName('users.show')->getMissing()); $this->assertIsCallable($this->router->getRoutes()->getByName('users.edit')->getMissing()); $this->assertIsCallable($this->router->getRoutes()->getByName('users.update')->getMissing()); $this->assertIsCallable($this->router->getRoutes()->getByName('users.destroy')->getMissing()); $this->assertNull($this->router->getRoutes()->getByName('users.index')->getMissing()); $this->assertNull($this->router->getRoutes()->getByName('users.create')->getMissing()); $this->assertNull($this->router->getRoutes()->getByName('users.store')->getMissing()); } public function testCanAccessRegisteredResourceRoutesAsRouteCollection() { $resource = $this->router->middleware('resource-middleware') ->resource('users', RouteRegistrarControllerStub::class) ->register(); $this->assertCount(7, $resource->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testCanLimitMethodsOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->only('index', 'show', 'destroy'); $this->assertCount(3, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testCanExcludeMethodsOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->except(['index', 'create', 'store', 'show', 'edit']); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testCanLimitAndExcludeMethodsOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->only('index', 'show', 'destroy') ->except('destroy'); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testCanSetShallowOptionOnRegisteredResource() { $this->router->resource('users.tasks', RouteRegistrarControllerStub::class)->shallow(); $this->assertCount(7, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.tasks.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('tasks.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.tasks.show')); } public function testCanSetScopedOptionOnRegisteredResource() { $this->router->resource('users.tasks', RouteRegistrarControllerStub::class)->scoped(); $this->router->getRoutes()->refreshNameLookups(); $this->assertSame( ['user' => null], $this->router->getRoutes()->getByName('users.tasks.index')->bindingFields() ); $this->assertSame( ['user' => null, 'task' => null], $this->router->getRoutes()->getByName('users.tasks.show')->bindingFields() ); $this->router->resource('users.tasks', RouteRegistrarControllerStub::class)->scoped([ 'task' => 'slug', ]); $this->router->getRoutes()->refreshNameLookups(); $this->assertSame( ['user' => null], $this->router->getRoutes()->getByName('users.tasks.index')->bindingFields() ); $this->assertSame( ['user' => null, 'task' => 'slug'], $this->router->getRoutes()->getByName('users.tasks.show')->bindingFields() ); } public function testCanExcludeMethodsOnRegisteredApiResource() { $this->router->apiResource('users', RouteRegistrarControllerStub::class) ->except(['index', 'show', 'store']); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testCanRegisterApiResourcesWithExceptOption() { $this->router->apiResources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ], ['except' => ['create', 'show']]); $this->assertCount(12, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); } } public function testCanRegisterApiResourcesWithOnlyOption() { $this->router->apiResources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ], ['only' => ['index', 'show']]); $this->assertCount(6, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); } } public function testCanRegisterApiResourcesWithoutOption() { $this->router->apiResources([ 'resource-one' => RouteRegistrarControllerStubOne::class, 'resource-two' => RouteRegistrarControllerStubTwo::class, 'resource-three' => RouteRegistrarControllerStubThree::class, ]); $this->assertCount(15, $this->router->getRoutes()); foreach (['one', 'two', 'three'] as $resource) { $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.destroy')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('resource-'.$resource.'.edit')); } } public function testUserCanRegisterApiResource() { $this->router->apiResource('users', RouteRegistrarControllerStub::class); $this->assertCount(5, $this->router->getRoutes()); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.edit')); } public function testUserCanRegisterApiResourceWithExceptOption() { $this->router->apiResource('users', RouteRegistrarControllerStub::class, [ 'except' => ['destroy'], ]); $this->assertCount(4, $this->router->getRoutes()); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.create')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.edit')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.destroy')); } public function testUserCanRegisterApiResourceWithOnlyOption() { $this->router->apiResource('users', RouteRegistrarControllerStub::class, [ 'only' => ['index', 'show'], ]); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); } public function testCanNameRoutesOnRegisteredResource() { $this->router->resource('comments', RouteRegistrarControllerStub::class) ->only('create', 'store')->names('reply'); $this->router->resource('users', RouteRegistrarControllerStub::class) ->only('create', 'store')->names([ 'create' => 'user.build', 'store' => 'user.save', ]); $this->router->resource('posts', RouteRegistrarControllerStub::class) ->only('create', 'destroy') ->name('create', 'posts.make') ->name('destroy', 'posts.remove'); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('reply.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('reply.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.build')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.save')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('posts.make')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('posts.remove')); } public function testCanOverrideParametersOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->parameters(['users' => 'admin_user']); $this->router->resource('posts', RouteRegistrarControllerStub::class) ->parameter('posts', 'topic'); $this->assertStringContainsString('admin_user', $this->router->getRoutes()->getByName('users.show')->uri); $this->assertStringContainsString('topic', $this->router->getRoutes()->getByName('posts.show')->uri); } public function testCanSetMiddlewareOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->middleware(RouteRegistrarMiddlewareStub::class); $this->seeMiddleware(RouteRegistrarMiddlewareStub::class); } public function testCanSetMiddlewareCanOnGroups() { $this->router->can('test')->group(function ($router) { $router->get('/'); }); $this->seeMiddleware('can:test'); } public function testCanSetMiddlewareCanWithModelsOnGroups() { $this->router->can('view', 'post')->group(function ($router) { $router->get('/post/{post}'); }); $this->seeMiddleware('can:view,post'); } public function testCanSetMiddlewareCanNestedOnGroups() { $this->router->can('access-admin')->group(function ($router) { $router->can('edit', 'post')->group(function ($router) { $router->get('/post/{post}/edit'); }); }); $this->assertEquals([ 'can:access-admin', 'can:edit,post', ], $this->getRoute()->middleware()); } public function testCanSetMiddlewareForSpecifiedMethodsOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->middleware('default') ->middlewareFor('index', RouteRegistrarMiddlewareStub::class) ->middlewareFor(['create', 'store'], 'one') ->middlewareFor(['edit'], ['one', 'two']); $this->router->getRoutes()->refreshNameLookups(); $this->assertEquals(['default', RouteRegistrarMiddlewareStub::class], $this->router->getRoutes()->getByName('users.index')->gatherMiddleware()); $this->assertEquals(['default', 'one'], $this->router->getRoutes()->getByName('users.create')->gatherMiddleware()); $this->assertEquals(['default', 'one'], $this->router->getRoutes()->getByName('users.store')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.show')->gatherMiddleware()); $this->assertEquals(['default', 'one', 'two'], $this->router->getRoutes()->getByName('users.edit')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.update')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.destroy')->gatherMiddleware()); $this->router->resource('users', RouteRegistrarControllerStub::class) ->middlewareFor('index', RouteRegistrarMiddlewareStub::class) ->middlewareFor(['create', 'store'], 'one') ->middlewareFor(['edit'], ['one', 'two']) ->middleware('default'); $this->router->getRoutes()->refreshNameLookups(); $this->assertEquals([RouteRegistrarMiddlewareStub::class, 'default'], $this->router->getRoutes()->getByName('users.index')->gatherMiddleware()); $this->assertEquals(['one', 'default'], $this->router->getRoutes()->getByName('users.create')->gatherMiddleware()); $this->assertEquals(['one', 'default'], $this->router->getRoutes()->getByName('users.store')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.show')->gatherMiddleware()); $this->assertEquals(['one', 'two', 'default'], $this->router->getRoutes()->getByName('users.edit')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.update')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.destroy')->gatherMiddleware()); } public function testResourceWithoutMiddlewareRegistration() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->only('index') ->middleware(['one', 'two']) ->withoutMiddleware('one'); $this->seeResponse('controller', Request::create('users', 'GET')); $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); } public function testCanSetExcludedMiddlewareForSpecifiedMethodsOnRegisteredResource() { $this->router->resource('users', RouteRegistrarControllerStub::class) ->withoutMiddleware('one') ->withoutMiddlewareFor('index', 'two') ->withoutMiddlewareFor(['create', 'store'], 'three') ->withoutMiddlewareFor(['edit'], ['four', 'five']); $this->assertEquals(['one', 'two'], $this->router->getRoutes()->getByName('users.index')->excludedMiddleware()); $this->assertEquals(['one', 'three'], $this->router->getRoutes()->getByName('users.create')->excludedMiddleware()); $this->assertEquals(['one', 'three'], $this->router->getRoutes()->getByName('users.store')->excludedMiddleware()); $this->assertEquals(['one'], $this->router->getRoutes()->getByName('users.show')->excludedMiddleware()); $this->assertEquals(['one', 'four', 'five'], $this->router->getRoutes()->getByName('users.edit')->excludedMiddleware()); $this->assertEquals(['one'], $this->router->getRoutes()->getByName('users.update')->excludedMiddleware()); $this->assertEquals(['one'], $this->router->getRoutes()->getByName('users.destroy')->excludedMiddleware()); } public function testResourceWithMiddlewareAsStringable() { $one = new class implements Stringable { public function __toString() { return 'one'; } }; $this->router->resource('users', RouteRegistrarControllerStub::class) ->only('index') ->middleware([$one, 'two']) ->withoutMiddleware('one'); $this->seeResponse('controller', Request::create('users', 'GET')); $this->assertEquals(['one', 'two'], $this->getRoute()->middleware()); $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); } public function testResourceWheres() { $wheres = [ 'user' => '\d+', 'test' => '[a-z]+', ]; $this->router->resource('users', RouteRegistrarControllerStub::class) ->where($wheres); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testWhereNumberRegistration() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; $this->router->get('/{foo}/{bar}')->whereNumber(['foo', 'bar']); $this->router->get('/api/{bar}/{foo}')->whereNumber(['bar', 'foo']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testWhereAlphaRegistration() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; $this->router->get('/{foo}/{bar}')->whereAlpha(['foo', 'bar']); $this->router->get('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testWhereAlphaNumericRegistration() { $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; $this->router->get('/{foo}')->whereAlphaNumeric(['1a2b3c']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testWhereUlidRegistration() { $this->router->get('/{foo}')->whereUlid('foo'); $this->assertTrue($this->getRoute()->matches(Request::create('/01ARZ3NDEKTSV4RRFFQ69G5FAV', 'GET'))); $this->assertFalse($this->getRoute()->matches(Request::create('/01ARZ3NDEKTSV4RRFFQ69G5FA', 'GET'))); $this->assertFalse($this->getRoute()->matches(Request::create('/01ARZ3NDEKTSV4RRFFQ69G5FAI', 'GET'))); $this->assertFalse($this->getRoute()->matches(Request::create('/81ARZ3NDEKTSV4RRFFQ69G5FAV', 'GET'))); } public function testWhereUuidRegistration() { $this->router->get('/{foo}')->whereUuid('foo'); $this->assertTrue($this->getRoute()->matches(Request::create('/2cd90b6d-3c34-4a0a-9d0d-9d0b7b1a2e6f', 'GET'))); $this->assertTrue($this->getRoute()->matches(Request::create('/2CD90B6D-3C34-4A0A-9D0D-9D0B7B1A2E6F', 'GET'))); $this->assertFalse($this->getRoute()->matches(Request::create('/2cd90b6d3c344a0a9d0d9d0b7b1a2e6f', 'GET'))); $this->assertFalse($this->getRoute()->matches(Request::create('/2cd90b6d-3c34-4a0a-9d0d-9d0b7b1a2e6', 'GET'))); } public function testWhereInRegistration() { $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; $this->router->get('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two']); $this->router->get('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two']); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testWhereInEnumRegistration() { $this->router->get('/posts/{category}')->whereIn('category', CategoryBackedEnum::cases()); $invalidRequest = Request::create('/posts/invalid-value', 'GET'); $this->assertFalse($this->getRoute()->matches($invalidRequest)); foreach (CategoryBackedEnum::cases() as $case) { $request = Request::create('/posts/'.$case->value, 'GET'); $this->assertTrue($this->getRoute()->matches($request)); } } public function testGroupWhereNumberRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; $this->router->prefix('/{foo}/{bar}')->whereNumber(['foo', 'bar'])->group(function ($router) { $router->get('/'); }); $this->router->prefix('/api/{bar}/{foo}')->whereNumber(['bar', 'foo'])->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereAlphaRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; $this->router->prefix('/{foo}/{bar}')->whereAlpha(['foo', 'bar'])->group(function ($router) { $router->get('/'); }); $this->router->prefix('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo'])->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereAlphaNumericRegistrationOnRouteRegistrar() { $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; $this->router->prefix('/{foo}')->whereAlphaNumeric(['1a2b3c'])->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereInRegistrationOnRouteRegistrar() { $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; $this->router->prefix('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two'])->group(function ($router) { $router->get('/'); }); $this->router->prefix('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two'])->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereNumberRegistrationOnRouter() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; $this->router->whereNumber(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); $this->router->whereNumber(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereAlphaRegistrationOnRouter() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; $this->router->whereAlpha(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); $this->router->whereAlpha(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereAlphaNumericRegistrationOnRouter() { $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; $this->router->whereAlphaNumeric(['1a2b3c'])->prefix('/{foo}')->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testGroupWhereInRegistrationOnRouter() { $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; $this->router->whereIn(['foo', 'bar'], ['one', 'two'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); $this->router->whereIn(['bar', 'foo'], ['one', 'two'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); }); /** @var \Illuminate\Routing\Route $route */ foreach ($this->router->getRoutes() as $route) { $this->assertEquals($wheres, $route->wheres); } } public function testCanSetRouteName() { $this->router->as('users.index')->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame('users.index', $this->getRoute()->getName()); } public function testCanSetRouteNameUsingNameAlias() { $this->router->name('users.index')->get('users', function () { return 'all-users'; }); $this->seeResponse('all-users', Request::create('users', 'GET')); $this->assertSame('users.index', $this->getRoute()->getName()); } public function testCanSetRouteNameUsingStringBackedEnum() { $this->router->name(RouteNameEnum::UserIndex)->get('users', fn () => 'all-users'); $this->assertSame('users.index', $this->getRoute()->getName()); } public function testCannotSetRouteNameUsingIntegerBackedEnum() { $this->expectExceptionObject(new InvalidArgumentException('Attribute [name] expects a string backed enum.')); $this->router->name(IntegerEnum::One)->get('users', fn () => 'all-users'); } public function testCanSetRouteDomainUsingStringBackedEnum() { $this->router->domain(RouteDomainEnum::DashboardDomain)->get('users', fn () => 'all-users'); $this->assertSame('dashboard.myapp.com', $this->getRoute()->getDomain()); } public function testCannotSetRouteDomainUsingIntegerBackedEnum() { $this->expectExceptionObject(new InvalidArgumentException('Attribute [domain] expects a string backed enum.')); $this->router->domain(IntegerEnum::One)->get('users', fn () => 'all-users'); } public function testPushMiddlewareToGroup() { $this->router->middlewareGroup('web', []); $this->router->pushMiddlewareToGroup('web', 'test-middleware'); $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); } public function testPushMiddlewareToGroupUnregisteredGroup() { $this->router->pushMiddlewareToGroup('web', 'test-middleware'); $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); } public function testPushMiddlewareToGroupDuplicatedMiddleware() { $this->router->pushMiddlewareToGroup('web', 'test-middleware'); $this->router->pushMiddlewareToGroup('web', 'test-middleware'); $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); } public function testCanRemoveMiddlewareFromGroup() { $this->router->pushMiddlewareToGroup('web', 'test-middleware'); $this->router->removeMiddlewareFromGroup('web', 'test-middleware'); $this->assertSame([], $this->router->getMiddlewareGroups()['web']); } public function testCanRemoveMiddlewareFromGroupNotUnregisteredMiddleware() { $this->router->middlewareGroup('web', []); $this->router->removeMiddlewareFromGroup('web', 'different-test-middleware'); $this->assertSame([], $this->router->getMiddlewareGroups()['web']); } public function testCanRemoveMiddlewareFromGroupUnregisteredGroup() { $this->router->removeMiddlewareFromGroup('web', ['test-middleware']); $this->assertSame([], $this->router->getMiddlewareGroups()); } public function testCanRegisterSingleton() { $this->router->singleton('user', RouteRegistrarControllerStub::class); $this->assertCount(3, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); } public function testCanSetRouteMetadataOnSingleton() { $this->router->singleton('user', RouteRegistrarControllerStub::class) ->metadata(['head' => ['title' => 'User']]); $this->assertSame( ['title' => 'User'], $this->router->getRoutes()->getByName('user.show')->getMetadata('head') ); } public function testCanRegisterApiSingleton() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); } public function testCanSetRouteMetadataOnApiSingleton() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->metadata(['head' => ['title' => 'User']]); $this->assertSame( ['title' => 'User'], $this->router->getRoutes()->getByName('user.show')->getMetadata('head') ); } public function testCanRegisterCreatableSingleton() { $this->router->singleton('user', RouteRegistrarControllerStub::class)->creatable(); $this->assertCount(6, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testCanRegisterCreatableApiSingleton() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class)->creatable(); $this->assertCount(4, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testSingletonCreatableNotDestroyable() { $this->router->singleton('user', RouteRegistrarControllerStub::class) ->creatable() ->except('destroy'); $this->assertCount(5, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testApiSingletonCreatableNotDestroyable() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->creatable() ->except('destroy'); $this->assertCount(3, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testSingletonCanBeDestroyable() { $this->router->singleton('user', RouteRegistrarControllerStub::class) ->destroyable(); $this->assertCount(4, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.edit')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testApiSingletonCanBeDestroyable() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->destroyable(); $this->assertCount(3, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.show')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.update')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.destroy')); } public function testSingletonCanBeOnlyCreatable() { $this->router->singleton('user', RouteRegistrarControllerStub::class) ->creatable() ->only('create', 'store'); $this->assertCount(2, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.create')); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); } public function testApiSingletonCanBeOnlyCreatable() { $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->creatable() ->only('store'); $this->assertCount(1, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.store')); } public function testSingletonDoesntAllowIncludingUnsupportedMethods() { $this->router->singleton('post', RouteRegistrarControllerStub::class) ->only('index', 'store', 'create', 'destroy'); $this->assertCount(0, $this->router->getRoutes()); $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->only('index', 'store', 'create', 'destroy'); $this->assertCount(0, $this->router->getRoutes()); } public function testApiSingletonCanIncludeAnySingletonMethods() { // This matches the behavior of the apiResource method. $this->router->apiSingleton('user', RouteRegistrarControllerStub::class) ->only('edit'); $this->assertCount(1, $this->router->getRoutes()); $this->assertTrue($this->router->getRoutes()->hasNamedRoute('user.edit')); } public function testCanSetMiddlewareForSpecifiedMethodsOnRegisteredSingletonResource() { $this->router->singleton('users', RouteRegistrarControllerStub::class) ->creatable() ->destroyable() ->middleware('default') ->middlewareFor('show', RouteRegistrarMiddlewareStub::class) ->middlewareFor(['create', 'store'], 'one') ->middlewareFor(['edit'], ['one', 'two']); $this->router->getRoutes()->refreshNameLookups(); $this->assertEquals(['default', 'one'], $this->router->getRoutes()->getByName('users.create')->gatherMiddleware()); $this->assertEquals(['default', 'one'], $this->router->getRoutes()->getByName('users.store')->gatherMiddleware()); $this->assertEquals(['default', RouteRegistrarMiddlewareStub::class], $this->router->getRoutes()->getByName('users.show')->gatherMiddleware()); $this->assertEquals(['default', 'one', 'two'], $this->router->getRoutes()->getByName('users.edit')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.update')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.destroy')->gatherMiddleware()); $this->router->singleton('users', RouteRegistrarControllerStub::class) ->creatable() ->destroyable() ->middlewareFor('show', RouteRegistrarMiddlewareStub::class) ->middlewareFor(['create', 'store'], 'one') ->middlewareFor(['edit'], ['one', 'two']) ->middleware('default'); $this->router->getRoutes()->refreshNameLookups(); $this->assertEquals(['one', 'default'], $this->router->getRoutes()->getByName('users.create')->gatherMiddleware()); $this->assertEquals(['one', 'default'], $this->router->getRoutes()->getByName('users.store')->gatherMiddleware()); $this->assertEquals([RouteRegistrarMiddlewareStub::class, 'default'], $this->router->getRoutes()->getByName('users.show')->gatherMiddleware()); $this->assertEquals(['one', 'two', 'default'], $this->router->getRoutes()->getByName('users.edit')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.update')->gatherMiddleware()); $this->assertEquals(['default'], $this->router->getRoutes()->getByName('users.destroy')->gatherMiddleware()); } public function testCanSetExcludedMiddlewareForSpecifiedMethodsOnRegisteredSingletonResource() { $this->router->singleton('users', RouteRegistrarControllerStub::class) ->creatable() ->destroyable() ->withoutMiddleware('one') ->withoutMiddlewareFor('show', 'two') ->withoutMiddlewareFor(['create', 'store'], 'three') ->withoutMiddlewareFor(['edit'], ['four', 'five']); $this->assertEquals(['one', 'three'], $this->router->getRoutes()->getByName('users.create')->excludedMiddleware()); $this->assertEquals(['one', 'three'], $this->router->getRoutes()->getByName('users.store')->excludedMiddleware()); $this->assertEquals(['one', 'two'], $this->router->getRoutes()->getByName('users.show')->excludedMiddleware()); $this->assertEquals(['one', 'four', 'five'], $this->router->getRoutes()->getByName('users.edit')->excludedMiddleware()); $this->assertEquals(['one'], $this->router->getRoutes()->getByName('users.update')->excludedMiddleware()); $this->assertEquals(['one'], $this->router->getRoutes()->getByName('users.destroy')->excludedMiddleware()); } /** * Get the last route registered with the router. * * @return \Illuminate\Routing\Route */ protected function getRoute() { return last($this->router->getRoutes()->get()); } /** * Assert that the last route has the given middleware. * * @param string $middleware * @return void */ protected function seeMiddleware($middleware) { $this->assertEquals($middleware, $this->getRoute()->middleware()[0]); } /** * Assert that the last route has the given content. * * @param string $content * @param \Illuminate\Http\Request $request * @return void */ protected function seeResponse($content, Request $request) { $route = $this->getRoute(); $this->assertTrue($route->matches($request)); $this->assertEquals($content, $route->bind($request)->run()); } } class RouteRegistrarControllerStub { public function index() { return 'controller'; } public function destroy() { return 'deleted'; } } class InvokableRouteRegistrarControllerStub { public function __invoke() { return 'controller'; } } class RouteRegistrarMiddlewareStub { // }