Skip to content

Commit

Permalink
Merge pull request #6839 from lenvanessen/release/3.3
Browse files Browse the repository at this point in the history
More descriptive exception when the bundled-extension class does not exist
  • Loading branch information
GwendolenLynch committed Jul 22, 2017
2 parents 06bcaa0 + 3dad2ad commit 6cd00e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,16 @@ public static function run($rootPath)
function ($extensions) use ($config) {
foreach ((array) $config['extensions'] as $extensionClass) {
if (is_string($extensionClass)) {
if (!class_exists($extensionClass)) {
throw new LogicException(sprintf('Extension class name "%s" is defined in .bolt.yml or .bolt.php, but the class name is misspelled or not loadable by Composer.', $extensionClass));
}
if (!is_a($extensionClass, ExtensionInterface::class, true)) {
throw new LogicException("$extensionClass needs to implement " . ExtensionInterface::class);
throw new LogicException(sprintf('Extension class "%s" must implement %s', $extensionClass, ExtensionInterface::class));
}
$extensionClass = new $extensionClass();
}
if (!$extensionClass instanceof ExtensionInterface) {
throw new LogicException(get_class($extensionClass) . ' needs to be an instance of ' . ExtensionInterface::class);
throw new LogicException(sprintf('Extension class "%s" must be an instance of %s', get_class($extensionClass), ExtensionInterface::class));
}
$extensions->add($extensionClass);
}
Expand Down
27 changes: 25 additions & 2 deletions tests/phpunit/unit/Bootstrap/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function testRunExtensionString()

/**
* @expectedException \LogicException
* @expectedExceptionMessage stdClass needs to implement Bolt\Extension\ExtensionInterface
* @expectedExceptionMessage Extension class "stdClass" must implement Bolt\Extension\ExtensionInterface
*/
public function testRunExtensionStringInvalid()
{
Expand All @@ -358,6 +358,29 @@ public function testRunExtensionStringInvalid()
$this->assertInstanceOf(NormalExtension::class, $ext);
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Extension class name "DropBear\Ninja" is defined in .bolt.yml or .bolt.php, but the class name is misspelled or not loadable by Composer.
*/
public function testRunExtensionStringNotFound()
{
$config = [
'extensions' => [
'DropBear\Ninja',
],
];

$yaml = (new Dumper())->dump($config, 4, 0, true);
$fs = new Filesystem();
$fs->dumpFile($this->rootPath . '/.bolt.yml', $yaml);

$app = Bootstrap::run($this->rootPath);
$extensions = $app['extensions'];

$ext = $extensions->get('Bolt/Normal');
$this->assertInstanceOf(NormalExtension::class, $ext);
}

public function testRunExtensionObject()
{
$config = <<<EOF
Expand All @@ -381,7 +404,7 @@ public function testRunExtensionObject()

/**
* @expectedException \LogicException
* @expectedExceptionMessage stdClass needs to be an instance of Bolt\Extension\ExtensionInterface
* @expectedExceptionMessage Extension class "stdClass" must be an instance of Bolt\Extension\ExtensionInterface
*/
public function testRunExtensionObjectInvalid()
{
Expand Down

0 comments on commit 6cd00e8

Please sign in to comment.