4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\TabCompletion;
14 use Psy\Command\ListCommand;
15 use Psy\Command\ShowCommand;
16 use Psy\Configuration;
19 use Psy\TabCompletion\Matcher;
21 class AutoCompleterTest extends \PHPUnit\Framework\TestCase
25 * @param array $mustContain
26 * @param array $mustNotContain
27 * @dataProvider classesInput
29 public function testClassesCompletion($line, $mustContain, $mustNotContain)
31 $context = new Context();
39 new Matcher\VariablesMatcher(),
40 new Matcher\ClassNamesMatcher(),
41 new Matcher\ConstantsMatcher(),
42 new Matcher\FunctionsMatcher(),
43 new Matcher\ObjectMethodsMatcher(),
44 new Matcher\ObjectAttributesMatcher(),
45 new Matcher\KeywordsMatcher(),
46 new Matcher\ClassAttributesMatcher(),
47 new Matcher\ClassMethodsMatcher(),
48 new Matcher\CommandsMatcher($commands),
51 $config = new Configuration();
52 $tabCompletion = $config->getAutoCompleter();
53 foreach ($matchers as $matcher) {
54 if ($matcher instanceof ContextAware) {
55 $matcher->setContext($context);
57 $tabCompletion->addMatcher($matcher);
60 $context->setAll(['foo' => 12, 'bar' => new \DOMDocument()]);
62 $code = $tabCompletion->processCallback('', 0, [
63 'line_buffer' => $line,
65 'end' => strlen($line),
68 foreach ($mustContain as $mc) {
69 $this->assertContains($mc, $code);
72 foreach ($mustNotContain as $mnc) {
73 $this->assertNotContains($mnc, $code);
80 * draft, open to modifications
81 * - [ ] if the variable is an array, return the square bracket for completion
82 * - [ ] if the variable is a constructor or method, reflect to complete as a function call
83 * - [ ] if the preceding token is a variable, call operators or keywords compatible for completion
84 * - [X] a command always should be the second token after php_open_tag
85 * - [X] keywords are never consecutive
86 * - [X] namespacing completion should work just fine
87 * - [X] after a new keyword, should always be a class constructor, never a function call or keyword, constant,
88 * or variable that does not contain a existing class name.
89 * - [X] on a namespaced constructor the completion must show the classes related, not constants.
93 public function classesInput()
96 // input, must had, must not had
97 ['T_OPE', ['T_OPEN_TAG'], []],
98 ['st', ['stdClass'], []],
99 ['stdCla', ['stdClass'], []],
100 ['new s', ['stdClass'], []],
103 ['stdClass', 'Psy\\Context', 'Psy\\Configuration'],
104 ['require', 'array_search', 'T_OPEN_TAG', '$foo'],
106 ['new Psy\\C', ['Context'], ['CASE_LOWER']],
107 ['\s', ['stdClass'], []],
108 ['array_', ['array_search', 'array_map', 'array_merge'], []],
109 ['$bar->', ['load'], []],
111 ['6 + $b', ['bar'], []],
115 ['sho', ['show'], []],
116 ['12 + clone $', ['foo'], []],
119 // array('+', 'clone'),
120 // array('$foo', 'DOMDocument', 'array_map')
121 // ), requires a operator matcher?
122 ['$', ['foo', 'bar'], ['require', 'array_search', 'T_OPEN_TAG', 'Psy']],
125 ['Context', 'TabCompletion\\Matcher\\AbstractMatcher'],
126 ['require', 'array_search'],
129 'Psy\Test\TabCompletion\StaticSample::CO',
130 ['Psy\Test\TabCompletion\StaticSample::CONSTANT_VALUE'],
134 'Psy\Test\TabCompletion\StaticSample::',
135 ['Psy\Test\TabCompletion\StaticSample::$staticVariable'],
139 'Psy\Test\TabCompletion\StaticSample::',
140 ['Psy\Test\TabCompletion\StaticSample::staticFunction'],