Skip to content

Commit

Permalink
Allow Command subclasses to be added to a collection.
Browse files Browse the repository at this point in the history
Allowing commands in the collection will enable developers to use
Commands in their applications.
  • Loading branch information
markstory committed Sep 30, 2017
1 parent 9e2e400 commit 3f3c88e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Console/CommandCollection.php
Expand Up @@ -15,8 +15,6 @@
namespace Cake\Console;

use ArrayIterator;
use Cake\Console\CommandScanner;
use Cake\Console\Shell;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
Expand Down Expand Up @@ -53,17 +51,17 @@ public function __construct(array $commands = [])
* Add a command to the collection
*
* @param string $name The name of the command you want to map.
* @param string|\Cake\Console\Shell $command The command to map.
* @param string|\Cake\Console\Shell|\Cake\Console\Command $command The command to map.
* @return $this
*/
public function add($name, $command)
{
// Once we have a new Command class this should check
// against that interface.
if (!is_subclass_of($command, Shell::class)) {
if (!is_subclass_of($command, Shell::class) && !is_subclass_of($command, Command::class)) {
$class = is_string($command) ? $command : get_class($command);
throw new InvalidArgumentException(
"Cannot use '$class' for command '$name' it is not a subclass of Cake\Console\Shell."
"Cannot use '$class' for command '$name' it is not a subclass of Cake\Console\Shell or Cake\Console\Command."
);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Console/CommandCollectionTest.php
Expand Up @@ -14,13 +14,15 @@
*/
namespace Cake\Test\Console;

use Cake\Console\Command;
use Cake\Console\CommandCollection;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Shell\I18nShell;
use Cake\Shell\RoutesShell;
use Cake\TestSuite\TestCase;
use stdClass;
use TestApp\Command\ExampleCommand;

/**
* Test case for the CommandCollection
Expand Down Expand Up @@ -77,6 +79,19 @@ public function testAdd()
$this->assertSame(RoutesShell::class, $collection->get('routes'));
}

/**
* test adding a command instance.
*
* @return void
*/
public function testAddCommand()
{
$collection = new CommandCollection();
$this->assertSame($collection, $collection->add('ex', ExampleCommand::class));
$this->assertTrue($collection->has('ex'));
$this->assertSame(ExampleCommand::class, $collection->get('ex'));
}

/**
* Test that add() replaces.
*
Expand Down

0 comments on commit 3f3c88e

Please sign in to comment.