-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConsoleTest.php
More file actions
189 lines (150 loc) · 5.41 KB
/
ConsoleTest.php
File metadata and controls
189 lines (150 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\CLI;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\DotEnv;
use CodeIgniter\Config\Services;
use CodeIgniter\Events\Events;
use CodeIgniter\Superglobals;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockCLIConfig;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class ConsoleTest extends CIUnitTestCase
{
use StreamFilterTrait;
protected function setUp(): void
{
parent::setUp();
Services::injectMock('superglobals', new Superglobals());
CLI::init();
(new DotEnv(ROOTPATH))->load();
$this->app = new MockCodeIgniter(new MockCLIConfig());
$this->app->initialize();
}
protected function tearDown(): void
{
parent::tearDown();
CLI::reset();
}
public function testHeaderShowsNormally(): void
{
$this->initializeConsole();
(new Console())->run();
$this->assertStringContainsString(
sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION),
$this->getStreamFilterBuffer(),
);
}
public function testHeaderDoesNotShowOnNoHeader(): void
{
$this->initializeConsole('--no-header');
(new Console())->run();
$this->assertStringNotContainsString(
sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION),
$this->getStreamFilterBuffer(),
);
}
public function testRun(): void
{
$this->initializeConsole();
(new Console())->run();
$this->assertStringContainsString('Lists the available commands.', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
}
public function testRunEventsPreCommand(): void
{
$result = '';
Events::on('pre_command', static function () use (&$result): void {
$result = 'fired';
});
$this->initializeConsole();
(new Console())->run();
$this->assertEventTriggered('pre_command');
$this->assertSame('fired', $result);
}
public function testRunEventsPostCommand(): void
{
$result = '';
Events::on('post_command', static function () use (&$result): void {
$result = 'fired';
});
$this->initializeConsole();
(new Console())->run();
$this->assertEventTriggered('post_command');
$this->assertSame('fired', $result);
}
public function testBadCommand(): void
{
$this->initializeConsole('bogus');
(new Console())->run();
$this->assertStringContainsString('Command "bogus" not found', $this->getStreamFilterBuffer());
}
public function testHelpCommandDetails(): void
{
$this->initializeConsole('help', 'make:migration');
(new Console())->run();
$this->assertStringContainsString('Description:', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Usage:', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Options:', $this->getStreamFilterBuffer());
}
public function testHelpCommandUsingHelpOption(): void
{
$this->initializeConsole('env', '--help');
(new Console())->run();
$this->assertStringContainsString('env [<environment>]', $this->getStreamFilterBuffer());
$this->assertStringContainsString(
'Retrieves the current environment, or set a new one.',
$this->getStreamFilterBuffer(),
);
}
public function testHelpOptionIsOnlyPassed(): void
{
$this->initializeConsole('--help');
(new Console())->run();
// Since calling `php spark` is the same as calling `php spark list`,
// `php spark --help` should be the same as `php spark list --help`
$this->assertStringContainsString('Lists the available commands.', $this->getStreamFilterBuffer());
}
public function testHelpArgumentAndHelpOptionCombined(): void
{
$this->initializeConsole('help', '--help');
(new Console())->run();
// Same as calling `php spark help` only
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
}
public function testConsoleReturnsTheLastExecutedCommand(): void
{
$console = new Console();
$this->assertSame('', $console->getCommand());
$this->initializeConsole();
$console->run();
$this->assertSame(Console::DEFAULT_COMMAND, $console->getCommand());
$this->initializeConsole('help');
$console->run();
$this->assertSame('help', $console->getCommand());
$this->initializeConsole('list');
$console->run();
$this->assertSame('list', $console->getCommand());
}
private function initializeConsole(string ...$tokens): void
{
service('superglobals')
->setServer('argv', ['spark', ...$tokens])
->setServer('argc', count($tokens) + 1);
CLI::init();
}
}