9374d133a43872e3027b92a3192bda55182f3d91
[yaffs-website] / vendor / stecman / symfony-console-completion / tests / Stecman / Component / Symfony / Console / BashCompletion / CompletionContextTest.php
1 <?php
2
3 namespace Stecman\Component\Symfony\Console\BashCompletion\Tests;
4
5 use PHPUnit\Framework\TestCase;
6 use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
7 use Stecman\Component\Symfony\Console\BashCompletion\EnvironmentCompletionContext;
8
9 class CompletionContextTest extends TestCase
10 {
11
12     public function testWordBreakSplit()
13     {
14         $context = new CompletionContext();
15         $context->setCommandLine('console  config:application --direction="west" --with-bruce --repeat 3');
16
17         // Cursor at the end of the first word
18         $context->setCharIndex(7);
19         $words = $context->getWords();
20
21         $this->assertEquals(array(
22             'console',
23             'config:application',
24             '--direction',
25             'west',
26             '--with-bruce',
27             '--repeat',
28             '3'
29         ), $words);
30     }
31
32     public function testCursorPosition()
33     {
34         $context = new CompletionContext();
35         $context->setCommandLine('make horse --legs 4 --colour black ');
36
37         // Cursor at the start of the line
38         $context->setCharIndex(0);
39         $this->assertEquals(0, $context->getWordIndex());
40
41         // Cursor at the end of the line
42         $context->setCharIndex(34);
43         $this->assertEquals(5, $context->getWordIndex());
44         $this->assertEquals('black', $context->getCurrentWord());
45
46         // Cursor after space at the end of the string
47         $context->setCharIndex(35);
48         $this->assertEquals(6, $context->getWordIndex());
49         $this->assertEquals('', $context->getCurrentWord());
50
51         // Cursor in the middle of 'horse'
52         $context->setCharIndex(8);
53         $this->assertEquals(1, $context->getWordIndex());
54         $this->assertEquals('hor', $context->getCurrentWord());
55
56         // Cursor at the end of '--legs'
57         $context->setCharIndex(17);
58         $this->assertEquals(2, $context->getWordIndex());
59         $this->assertEquals('--legs', $context->getCurrentWord());
60     }
61
62     public function testWordBreakingWithSmallInputs()
63     {
64         $context = new CompletionContext();
65
66         // Cursor at the end of a word and not in the following space has no effect
67         $context->setCommandLine('cmd a');
68         $context->setCharIndex(5);
69         $this->assertEquals(array('cmd', 'a'), $context->getWords());
70         $this->assertEquals(1, $context->getWordIndex());
71         $this->assertEquals('a', $context->getCurrentWord());
72
73         // As above, but in the middle of the command line string
74         $context->setCommandLine('cmd a');
75         $context->setCharIndex(3);
76         $this->assertEquals(array('cmd', 'a'), $context->getWords());
77         $this->assertEquals(0, $context->getWordIndex());
78         $this->assertEquals('cmd', $context->getCurrentWord());
79
80         // Cursor at the end of the command line with a space appends an empty word
81         $context->setCommandLine('cmd   a ');
82         $context->setCharIndex(8);
83         $this->assertEquals(array('cmd', 'a', ''), $context->getWords());
84         $this->assertEquals(2, $context->getWordIndex());
85         $this->assertEquals('', $context->getCurrentWord());
86
87         // Cursor in break space before a word appends an empty word in that position
88         $context->setCommandLine('cmd a');
89         $context->setCharIndex(4);
90         $this->assertEquals(array('cmd', '',  'a',), $context->getWords());
91         $this->assertEquals(1, $context->getWordIndex());
92         $this->assertEquals('', $context->getCurrentWord());
93     }
94
95     public function testConfigureFromEnvironment()
96     {
97         putenv("CMDLINE_CONTENTS=beam up li");
98         putenv('CMDLINE_CURSOR_INDEX=10');
99
100         $context = new EnvironmentCompletionContext();
101
102         $this->assertEquals(
103             array(
104                 'beam',
105                 'up',
106                 'li'
107             ),
108             $context->getWords()
109         );
110
111         $this->assertEquals('li', $context->getCurrentWord());
112     }
113 }