Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / TabCompletion / AutoCompleterTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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 $line
25      * @param $mustContain
26      * @param $mustNotContain
27      * @dataProvider classesInput
28      */
29     public function testClassesCompletion($line, $mustContain, $mustNotContain)
30     {
31         $context = new Context();
32
33         $commands = array(
34             new ShowCommand(),
35             new ListCommand(),
36         );
37
38         $matchers = array(
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(array('foo' => 12, 'bar' => new \DOMDocument()));
61
62         $code = $tabCompletion->processCallback('', 0, array(
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 array(
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()),
101             array(
102                 'new ',
103                 array('stdClass', 'Psy\\Context', 'Psy\\Configuration'),
104                 array('require', 'array_search', 'T_OPEN_TAG', '$foo'),
105             ),
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()),
117             // array(
118             //   '$foo ',
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')),
123             array(
124                 'Psy\\',
125                 array('Context', 'TabCompletion\\Matcher\\AbstractMatcher'),
126                 array('require', 'array_search'),
127             ),
128             array(
129                 'Psy\Test\TabCompletion\StaticSample::CO',
130                 array('Psy\Test\TabCompletion\StaticSample::CONSTANT_VALUE'),
131                 array(),
132             ),
133             array(
134                 'Psy\Test\TabCompletion\StaticSample::',
135                 array('Psy\Test\TabCompletion\StaticSample::$staticVariable'),
136                 array(),
137             ),
138             array(
139                 'Psy\Test\TabCompletion\StaticSample::',
140                 array('Psy\Test\TabCompletion\StaticSample::staticFunction'),
141                 array(),
142             ),
143         );
144     }
145 }