4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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
26 * @param $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(array('foo' => 12, 'bar' => new \DOMDocument()));
62 $code = $tabCompletion->processCallback('', 0, array(
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 array('T_OPE', array('T_OPEN_TAG'), array()),
98 array('st', array('stdClass'), array()),
99 array('stdCla', array('stdClass'), array()),
100 array('new s', array('stdClass'), array()),
103 array('stdClass', 'Psy\\Context', 'Psy\\Configuration'),
104 array('require', 'array_search', 'T_OPEN_TAG', '$foo'),
106 array('new Psy\\C', array('Context'), array('CASE_LOWER')),
107 array('\s', array('stdClass'), array()),
108 array('array_', array('array_search', 'array_map', 'array_merge'), array()),
109 array('$bar->', array('load'), array()),
110 array('$b', array('bar'), array()),
111 array('6 + $b', array('bar'), array()),
112 array('$f', array('foo'), array()),
113 array('l', array('ls'), array()),
114 array('ls ', array(), array('ls')),
115 array('sho', array('show'), array()),
116 array('12 + clone $', array('foo'), array()),
119 // array('+', 'clone'),
120 // array('$foo', 'DOMDocument', 'array_map')
121 // ), requires a operator matcher?
122 array('$', array('foo', 'bar'), array('require', 'array_search', 'T_OPEN_TAG', 'Psy')),
125 array('Context', 'TabCompletion\\Matcher\\AbstractMatcher'),
126 array('require', 'array_search'),
129 'Psy\Test\TabCompletion\StaticSample::CO',
130 array('Psy\Test\TabCompletion\StaticSample::CONSTANT_VALUE'),
134 'Psy\Test\TabCompletion\StaticSample::',
135 array('Psy\Test\TabCompletion\StaticSample::$staticVariable'),
139 'Psy\Test\TabCompletion\StaticSample::',
140 array('Psy\Test\TabCompletion\StaticSample::staticFunction'),