Skip to content

Commit 59114a3

Browse files
committed
add some test
1 parent f162bae commit 59114a3

9 files changed

+105
-17
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"autoload-dev": {
3434
"psr-4": {
35-
"Inhere\\Console\\Test\\": "test/"
35+
"Inhere\\ConsoleTest\\": "test/"
3636
}
3737
},
3838
"suggest": {

phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
1010
stopOnFailure="false"
11-
syntaxCheck="false"
1211
>
1312
<testsuites>
1413
<testsuite name="Php Console Test Suite">

src/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function controller(string $name, $class = null, $option = null)
3939

4040
if (!$name || !$class) {
4141
Helper::throwInvalidArgument(
42-
'Group-command "name" and "controller" not allowed to is empty! name: %s, controller: %s',
42+
'Group-command "name" and "controller" cannot be empty! name: %s, controller: %s',
4343
$name,
4444
$class
4545
);

src/Base/AbstractApplication.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ abstract class AbstractApplication implements ApplicationInterface
5656
'updateAt' => '2017.03.24',
5757
'rootPath' => '',
5858
'hideRootPath' => true,
59+
5960
// 'timeZone' => 'Asia/Shanghai',
6061
// 'env' => 'pdt', // dev test pdt
6162
// 'charset' => 'UTF-8',
@@ -153,7 +154,7 @@ protected function beforeRun()
153154
*/
154155
public function run(bool $exit = true)
155156
{
156-
$command = trim($this->input->getCommand(), $this->delimiter);
157+
$command = \trim($this->input->getCommand(), $this->delimiter);
157158

158159
$this->prepareRun();
159160
$this->filterSpecialCommand($command);
@@ -749,14 +750,22 @@ public function getName(): string
749750
return $this->meta['name'];
750751
}
751752

753+
/**
754+
* @return string
755+
*/
756+
public function getVersion(): string
757+
{
758+
return $this->meta['version'];
759+
}
760+
752761
/**
753762
* set meta info
754763
* @param array $meta
755764
*/
756765
public function setMeta(array $meta)
757766
{
758767
if ($meta) {
759-
$this->meta = array_merge($this->meta, $meta);
768+
$this->meta = \array_merge($this->meta, $meta);
760769
}
761770
}
762771

@@ -862,10 +871,6 @@ public function setCommandMetaValue(string $command, string $key, $value)
862871
*/
863872
public function getCommandMetaValue(string $command, string $key, $default = null)
864873
{
865-
if (isset($this->commandsMeta[$command][$key])) {
866-
return $this->commandsMeta[$command][$key];
867-
}
868-
869-
return $default;
874+
return $this->commandsMeta[$command][$key] ?? $default;
870875
}
871876
}

test/ApplicationTest.php

+55-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,77 @@
11
<?php
22

3+
namespace Inhere\ConsoleTest;
4+
5+
use Inhere\Console\Application;
36
use Inhere\Console\IO\InputInterface;
4-
use Inhere\Console\IO\OutputInterface;
57
use PHPUnit\Framework\TestCase;
6-
use Inhere\Console\Application;
78

89
/**
910
* @covers Application
1011
*/
1112
class ApplicationTest extends TestCase
1213
{
14+
private function newApp()
15+
{
16+
return new Application([
17+
'name' => 'Tests',
18+
'debug' => 1,
19+
'version' => '1.0.0',
20+
]);
21+
}
22+
1323
public function testApp()
1424
{
1525
$app = new Application([
16-
'name' => 'Tests',
17-
'debug' => 1,
26+
'name' => 'Tests',
27+
'debug' => 1,
1828
'version' => '1.0.0',
1929
]);
2030

2131
$this->assertArrayHasKey('name', $app->getMeta());
32+
$this->assertEquals('Tests', $app->getName());
2233
$this->assertEquals('Tests', $app->getMeta('name'));
2334

2435
$this->assertInstanceOf(InputInterface::class, $app->getInput());
25-
$this->assertInstanceOf(OutputInterface::class, $app->getOutput());
36+
}
37+
38+
public function testAddCommand()
39+
{
40+
$app = $this->newApp();
41+
42+
$app->addCommand('test', function (){
43+
return 0;
44+
});
45+
46+
$this->assertTrue($app->isCommand('test'));
47+
$this->assertFalse($app->isController('test'));
48+
$this->assertArrayHasKey('test', $app->getCommands());
49+
$this->assertContains('test', $app->getCommandNames());
50+
}
51+
52+
public function testAddController()
53+
{
54+
$app = $this->newApp();
55+
56+
$app->addGroup('test', TestController::class);
57+
58+
$this->assertTrue($app->isController('test'));
59+
$this->assertFalse($app->isCommand('test'));
60+
$this->assertArrayHasKey('test', $app->getControllers());
61+
$this->assertContains('test', $app->getControllerNames());
62+
$this->assertSame(TestController::class, $app->getControllers()['test']);
63+
}
64+
65+
public function testAddControllerError()
66+
{
67+
$app = $this->newApp();
68+
69+
$this->expectException(\InvalidArgumentException::class);
70+
$this->expectExceptionMessageRegExp('/"name" and "controller" cannot be empty/');
71+
$app->addController('');
72+
73+
$this->expectException(\InvalidArgumentException::class);
74+
$this->expectExceptionMessageRegExp('/"name" and "controller" cannot be empty/');
75+
$app->controller('test', 'invalid');
2676
}
2777
}

test/CommandTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Inhere\ConsoleTest;
4+
35
use PHPUnit\Framework\TestCase;
46
use Inhere\Console\Command;
57

test/ControllerTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Inhere\ConsoleTest;
4+
35
use PHPUnit\Framework\TestCase;
46
use Inhere\Console\Controller;
57

test/TestController.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-12-30
6+
* Time: 18:58
7+
*/
8+
9+
namespace Inhere\ConsoleTest;
10+
11+
use Inhere\Console\Controller;
12+
13+
/**
14+
* Class TestController
15+
* @package Inhere\ConsoleTest
16+
*/
17+
class TestController extends Controller
18+
{
19+
protected static $name = 'test';
20+
protected static $description = 'test description message';
21+
22+
/**
23+
* this is an demo command in test
24+
* @return int
25+
*/
26+
public function demoCommand(): int
27+
{
28+
return 0;
29+
}
30+
}

test/boot.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\Examples\\')));
1515
$file = dirname(__DIR__) . "/examples/{$path}.php";
1616

17-
} elseif (0 === strpos($class,'Inhere\Console\Test\\')) {
18-
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\Test\\')));
17+
} elseif (0 === strpos($class,'Inhere\ConsoleTest\\')) {
18+
$path = str_replace('\\', '/', substr($class, strlen('Inhere\ConsoleTest\\')));
1919
$file = __DIR__ . "/{$path}.php";
2020
} elseif (0 === strpos($class,'Inhere\Console\\')) {
2121
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\\')));

0 commit comments

Comments
 (0)