Use levenshtein level for better Bundle matching

This commit is contained in:
Jeremy Benoist 2016-05-17 14:52:22 +02:00
parent 66da91d030
commit ac7f74ecca
2 changed files with 7 additions and 4 deletions

View File

@ -142,6 +142,7 @@ class ControllerNameParser
$lev = levenshtein($nonExistentBundleName, $bundleName); $lev = levenshtein($nonExistentBundleName, $bundleName);
if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) { if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
$alternative = $bundleName; $alternative = $bundleName;
$shortest = $lev;
} }
} }

View File

@ -59,8 +59,8 @@ class ControllerNameParserTest extends TestCase
{ {
$parser = $this->createParser(); $parser = $this->createParser();
$this->assertEquals('FooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); $this->assertEquals('FoooooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
$this->assertEquals('FooBundle:Sub\Default:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); $this->assertEquals('FoooooBundle:Sub\Default:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
try { try {
$parser->build('TestBundle\FooBundle\Controller\DefaultController::index'); $parser->build('TestBundle\FooBundle\Controller\DefaultController::index');
@ -132,8 +132,9 @@ class ControllerNameParserTest extends TestCase
public function getInvalidBundleNameTests() public function getInvalidBundleNameTests()
{ {
return array( return array(
array('FoodBundle:Default:index', 'FooBundle:Default:index'), 'Alternative will be found using levenshtein' => array('FoodBundle:Default:index', 'FooBundle:Default:index'),
array('CrazyBundle:Default:index', false), 'Alternative will be found using partial match' => array('FabpotFooBund:Default:index', 'FabpotFooBundle:Default:index'),
'Bundle does not exist at all' => array('CrazyBundle:Default:index', false),
); );
} }
@ -162,6 +163,7 @@ class ControllerNameParserTest extends TestCase
$bundles = array( $bundles = array(
'SensioFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), 'SensioFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
'SensioCmsFooBundle' => $this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle'), 'SensioCmsFooBundle' => $this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle'),
'FoooooBundle' => $this->getBundle('TestBundle\FooBundle', 'FoooooBundle'),
'FooBundle' => $this->getBundle('TestBundle\FooBundle', 'FooBundle'), 'FooBundle' => $this->getBundle('TestBundle\FooBundle', 'FooBundle'),
'FabpotFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), 'FabpotFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
); );