Skip to content

Commit da3b9a0

Browse files
committed
Enhance CommandRunnerTest
1 parent 6870339 commit da3b9a0

4 files changed

Lines changed: 76 additions & 5 deletions

File tree

system/CLI/CommandRunner.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\CLI;
1+
<?php
2+
namespace CodeIgniter\CLI;
23

34
/**
45
* CodeIgniter
@@ -39,6 +40,7 @@
3940

4041
class CommandRunner extends Controller
4142
{
43+
4244
/**
4345
* Stores the info about found Commands.
4446
*
@@ -73,7 +75,6 @@ public function _remap($method, ...$params)
7375

7476
//--------------------------------------------------------------------
7577

76-
7778
/**
7879
* @param array $params
7980
*
@@ -143,12 +144,17 @@ protected function createCommandList()
143144
foreach ($files as $file)
144145
{
145146
$className = service('locator')->findQualifiedNameFromPath($file);
146-
147147
if (empty($className) || ! class_exists($className))
148148
{
149149
continue;
150150
}
151151

152+
$class = new \ReflectionClass($className);
153+
if ( ! $class->isInstantiable() || ! $class->isSubclassOf(BaseCommand::class))
154+
{
155+
continue;
156+
}
157+
152158
$class = new $className($this->logger, $this);
153159

154160
// Store it!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Tests\Support\Commands;
3+
4+
use CodeIgniter\CLI\BaseCommand;
5+
use CodeIgniter\CLI\CLI;
6+
use CodeIgniter\CodeIgniter;
7+
8+
abstract class AbstractInfo extends BaseCommand
9+
{
10+
11+
protected $group = 'demo';
12+
protected $name = 'app:pablo';
13+
protected $description = 'Displays basic application information.';
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Tests\Support\Commands;
3+
4+
use CodeIgniter\CLI\BaseCommand;
5+
use CodeIgniter\CLI\CLI;
6+
use CodeIgniter\CodeIgniter;
7+
8+
class AppInfo extends BaseCommand
9+
{
10+
11+
protected $group = 'demo';
12+
protected $name = 'app:info';
13+
protected $description = 'Displays basic application information.';
14+
15+
public function run(array $params)
16+
{
17+
CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red'));
18+
}
19+
20+
}

tests/system/Commands/CommandsTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Commands;
1+
<?php
2+
namespace CodeIgniter\Commands;
23

34
use Config\Services;
45
use Tests\Support\Config\MockAppConfig;
@@ -19,7 +20,6 @@ class CommandsTest extends \CIUnitTestCase
1920
protected $logger;
2021
protected $runner;
2122

22-
2323
public function setUp()
2424
{
2525
parent::setUp();
@@ -73,4 +73,34 @@ public function testListCommands()
7373
$this->assertContains('Displays basic usage information.', $result);
7474
}
7575

76+
public function testCustomCommand()
77+
{
78+
$this->runner->index(['app:info']);
79+
$result = CITestStreamFilter::$buffer;
80+
81+
$this->assertContains('CI Version:', $result);
82+
}
83+
84+
public function testNonexistantCommand()
85+
{
86+
// catch errors too
87+
$this->stream_filter = stream_filter_append(STDERR, 'CITestStreamFilter');
88+
89+
$this->runner->index(['app:oops']);
90+
$result = CITestStreamFilter::$buffer;
91+
92+
$this->assertContains('not found', $result);
93+
}
94+
95+
public function testAbstractCommand()
96+
{
97+
// catch errors too
98+
$this->stream_filter = stream_filter_append(STDERR, 'CITestStreamFilter');
99+
100+
$this->runner->index(['app:pablo']);
101+
$result = CITestStreamFilter::$buffer;
102+
103+
$this->assertContains('not found', $result);
104+
}
105+
76106
}

0 commit comments

Comments
 (0)