Browse Source

Finish off the current methods, docs and file headers.

Mark Story 8 years ago
parent
commit
8568c8d3b8
2 changed files with 134 additions and 11 deletions
  1. 66 1
      src/Console/CommandCollection.php
  2. 68 10
      tests/TestCase/Console/CommandCollectionTest.php

+ 66 - 1
src/Console/CommandCollection.php

@@ -1,14 +1,45 @@
 <?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) {
@@ -16,6 +47,13 @@ class CommandCollection implements IteratorAggregate
         }
     }
 
+    /**
+     * 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
@@ -30,8 +68,17 @@ class CommandCollection implements IteratorAggregate
         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;
     }
 
     /**
@@ -57,11 +104,29 @@ class CommandCollection implements IteratorAggregate
         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);
+    }
 }

+ 68 - 10
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;
@@ -12,19 +25,35 @@ use stdClass;
  */
 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
+        ]);
     }
 
     /**
@@ -97,18 +126,47 @@ class CommandCollectionTest extends TestCase
         $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);
     }
 }