More updates to stop using dev or alpha or beta versions.
[yaffs-website] / vendor / psy / psysh / test / TabCompletion / AutoCompleterTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test\TabCompletion;
13
14 use Psy\Command\ListCommand;
15 use Psy\Command\ShowCommand;
16 use Psy\Configuration;
17 use Psy\Context;
18 use Psy\ContextAware;
19 use Psy\TabCompletion\Matcher;
20
21 class AutoCompleterTest extends \PHPUnit\Framework\TestCase
22 {
23     /**
24      * @param string $line
25      * @param array  $mustContain
26      * @param array  $mustNotContain
27      * @dataProvider classesInput
28      */
29     public function testClassesCompletion($line, $mustContain, $mustNotContain)
30     {
31         $context = new Context();
32
33         $commands = [
34             new ShowCommand(),
35             new ListCommand(),
36         ];
37
38         $matchers = [
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),
49         ];
50
51         $config = new Configuration();
52         $tabCompletion = $config->getAutoCompleter();
53         foreach ($matchers as $matcher) {
54             if ($matcher instanceof ContextAware) {
55                 $matcher->setContext($context);
56             }
57             $tabCompletion->addMatcher($matcher);
58         }
59
60         $context->setAll(['foo' => 12, 'bar' => new \DOMDocument()]);
61
62         $code = $tabCompletion->processCallback('', 0, [
63            'line_buffer' => $line,
64            'point'       => 0,
65            'end'         => strlen($line),
66         ]);
67
68         foreach ($mustContain as $mc) {
69             $this->assertContains($mc, $code);
70         }
71
72         foreach ($mustNotContain as $mnc) {
73             $this->assertNotContains($mnc, $code);
74         }
75     }
76
77     /**
78      * TODO
79      * ====
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.
90      *
91      * @return array
92      */
93     public function classesInput()
94     {
95         return [
96             // input, must had, must not had
97             ['T_OPE', ['T_OPEN_TAG'], []],
98             ['st', ['stdClass'], []],
99             ['stdCla', ['stdClass'], []],
100             ['new s', ['stdClass'], []],
101             [
102                 'new ',
103                 ['stdClass', 'Psy\\Context', 'Psy\\Configuration'],
104                 ['require', 'array_search', 'T_OPEN_TAG', '$foo'],
105             ],
106             ['new Psy\\C', ['Context'], ['CASE_LOWER']],
107             ['\s', ['stdClass'], []],
108             ['array_', ['array_search', 'array_map', 'array_merge'], []],
109             ['$bar->', ['load'], []],
110             ['$b', ['bar'], []],
111             ['6 + $b', ['bar'], []],
112             ['$f', ['foo'], []],
113             ['l', ['ls'], []],
114             ['ls ', [], ['ls']],
115             ['sho', ['show'], []],
116             ['12 + clone $', ['foo'], []],
117             // array(
118             //   '$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']],
123             [
124                 'Psy\\',
125                 ['Context', 'TabCompletion\\Matcher\\AbstractMatcher'],
126                 ['require', 'array_search'],
127             ],
128             [
129                 'Psy\Test\TabCompletion\StaticSample::CO',
130                 ['StaticSample::CONSTANT_VALUE'],
131                 [],
132             ],
133             [
134                 'Psy\Test\TabCompletion\StaticSample::',
135                 ['StaticSample::$staticVariable'],
136                 [],
137             ],
138             [
139                 'Psy\Test\TabCompletion\StaticSample::',
140                 ['StaticSample::staticFunction'],
141                 [],
142             ],
143         ];
144     }
145 }