diff --git a/src/Routing/RouteBuilder.php b/src/Routing/RouteBuilder.php index f0eb25a0ba3..3d206410a27 100644 --- a/src/Routing/RouteBuilder.php +++ b/src/Routing/RouteBuilder.php @@ -146,15 +146,39 @@ public function __construct(RouteCollection $collection, $path, array $params = /** * Get or set default route class. * + * @deprecated 3.5.0 Use getRouteClass/setRouteClass instead. * @param string|null $routeClass Class name. * @return string|null */ public function routeClass($routeClass = null) { if ($routeClass === null) { - return $this->_routeClass; + return $this->getRouteClass(); } + $this->setRouteClass($routeClass); + } + + /** + * Set default route class. + * + * @param string $routeClass Class name. + * @return $this + */ + public function setRouteClass($routeClass) + { $this->_routeClass = $routeClass; + + return $this; + } + + /** + * Get default route class. + * + * @return string + */ + public function getRouteClass() + { + return $this->_routeClass; } /** @@ -163,15 +187,42 @@ public function routeClass($routeClass = null) * Future routes connected in through this builder will have the connected * extensions applied. However, setting extensions does not modify existing routes. * + * @deprecated 3.5.0 Use getExtensions/setExtensions instead. * @param null|string|array $extensions Either the extensions to use or null. * @return array|null */ public function extensions($extensions = null) { if ($extensions === null) { - return $this->_extensions; + return $this->getExtensions(); } + $this->setExtensions($extensions); + } + + /** + * Set the extensions in this route builder's scope. + * + * Future routes connected in through this builder will have the connected + * extensions applied. However, setting extensions does not modify existing routes. + * + * @param string|array $extensions The extensions to set. + * @return $this + */ + public function setExtensions($extensions) + { $this->_extensions = (array)$extensions; + + return $this; + } + + /** + * Get the extensions in this route builder's scope. + * + * @return array + */ + public function getExtensions() + { + return $this->_extensions; } /** diff --git a/tests/TestCase/Routing/RouteBuilderTest.php b/tests/TestCase/Routing/RouteBuilderTest.php index e2a73273ee4..7facc51975d 100644 --- a/tests/TestCase/Routing/RouteBuilderTest.php +++ b/tests/TestCase/Routing/RouteBuilderTest.php @@ -122,7 +122,8 @@ public function testRouteClass() $this->collection = new RouteCollection(); $routes = new RouteBuilder($this->collection, '/l'); - $routes->routeClass('TestApp\Routing\Route\DashedRoute'); + $this->assertSame($routes, $routes->setRouteClass('TestApp\Routing\Route\DashedRoute')); + $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass()); $routes->connect('/:controller', ['action' => 'index']); $all = $this->collection->routes(); @@ -200,6 +201,18 @@ public function testNameExists() $this->assertTrue($routes->nameExists('myRouteName')); } + /** + * Test setExtensions() and getExtensions(). + * + * @return void + */ + public function testExtensions() + { + $routes = new RouteBuilder($this->collection, '/l'); + $this->assertSame($routes, $routes->setExtensions(['html'])); + $this->assertSame(['html'], $routes->getExtensions()); + } + /** * Test extensions being connected to routes. * @@ -213,13 +226,13 @@ public function testConnectExtensions() [], ['extensions' => ['json']] ); - $this->assertEquals(['json'], $routes->extensions()); + $this->assertEquals(['json'], $routes->getExtensions()); $routes->connect('/:controller'); $route = $this->collection->routes()[0]; $this->assertEquals(['json'], $route->options['_ext']); - $routes->extensions(['xml', 'json']); + $routes->setExtensions(['xml', 'json']); $routes->connect('/:controller/:action'); $new = $this->collection->routes()[1]; @@ -240,26 +253,26 @@ public function testConnectExtensionsAdd() [], ['extensions' => ['json']] ); - $this->assertEquals(['json'], $routes->extensions()); + $this->assertEquals(['json'], $routes->getExtensions()); $routes->addExtensions(['xml']); - $this->assertEquals(['json', 'xml'], $routes->extensions()); + $this->assertEquals(['json', 'xml'], $routes->getExtensions()); $routes->addExtensions('csv'); - $this->assertEquals(['json', 'xml', 'csv'], $routes->extensions()); + $this->assertEquals(['json', 'xml', 'csv'], $routes->getExtensions()); } /** - * test that extensions() accepts a string. + * test that setExtensions() accepts a string. * * @return void */ public function testExtensionsString() { $routes = new RouteBuilder($this->collection, '/l'); - $routes->extensions('json'); + $routes->setExtensions('json'); - $this->assertEquals(['json'], $routes->extensions()); + $this->assertEquals(['json'], $routes->getExtensions()); } /** @@ -586,7 +599,7 @@ public function testResourcesMappings() public function testResourcesInScope() { Router::scope('/api', ['prefix' => 'api'], function ($routes) { - $routes->extensions(['json']); + $routes->setExtensions(['json']); $routes->resources('Articles'); }); $url = Router::url([ @@ -765,7 +778,7 @@ public function testFallbacksWithClass() public function testDefaultRouteClassFallbacks() { $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']); - $routes->routeClass('TestApp\Routing\Route\DashedRoute'); + $routes->setRouteClass('TestApp\Routing\Route\DashedRoute'); $routes->fallbacks(); $all = $this->collection->routes(); @@ -1061,4 +1074,34 @@ public function testLoadPlugin() $this->assertCount(1, $this->collection->routes()); $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET')); } + + /** + * Test routeClass() still works. + * + * @return void + */ + public function testRouteClassBackwardCompat() + { + $routes = new RouteBuilder($this->collection, '/l'); + $this->assertNull($routes->routeClass('TestApp\Routing\Route\DashedRoute')); + $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->routeClass()); + $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass()); + } + + /** + * Test extensions() still works. + * + * @return void + */ + public function testExtensionsBackwardCompat() + { + $routes = new RouteBuilder($this->collection, '/l'); + $this->assertNull($routes->extensions(['html'])); + $this->assertSame(['html'], $routes->extensions()); + $this->assertSame(['html'], $routes->getExtensions()); + + $this->assertNull($routes->extensions('json')); + $this->assertSame(['json'], $routes->extensions()); + $this->assertSame(['json'], $routes->getExtensions()); + } } diff --git a/tests/TestCase/Routing/RouterTest.php b/tests/TestCase/Routing/RouterTest.php index f0b7dd9fb46..bc936b92d3c 100644 --- a/tests/TestCase/Routing/RouterTest.php +++ b/tests/TestCase/Routing/RouterTest.php @@ -1819,11 +1819,11 @@ public function testExtensionsWithScopedRoutes() Router::extensions(['json']); Router::scope('/', function ($routes) { - $routes->extensions('rss'); + $routes->setExtensions('rss'); $routes->connect('/', ['controller' => 'Pages', 'action' => 'index']); $routes->scope('/api', function ($routes) { - $routes->extensions('xml'); + $routes->setExtensions('xml'); $routes->connect('/docs', ['controller' => 'ApiDocs', 'action' => 'index']); }); }); @@ -1839,7 +1839,7 @@ public function testExtensionsWithScopedRoutes() public function testResourcesInScope() { Router::scope('/api', ['prefix' => 'api'], function ($routes) { - $routes->extensions(['json']); + $routes->setExtensions(['json']); $routes->resources('Articles'); }); $url = Router::url([ @@ -3107,12 +3107,12 @@ public function testScopeExtensionsContained() { Router::extensions(['json']); Router::scope('/', function ($routes) { - $this->assertEquals(['json'], $routes->extensions(), 'Should default to global extensions.'); - $routes->extensions(['rss']); + $this->assertEquals(['json'], $routes->getExtensions(), 'Should default to global extensions.'); + $routes->setExtensions(['rss']); $this->assertEquals( ['rss'], - $routes->extensions(), + $routes->getExtensions(), 'Should include new extensions.' ); $routes->connect('/home', []); @@ -3121,13 +3121,13 @@ public function testScopeExtensionsContained() $this->assertEquals(['json', 'rss'], array_values(Router::extensions())); Router::scope('/api', function ($routes) { - $this->assertEquals(['json'], $routes->extensions(), 'Should default to global extensions.'); + $this->assertEquals(['json'], $routes->getExtensions(), 'Should default to global extensions.'); - $routes->extensions(['json', 'csv']); + $routes->setExtensions(['json', 'csv']); $routes->connect('/export', []); $routes->scope('/v1', function ($routes) { - $this->assertEquals(['json', 'csv'], $routes->extensions()); + $this->assertEquals(['json', 'csv'], $routes->getExtensions()); }); }); @@ -3143,8 +3143,8 @@ public function testScopeOptions() { $options = ['param' => 'value', 'routeClass' => 'InflectedRoute', 'extensions' => ['json']]; Router::scope('/path', $options, function ($routes) { - $this->assertSame('InflectedRoute', $routes->routeClass()); - $this->assertSame(['json'], $routes->extensions()); + $this->assertSame('InflectedRoute', $routes->getRouteClass()); + $this->assertSame(['json'], $routes->getExtensions()); $this->assertEquals('/path', $routes->path()); $this->assertEquals(['param' => 'value'], $routes->params()); }); @@ -3372,7 +3372,7 @@ public function testCreateRouteBuilder() 'extensions' => ['json'] ]); $this->assertInstanceOf(RouteBuilder::class, $builder); - $this->assertSame(['json'], $builder->extensions()); + $this->assertSame(['json'], $builder->getExtensions()); } /**