Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Correct exception message when mounting
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and fabpot committed Apr 11, 2015
1 parent 11e5d5f commit 5c52a09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Silex/Application.php
Expand Up @@ -494,11 +494,15 @@ public function sendFile($file, $status = 200, array $headers = array(), $conten
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);
Expand Down
29 changes: 29 additions & 0 deletions tests/Silex/Tests/ApplicationTest.php
Expand Up @@ -13,6 +13,7 @@

use Silex\Application;
use Silex\ControllerCollection;
use Silex\ControllerProviderInterface;
use Silex\Route;
use Silex\Provider\MonologServiceProvider;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -505,6 +506,26 @@ public function testMountPreservesOrder()
$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();
Expand Down Expand Up @@ -551,3 +572,11 @@ public function barAction(Application $app, $name)
return 'Hello '.$app->escape($name);
}
}

class IncorrectControllerCollection implements ControllerProviderInterface
{
public function connect(Application $app)
{
return;
}
}

0 comments on commit 5c52a09

Please sign in to comment.