Skip to content

Commit

Permalink
Finish off the current methods, docs and file headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jun 10, 2017
1 parent 4c00758 commit 8568c8d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 11 deletions.
67 changes: 66 additions & 1 deletion src/Console/CommandCollection.php
@@ -1,21 +1,59 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console;

use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;

class CommandCollection implements IteratorAggregate
/**
* Collection for Commands.
*
* Used by Applications to whitelist their console commands.
* CakePHP will use the mapped commands to construct and dispatch
* shell commands.
*/
class CommandCollection implements IteratorAggregate, Countable
{
/**
* Command list
*
* @var array
*/
protected $commands = [];

/**
* Constructor
*
* @param array $commands The map of commands to add to the collection.
*/
public function __construct(array $commands = [])
{
foreach ($commands as $name => $command) {
$this->add($name, $command);
}
}

/**
* 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.
* @return $this
*/
public function add($name, $command)
{
// Once we have a new Command class this should check
Expand All @@ -30,8 +68,17 @@ public function add($name, $command)
return $this;
}

/**
* Remove a command from the collection if it exists.
*
* @param string $name The named shell.
* @return $this
*/
public function remove($name)
{
unset($this->commands[$name]);

return $this;
}

/**
Expand All @@ -57,11 +104,29 @@ public function get($name)
if (!$this->has($name)) {
throw new InvalidArgumentException("The $name is not a known command name.");
}

return $this->commands[$name];
}

/**
* Implementation of IteratorAggregate.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->commands);
}

/**
* Implementation of Countable.
*
* Get the number of commands in the collection.
*
* @return int
*/
public function count()
{
return count($this->commands);
}
}
78 changes: 68 additions & 10 deletions tests/TestCase/Console/CommandCollectionTest.php
@@ -1,4 +1,17 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\Console;

use Cake\Console\CommandCollection;
Expand All @@ -12,19 +25,35 @@
*/
class CommandCollectionTest extends TestCase
{
/**
* Test constructor with valid classnames
*
* @return void
*/
public function testConstructor()
{
$this->markTestIncomplete();
$collection = new CommandCollection([
'i18n' => I18nShell::class,
'routes' => RoutesShell::class
]);
$this->assertTrue($collection->has('routes'));
$this->assertTrue($collection->has('i18n'));
$this->assertCount(2, $collection);
}

/**
* Constructor with invalid class names should blow up
*
* @return void
* @expectedException InvalidArgumentException
* @expectedExceptionMessage 'nope' is not a subclass of Cake\Console\Shell
*/
public function testConstructorInvalidClass()
{
$this->markTestIncomplete();
}

public function testConstructorInvalidFactory()
{
$this->markTestIncomplete();
new CommandCollection([
'i18n' => I18nShell::class,
'nope' => stdClass::class
]);
}

/**
Expand Down Expand Up @@ -97,18 +126,47 @@ public function testInvalidShellClassName()
$collection->add('routes', stdClass::class);
}

/**
* Test removing a command
*
* @return void
*/
public function testRemove()
{
$this->markTestIncomplete();
$collection = new CommandCollection();
$collection->add('routes', RoutesShell::class);
$this->assertSame($collection, $collection->remove('routes'));
$this->assertFalse($collection->has('routes'));
}

/**
* Removing an unknown command does not fail
*
* @return void
*/
public function testRemoveUnknown()
{
$this->markTestIncomplete();
$collection = new CommandCollection();
$this->assertSame($collection, $collection->remove('nope'));
$this->assertFalse($collection->has('nope'));
}

/**
* test getIterator
*
* @return void
*/
public function testGetIterator()
{
$this->markTestIncomplete();
$in = [
'i18n' => I18nShell::class,
'routes' => RoutesShell::class
];
$collection = new CommandCollection($in);
$out = [];
foreach ($collection as $key => $value) {
$out[$key] = $value;
}
$this->assertEquals($in, $out);
}
}

0 comments on commit 8568c8d

Please sign in to comment.