Commit 7e374761 authored by Fabien Potencier's avatar Fabien Potencier

bug #1137 Correct exception message when mounting (SpacePossum)

This PR was squashed before being merged into the 1.2 branch (closes #1137).

Discussion
----------

Correct exception message when mounting

Use a better exception message when calling `mount` with a `ControllerProviderInterface` that doesn't return an `ControllerCollection` on the `connect` call.

Commits
-------

5c52a09e Correct exception message when mounting
parents 11e5d5f4 5c52a09e
......@@ -494,11 +494,15 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
public function mount($prefix, $controllers)
{
if ($controllers instanceof ControllerProviderInterface) {
$controllers = $controllers->connect($this);
$connectedControllers = $controllers->connect($this);
if (!$connectedControllers instanceof ControllerCollection) {
throw new \LogicException(sprintf('The method "%s::connect" must return a "ControllerCollection" instance. Got: "%s"', get_class($controllers), is_object($connectedControllers) ? get_class($connectedControllers) : gettype($connectedControllers)));
}
if (!$controllers instanceof ControllerCollection) {
throw new \LogicException('The "mount" method takes either a ControllerCollection or a ControllerProviderInterface instance.');
$controllers = $connectedControllers;
} elseif (!$controllers instanceof ControllerCollection) {
throw new \LogicException('The "mount" method takes either a "ControllerCollection" or a "ControllerProviderInterface" instance.');
}
$this['controllers']->mount($prefix, $controllers);
......
......@@ -13,6 +13,7 @@ namespace Silex\Tests;
use Silex\Application;
use Silex\ControllerCollection;
use Silex\ControllerProviderInterface;
use Silex\Route;
use Silex\Provider\MonologServiceProvider;
use Symfony\Component\HttpFoundation\Request;
......@@ -505,6 +506,26 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('first', 'second', 'third'), array_keys(iterator_to_array($app['routes'])));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The "mount" method takes either a "ControllerCollection" or a "ControllerProviderInterface" instance.
*/
public function testMountNullException()
{
$app = new Application();
$app->mount('/exception', null);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The method "Silex\Tests\IncorrectControllerCollection::connect" must return a "ControllerCollection" instance. Got: "NULL"
*/
public function testMountWrongConnectReturnValueException()
{
$app = new Application();
$app->mount('/exception', new IncorrectControllerCollection());
}
public function testSendFile()
{
$app = new Application();
......@@ -551,3 +572,11 @@ class FooController
return 'Hello '.$app->escape($name);
}
}
class IncorrectControllerCollection implements ControllerProviderInterface
{
public function connect(Application $app)
{
return;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment