From c41a08a6bef8e1c45942b06b66ef210b405abd6b Mon Sep 17 00:00:00 2001 From: jwage Date: Thu, 18 Mar 2010 21:48:04 +0000 Subject: [PATCH] [2.0][DDC-359] Adding some tests for CliController and fixing bug with empty option value .i.e "--option=" --- lib/Doctrine/Common/Cli/CliController.php | 2 +- tests/Doctrine/Tests/Common/Cli/AllTests.php | 1 + .../Tests/Common/Cli/CliControllerTest.php | 115 ++++++++++++++++++ tests/Doctrine/Tests/Mocks/TaskMock.php | 81 ++++++++++++ 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/Common/Cli/CliControllerTest.php create mode 100644 tests/Doctrine/Tests/Mocks/TaskMock.php diff --git a/lib/Doctrine/Common/Cli/CliController.php b/lib/Doctrine/Common/Cli/CliController.php index 067587fc9..e78e26bba 100644 --- a/lib/Doctrine/Common/Cli/CliController.php +++ b/lib/Doctrine/Common/Cli/CliController.php @@ -233,7 +233,7 @@ class CliController extends AbstractNamespace $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 diff --git a/tests/Doctrine/Tests/Common/Cli/AllTests.php b/tests/Doctrine/Tests/Common/Cli/AllTests.php index da6b6b5f9..852bba32d 100644 --- a/tests/Doctrine/Tests/Common/Cli/AllTests.php +++ b/tests/Doctrine/Tests/Common/Cli/AllTests.php @@ -23,6 +23,7 @@ class AllTests $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; } diff --git a/tests/Doctrine/Tests/Common/Cli/CliControllerTest.php b/tests/Doctrine/Tests/Common/Cli/CliControllerTest.php new file mode 100644 index 000000000..d9ce8acc8 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cli/CliControllerTest.php @@ -0,0 +1,115 @@ + + */ +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)); + } +} diff --git a/tests/Doctrine/Tests/Mocks/TaskMock.php b/tests/Doctrine/Tests/Mocks/TaskMock.php new file mode 100644 index 000000000..d4d50689d --- /dev/null +++ b/tests/Doctrine/Tests/Mocks/TaskMock.php @@ -0,0 +1,81 @@ +. + */ + +namespace Doctrine\Tests\Mocks; + +use Doctrine\Common\Cli\AbstractNamespace; + +/** + * TaskMock used for testing the CLI interface. + * @author Nils Adermann + */ +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() + { + } +}