Skip to content

Commit

Permalink
Container: shows suggestions for missing components
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 6, 2015
1 parent 533063e commit 07bce43
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": ">=5.3.1",
"nette/utils": "~2.2"
"nette/utils": "^2.3.5"
},
"require-dev": {
"nette/tester": "~1.3"
Expand Down
6 changes: 5 additions & 1 deletion src/ComponentModel/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ public function getComponent($name, $need = TRUE)
}

} elseif ($need) {
throw new Nette\InvalidArgumentException("Component with name '$name' does not exist.");
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_merge(
array_keys($this->components),
array_map('lcfirst', preg_filter('#^createComponent([A-Z0-9].*)#', '$1', get_class_methods($this)))
), $name);
throw new Nette\InvalidArgumentException("Component with name '$name' does not exist" . ($hint ? ", did you mean '$hint'?" : '.'));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ComponentModel/Container.factory.addComponent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Assert::same('b', $a->getComponent('b')->getName());

Assert::exception(function () use ($a) {
$a->getComponent('B')->getName();
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Assert::same('b', $a->getComponent('b')->getName());

Assert::exception(function () use ($a) {
$a->getComponent('B')->getName();
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");
2 changes: 1 addition & 1 deletion tests/ComponentModel/Container.factory.return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Assert::count(1, $a->getComponents());

Assert::exception(function () use ($a) {
$a->getComponent('B')->getName();
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");


$a->removeComponent($b);
Expand Down
57 changes: 57 additions & 0 deletions tests/ComponentModel/Container.suggestions.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Test: Nette\ComponentModel\Container suggestions.
*/

use Nette\ComponentModel\Container;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestContainer extends Container
{

public function createComponentPublic()
{
}

public static function createComponentPublicStatic()
{
}

protected function createComponentProtected()
{
}

private function createComponentPrivate()
{
}

}


$cont = new TestContainer;
$cont->addComponent(new TestContainer, 'form');

Assert::exception(function () use ($cont) {
$comp = $cont->getComponent('from');
}, 'Nette\InvalidArgumentException', "Component with name 'from' does not exist, did you mean 'form'?");

Assert::exception(function () use ($cont) {
$comp = $cont->getComponent('Public');
}, 'Nette\InvalidArgumentException', "Component with name 'Public' does not exist, did you mean 'public'?");

Assert::exception(function () use ($cont) {
$comp = $cont->getComponent('PublicStatic');
}, 'Nette\InvalidArgumentException', "Component with name 'PublicStatic' does not exist, did you mean 'publicStatic'?");

Assert::exception(function () use ($cont) {
$comp = $cont->getComponent('Protected');
}, 'Nette\InvalidArgumentException', "Component with name 'Protected' does not exist, did you mean 'protected'?");

Assert::exception(function () use ($cont) { // suggest only non-private methods
$comp = $cont->getComponent('Private');
}, 'Nette\InvalidArgumentException', "Component with name 'Private' does not exist.");

0 comments on commit 07bce43

Please sign in to comment.