mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-16 22:39:52 +08:00
implement suggestion for unknown command in console application
suggestion is based on two principles: - first suggest commands the begin with the unknown name, to suggest commands after accidentally hitting enter - second find similar commands by computing the levenshtein distance which is a measurement on how many changes need to be made to convert one string into another. This is perfect for finding typos.
This commit is contained in:
70
tests/framework/console/UnkownCommandExceptionTest.php
Normal file
70
tests/framework/console/UnkownCommandExceptionTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yiiunit\framework\console;
|
||||
|
||||
use Yii;
|
||||
use yii\console\Application;
|
||||
use yii\console\UnknownCommandException;
|
||||
use yiiunit\TestCase;
|
||||
|
||||
/**
|
||||
* @group console
|
||||
*/
|
||||
class UnkownCommandExceptionTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->mockApplication([
|
||||
'enableCoreCommands' => false,
|
||||
'controllerMap' => [
|
||||
'cache' => 'yii\console\controllers\CacheController',
|
||||
'migrate' => 'yii\console\controllers\MigrateController',
|
||||
'message' => 'yii\console\controllers\MessageController',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function suggestedCommandsProvider()
|
||||
{
|
||||
return [
|
||||
['migate', ['migrate']],
|
||||
['mihate/u', ['migrate/up']],
|
||||
['mirgte/u', ['migrate/up']],
|
||||
['mirgte/up', ['migrate/up']],
|
||||
['mirgte', ['migrate']],
|
||||
['hlp', ['help']],
|
||||
['ca', ['cache', 'cache/flush', 'cache/flush-all', 'cache/flush-schema', 'cache/index']],
|
||||
['cach', ['cache', 'cache/flush', 'cache/flush-all', 'cache/flush-schema', 'cache/index']],
|
||||
['cach/fush', ['cache/flush']],
|
||||
['cach/fushall', ['cache/flush-all']],
|
||||
['what?', []],
|
||||
// test UTF 8 chars
|
||||
['ёлка', []],
|
||||
// this crashes levenshtein because string is longer than 255 chars
|
||||
[str_repeat('asdw1234', 31), []],
|
||||
[str_repeat('asdw1234', 32), []],
|
||||
[str_repeat('asdw1234', 33), []],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider suggestedCommandsProvider
|
||||
*/
|
||||
public function testSuggestCommand($command, $expectedSuggestion)
|
||||
{
|
||||
$exception = new UnknownCommandException($command, Yii::$app);
|
||||
$this->assertEquals($expectedSuggestion, $exception->getSuggestedAlternatives());
|
||||
}
|
||||
|
||||
public function testNameAndConstructor()
|
||||
{
|
||||
$exception = new UnknownCommandException('test', Yii::$app);
|
||||
$this->assertEquals('Unknown command', $exception->getName());
|
||||
$this->assertEquals('test', $exception->command);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user