Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/27196a8ad68d10e999a5 to your computer and use it in GitHub Desktop.
Save doctrinebot/27196a8ad68d10e999a5 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-359 - https://github.com/doctrine/doctrine2/issues/4411
Index: lib/Doctrine/Common/Cli/CliController.php
===================================================================
--- lib/Doctrine/Common/Cli/CliController.php (revision 7203)
+++ lib/Doctrine/Common/Cli/CliController.php (working copy)
@@ -61,9 +61,9 @@
* The CLI processor of tasks
*
* @param Configuration $config
- * @param AbstractPrinter $printer CLI Output printer
+ * @param Printers\AbstractPrinter $printer CLI Output printer
*/
- public function __construct(Configuration $config, AbstractPrinter $printer = null)
+ public function __construct(Configuration $config, Printers\AbstractPrinter $printer = null)
{
$this->setPrinter($printer);
$this->setConfiguration($config);
@@ -226,7 +226,7 @@
$value = (strpos($value, ' ') !== false) ? $value : array_values(array_filter(
explode(',', $value), function ($v) { return trim($v) != ''; }
));
- $out[$key] = ( ! is_array($value) || (is_array($value) && count($value) > 1))
+ $out[$key] = ( ! is_array($value) || empty($value) || (is_array($value) && count($value) > 1))
? $value : $value[0];
}
// -k=value -abc
@@ -295,4 +295,4 @@
return $taskNamespace;
}
-}
\ No newline at end of file
+}
Index: tests/Doctrine/Tests/Common/Cli/AllTests.php
===================================================================
--- tests/Doctrine/Tests/Common/Cli/AllTests.php (revision 7203)
+++ tests/Doctrine/Tests/Common/Cli/AllTests.php (working copy)
@@ -23,6 +23,7 @@
$suite->addTestSuite('Doctrine\Tests\Common\Cli\OptionTest');
$suite->addTestSuite('Doctrine\Tests\Common\Cli\OptionGroupTest');
$suite->addTestSuite('Doctrine\Tests\Common\Cli\StyleTest');
+ $suite->addTestSuite('Doctrine\Tests\Common\Cli\CliControllerTest');
return $suite;
}
Index: tests/Doctrine/Tests/Common/Cli/CliControllerTest.php
===================================================================
--- tests/Doctrine/Tests/Common/Cli/CliControllerTest.php (revision 0)
+++ tests/Doctrine/Tests/Common/Cli/CliControllerTest.php (revision 0)
@@ -0,0 +1,115 @@
+<?php
+
+namespace Doctrine\Tests\Common\Cli;
+
+use Doctrine\Tests\Mocks\TaskMock;
+use Doctrine\Common\Cli\Configuration;
+use Doctrine\Common\Cli\CliController;
+
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ * @author Nils Adermann <naderman@naderman.de>
+ */
+class CliControllerTest extends \Doctrine\Tests\DoctrineTestCase
+{
+ private $cli;
+
+ /**
+ * Sets up a CliController instance with a task referencing the TaskMock
+ * class. Instances of that class created by the CliController can be
+ * inspected for correctness.
+ */
+ function setUp()
+ {
+ $config = $this->getMock('\Doctrine\Common\Cli\Configuration');
+ $printer = $this->getMockForAbstractClass('\Doctrine\Common\Cli\Printers\AbstractPrinter');
+
+ $this->cli = new CliController($config, $printer);
+
+ TaskMock::$instances = array();
+
+ $this->cli->addTask('task-mock', '\Doctrine\Tests\Mocks\TaskMock');
+ }
+
+ /**
+ * Data provider with a bunch of task-mock calls with different arguments
+ * and their expected parsed format.
+ */
+ static public function dataCliControllerArguments()
+ {
+ return array(
+ array(
+ array('doctrine', 'Core:task-mock', '--bool'),
+ array('bool' => true),
+ 'Bool option'
+ ),
+ array(
+ array('doctrine', 'Core:task-mock', '--option=value'),
+ array('option' => 'value'),
+ 'Option with string value'
+ ),
+ array(
+ array('doctrine', 'Core:task-mock', '--option=value, with additional, info'),
+ array('option' => 'value, with additional, info'),
+ 'Option with string value containing space and comma'
+ ),
+ array(
+ array('doctrine', 'Core:task-mock', '--option='),
+ array('option' => array()),
+ 'Empty option value'
+ ),
+ array(
+ array('doctrine', 'Core:task-mock', '--option=value1,value2,value3'),
+ array('option' => array('value1', 'value2', 'value3')),
+ 'Option with list of string values'
+ ),
+ );
+ }
+
+ /**
+ * Checks whether the arguments coming from the data provider are correctly
+ * parsed by the CliController and passed to the task to be run.
+ *
+ * @dataProvider dataCliControllerArguments
+ * @param array $rawArgs
+ * @param array $parsedArgs
+ * @param string $message
+ */
+ public function testArgumentParsing($rawArgs, $parsedArgs, $message)
+ {
+ $this->cli->run($rawArgs);
+
+ $this->assertEquals(count(TaskMock::$instances), 1);
+
+ $task = TaskMock::$instances[0];
+
+ $this->assertEquals($task->getArguments(), $parsedArgs, $message);
+ }
+
+ /**
+ * Checks whether multiple tasks in one command are correctly run with
+ * their respective options.
+ */
+ public function testMultipleTaskExecution()
+ {
+ $this->cli->run(array(
+ 'doctrine',
+ 'Core:task-mock',
+ '--option=',
+ 'Core:task-mock',
+ '--bool'
+ ));
+
+ $this->assertEquals(count(TaskMock::$instances), 2);
+
+ $task0 = TaskMock::$instances[0];
+ $task1 = TaskMock::$instances[1];
+
+ $this->assertEquals($task0->getRunCounter(), 1);
+ $this->assertEquals($task1->getRunCounter(), 1);
+
+ $this->assertEquals($task0->getArguments(), array('option' => array()));
+ $this->assertEquals($task1->getArguments(), array('bool' => true));
+ }
+}
Property changes on: tests/Doctrine/Tests/Common/Cli/CliControllerTest.php
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ LF
Index: tests/Doctrine/Tests/Mocks/TaskMock.php
===================================================================
--- tests/Doctrine/Tests/Mocks/TaskMock.php (revision 0)
+++ tests/Doctrine/Tests/Mocks/TaskMock.php (revision 0)
@@ -0,0 +1,81 @@
+<?php
+/*
+ * $Id$
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\Tests\Mocks;
+
+use Doctrine\Common\Cli\AbstractNamespace;
+
+/**
+ * TaskMock used for testing the CLI interface.
+ * @author Nils Adermann <naderman@naderman.de>
+ */
+class TaskMock extends \Doctrine\Common\Cli\Tasks\AbstractTask
+{
+ /**
+ * Since instances of this class can be created elsewhere all instances
+ * register themselves in this array for later inspection.
+ *
+ * @var array(TaskMock)
+ */
+ static public $instances = array();
+
+ private $runCounter = 0;
+
+ /**
+ * Constructor of Task Mock Object.
+ * Makes sure the object can be inspected later.
+ *
+ * @param AbstractNamespace CLI Namespace, passed to parent constructor
+ */
+ function __construct(AbstractNamespace $namespace)
+ {
+ self::$instances[] = $this;
+
+ parent::__construct($namespace);
+ }
+
+ /**
+ * Returns the number of times run() was called on this object.
+ *
+ * @return int
+ */
+ public function getRunCounter()
+ {
+ return $this->runCounter;
+ }
+
+ /* Mock API */
+
+ /**
+ * Method invoked by CLI to run task.
+ */
+ public function run()
+ {
+ $this->runCounter++;
+ }
+
+ /**
+ * Method supposed to generate the CLI Task Documentation
+ */
+ public function buildDocumentation()
+ {
+ }
+}
Property changes on: tests/Doctrine/Tests/Mocks/TaskMock.php
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ LF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment