forked from stecman/symfony-console-completion
/
CompletionHandlerTestCase.php
69 lines (60 loc) · 2.09 KB
/
CompletionHandlerTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Stecman\Component\Symfony\Console\BashCompletion\Tests\Common;
use PHPUnit\Framework\TestCase;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionHandler;
use Symfony\Component\Console\Application;
/**
* Base test case for running CompletionHandlers
*/
abstract class CompletionHandlerTestCase extends TestCase
{
/**
* @var Application
*/
protected $application;
public static function setUpBeforeClass()
{
require_once __DIR__ . '/../Fixtures/CompletionAwareCommand.php';
require_once __DIR__ . '/../Fixtures/HiddenCommand.php';
require_once __DIR__ . '/../Fixtures/TestBasicCommand.php';
require_once __DIR__ . '/../Fixtures/TestSymfonyStyleCommand.php';
}
protected function setUp()
{
$this->application = new Application('Base application');
$this->application->addCommands(array(
new \CompletionAwareCommand(),
new \TestBasicCommand(),
new \TestSymfonyStyleCommand()
));
if (method_exists('\HiddenCommand', 'setHidden')) {
$this->application->add(new \HiddenCommand());
}
}
/**
* Create a handler set up with the given commandline and cursor position
*
* @param $commandLine
* @param int $cursorIndex
* @return CompletionHandler
*/
protected function createHandler($commandLine, $cursorIndex = null)
{
$context = new CompletionContext();
$context->setCommandLine($commandLine);
$context->setCharIndex($cursorIndex === null ? strlen($commandLine) : $cursorIndex);
return new CompletionHandler($this->application, $context);
}
/**
* Get the list of terms from the output of CompletionHandler
* The array index needs to be reset so that PHPUnit's array equality assertions match correctly.
*
* @param string $handlerOutput
* @return string[]
*/
protected function getTerms($handlerOutput)
{
return array_values($handlerOutput);
}
}