forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleTest.php
183 lines (143 loc) · 4.96 KB
/
ConsoleTest.php
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockCLIConfig;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use CodeIgniter\Test\StreamFilterTrait;
/**
* @internal
*
* @group Others
*/
final class ConsoleTest extends CIUnitTestCase
{
use StreamFilterTrait;
private DotEnv $env;
protected function setUp(): void
{
parent::setUp();
$this->env = new DotEnv(ROOTPATH);
$this->env->load();
// Set environment values that would otherwise stop the framework from functioning during tests.
if (! isset($_SERVER['app.baseURL'])) {
$_SERVER['app.baseURL'] = 'http://example.com/';
}
$this->app = new MockCodeIgniter(new MockCLIConfig());
$this->app->initialize();
}
public function testHeader(): void
{
$console = new Console();
$console->showHeader();
$this->assertGreaterThan(
0,
strpos(
$this->getStreamFilterBuffer(),
sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION)
)
);
}
public function testNoHeader(): void
{
$console = new Console();
$console->showHeader(true);
$this->assertSame('', $this->getStreamFilterBuffer());
}
public function testRun(): void
{
$this->initCLI();
$console = new Console();
$console->run();
// make sure the result looks like a command list
$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->initCLI();
$console = new Console();
$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->initCLI();
$console = new Console();
$console->run();
$this->assertEventTriggered('post_command');
$this->assertSame('fired', $result);
}
public function testBadCommand(): void
{
$this->initCLI('bogus');
$console = new Console();
$console->run();
// make sure the result looks like a command list
$this->assertStringContainsString('Command "bogus" not found', $this->getStreamFilterBuffer());
}
public function testHelpCommandDetails(): void
{
$this->initCLI('help', 'make:migration');
$console = new Console();
$console->run();
// make sure the result looks like more detailed help
$this->assertStringContainsString('Description:', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Usage:', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Options:', $this->getStreamFilterBuffer());
}
public function testHelpCommandUsingHelpOption(): void
{
$this->initCLI('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->initCLI('--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->initCLI('help', '--help');
(new Console())->run();
// Same as calling `php spark help` only
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
}
/**
* @param array $command
*/
protected function initCLI(...$command): void
{
$_SERVER['argv'] = ['spark', ...$command];
$_SERVER['argc'] = count($_SERVER['argv']);
CLI::init();
}
}