-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathApplicationTest.php
More file actions
210 lines (170 loc) · 5.65 KB
/
Copy pathApplicationTest.php
File metadata and controls
210 lines (170 loc) · 5.65 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\ConsoleTest;
use Inhere\Console\Application;
use Inhere\Console\Console;
use Inhere\Console\IO\Input;
use Inhere\Console\Contract\InputInterface;
use Inhere\Console\Component\Router;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Throwable;
use function get_class;
use function strpos;
/**
* Class ApplicationTest
*
* @package Inhere\ConsoleTest
*/
class ApplicationTest extends TestCase
{
protected function assertStringContains(string $string, string $contains): void
{
self::assertNotFalse(strpos($string, $contains), "string \"$string\" not contains: $contains");
}
private function newApp(?array $args = null): Application
{
$input = new Input($args);
return new Application([
'name' => 'Tests',
'debug' => 1,
'version' => '1.0.0',
], $input);
}
public function testApp(): void
{
$app = Console::newApp([
'name' => 'Tests',
]);
self::assertArrayHasKey('name', $app->getConfig());
self::assertEquals('Tests', $app->getName());
self::assertEquals('Tests', $app->getParam('name'));
self::assertInstanceOf(InputInterface::class, $app->getInput());
}
public function testAddCommand(): void
{
$app = $this->newApp();
$app->addCommand('test', function () {
return 0;
});
$router = $app->getRouter();
self::assertTrue($router->isCommand('test'));
self::assertFalse($router->isController('test'));
self::assertArrayHasKey('test', $router->getCommands());
self::assertContains('test', $router->getCommandNames());
}
public function testAddCommandError(): void
{
$app = $this->newApp();
try {
$app->addCommand('');
} catch (Throwable $e) {
self::assertSame(get_class($e), InvalidArgumentException::class);
}
try {
$app->addCommand('test', 'invalid');
} catch (Throwable $e) {
self::assertSame(get_class($e), InvalidArgumentException::class);
self::assertSame($e->getMessage(), "The console command class 'invalid' not exists!");
}
}
public function testAddCommand_class_run(): void
{
$app = $this->newApp([
'./app',
'test1'
]);
$app->addCommand(TestCommand::class);
$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestCommand::execute', $ret);
}
public function testAddCommand_callback_run(): void
{
$app = $this->newApp([
'./app',
'test'
]);
$app->addCommand('test', function () {
return 'hello';
});
$ret = $app->run(false);
self::assertSame('hello', $ret);
}
public function testAddCommand_object_run(): void
{
$app = $this->newApp([
'./app',
'test'
]);
$app->command('test', new TestCommand());
$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestCommand::execute', $ret);
}
public function testAddController(): void
{
$app = $this->newApp();
$app->addGroup('test', TestController::class);
$router = $app->getRouter();
self::assertTrue($app->getRouter()->isController('test'));
self::assertFalse($app->getRouter()->isCommand('test'));
self::assertArrayHasKey('test', $router->getControllers());
$group = $router->getControllers()['test'];
self::assertSame(TestController::class, $group['handler']);
self::assertSame(Router::TYPE_GROUP, $group['type']);
}
public function testAddControllerError(): void
{
$app = $this->newApp();
try {
$app->addController('');
} catch (Throwable $e) {
self::assertSame(get_class($e), InvalidArgumentException::class);
$this->assertStringContains($e->getMessage(), '"name" and "controller" cannot be empty');
}
try {
$app->controller('test', 'invalid');
} catch (Throwable $e) {
self::assertSame(get_class($e), InvalidArgumentException::class);
self::assertSame($e->getMessage(), 'The console controller class [invalid] not exists!');
}
}
public function testAdd_Controller_class_Run(): void
{
$app = $this->newApp([
'./app',
'test:demo'
]);
$app->controller('test', TestController::class);
$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestController::demoCommand', $ret);
}
public function testAdd_Controller_object_Run(): void
{
$app = $this->newApp([
'./app',
'test:demo'
]);
$app->controller('test', new TestController);
$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestController::demoCommand', $ret);
}
public function testTriggerEvent(): void
{
$app = $this->newApp([
'./app',
'test1'
]);
$app->on(Application::ON_BEFORE_RUN, function (Application $app): void {
$this->assertEquals('Tests', $app->getName());
});
$app->addCommand('test1', TestCommand::class);
$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestCommand::execute', $ret);
}
}