Skip to content

Commit

Permalink
Split RouteBuilder::routeClass() and RouteBuilder::extensions()
Browse files Browse the repository at this point in the history
  • Loading branch information
chinpei215 committed Jul 24, 2017
1 parent 27afeae commit 97d1f31
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 25 deletions.
55 changes: 53 additions & 2 deletions src/Routing/RouteBuilder.php
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
65 changes: 54 additions & 11 deletions tests/TestCase/Routing/RouteBuilderTest.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*
Expand All @@ -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];
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}
24 changes: 12 additions & 12 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -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']);
});
});
Expand All @@ -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([
Expand Down Expand Up @@ -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', []);
Expand All @@ -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());
});
});

Expand All @@ -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());
});
Expand Down Expand Up @@ -3372,7 +3372,7 @@ public function testCreateRouteBuilder()
'extensions' => ['json']
]);
$this->assertInstanceOf(RouteBuilder::class, $builder);
$this->assertSame(['json'], $builder->extensions());
$this->assertSame(['json'], $builder->getExtensions());
}

/**
Expand Down

0 comments on commit 97d1f31

Please sign in to comment.