Skip to content

Commit

Permalink
add tests and DuplicateRouteException
Browse files Browse the repository at this point in the history
  • Loading branch information
saeideng authored and markstory committed Aug 19, 2016
1 parent fceab36 commit f2d7c7c
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/Routing/Exception/DuplicateRouteException.php
@@ -0,0 +1,39 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.2.14
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Routing\Exception;

use Cake\Core\Exception\Exception;

/**
* Exception raised when a URL cannot be reverse routed
* or when a URL cannot be parsed.
*/
class DuplicateRouteException extends Exception
{

/**
* {@inheritDoc}
*/
protected $_messageTemplate = 'A named route was found for "%s" that is already used, route names must be unique across your entire application.';

/**
* {@inheritDoc}
*/
public function __construct($message, $code = 404)
{
if (is_array($message) && isset($message['message'])) {
$this->_messageTemplate = $message['message'];
}
parent::__construct($message, $code);
}
}
3 changes: 2 additions & 1 deletion src/Routing/RouteCollection.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Routing;

use Cake\Routing\Exception\MissingRouteException;
use Cake\Routing\Exception\DuplicateRouteException;
use Cake\Routing\Route\Route;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ public function add(Route $route, array $options = [])
// Explicit names
if (isset($options['_name'])) {
if (isset($this->_named[$options['_name']])) {
throw new MissingRouteException([
throw new DuplicateRouteException([
'url' => $options['_name'],
'message' => 'A named route was found for "%s" that is already used, route names must be unique across your entire application.'
]);
Expand Down
69 changes: 69 additions & 0 deletions src/Template/Error/duplicate_route.ctp
@@ -0,0 +1,69 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.2.14
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

use Cake\Routing\Router;
use Cake\Error\Debugger;

$this->layout = 'dev_error';

$this->assign('title', 'Duplicate Route');
$this->assign('templateName', 'duplicate_route.ctp');

$attributes = $error->getAttributes();

$this->start('subheading');
?>
<strong>Error: </strong>
<?= $error->getMessage(); ?>
<?php $this->end() ?>

<?php $this->start('file') ?>
<p>Route names must be unique across your entire application.
The same _name cannot be used twice,
even if the names occur inside a different routing scope.
remove duplicated route name in <?= 'config' . DIRECTORY_SEPARATOR . 'routes.php' ?></p>

<?php if (!empty($attributes['context'])): ?>
<p>The passed context was:</p>
<pre>
<?= Debugger::exportVar($attributes['context']); ?>
</pre>
<?php endif; ?>

<h3>Connected Routes</h3>
<table cellspacing="0" cellpadding="0">
<tr><th>Template</th><th>Defaults</th><th>Options</th></tr>
<?php
$url=false;
if (!empty($attributes['url'])){
$url=$attributes['url'];
}
foreach (Router::routes() as $route):
if(isset($route->options['_name']) && $url===$route->options['_name']){
echo '<tr class="error">';
}else{
echo '<tr>';
}
printf(
'<td width="25%%">%s</td><td>%s</td><td width="20%%">%s</td>',
$route->template,
Debugger::exportVar($route->defaults),
Debugger::exportVar($route->options)
);
echo '</tr>';
endforeach;
?>
</table>
<?php $this->end() ?>
16 changes: 16 additions & 0 deletions tests/TestCase/Routing/RouteCollectionTest.php
Expand Up @@ -355,6 +355,22 @@ public function testAddingRoutes()
$this->assertSame($two, $routes[1]);
}

/**
* Test the add() with some _name.
*
* @expectedException \Cake\Routing\Exception\DuplicateRouteException
*
* @return void
*/
public function testAddingDuplicatedRoutesName()
{
$one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
$this->collection->add($one,['_name' => 'test']);
$this->collection->add($two,['_name' => 'test']);

}

/**
* Test basic get/set of extensions.
*
Expand Down
49 changes: 49 additions & 0 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -1191,6 +1191,55 @@ public function testNamedRouteException()
$url = Router::url(['_name' => 'junk', 'name' => 'mark']);
}

/**
* Test that using duplicate names causes exceptions.
*
* @expectedException \Cake\Routing\Exception\DuplicateRouteException
* @return void
*/
public function testDuplicateRouteException()
{
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'test']
);
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'otherName']
);
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'test']
);
}

/**
* Test that using defferent names not causes exceptions.
*
* @return void
*/
public function testNoDuplicateRouteException()
{
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'test']
);
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'otherName']
);
Router::connect(
'/users/:name',
['controller' => 'users', 'action' => 'view'],
['_name' => 'test3']
);
}

/**
* Test that url filters are applied to url params.
*
Expand Down

0 comments on commit f2d7c7c

Please sign in to comment.