From 03d69eea77884ecfa065ea57480781fe67953979 Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Sat, 9 Jan 2010 21:25:51 +0000 Subject: [PATCH] [2.0] Added some tests for CLI --- lib/Doctrine/Common/Cli/Option.php | 10 +- lib/Doctrine/Common/Cli/OptionGroup.php | 2 +- tests/Doctrine/Tests/Common/AllTests.php | 1 + tests/Doctrine/Tests/Common/Cli/AllTests.php | 32 +++++ .../Tests/Common/Cli/OptionGroupTest.php | 123 ++++++++++++++++++ .../Doctrine/Tests/Common/Cli/OptionTest.php | 40 ++++++ tests/Doctrine/Tests/Common/Cli/StyleTest.php | 19 +++ 7 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/Doctrine/Tests/Common/Cli/AllTests.php create mode 100644 tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php create mode 100644 tests/Doctrine/Tests/Common/Cli/OptionTest.php create mode 100644 tests/Doctrine/Tests/Common/Cli/StyleTest.php diff --git a/lib/Doctrine/Common/Cli/Option.php b/lib/Doctrine/Common/Cli/Option.php index 8382aae39..5096898fe 100644 --- a/lib/Doctrine/Common/Cli/Option.php +++ b/lib/Doctrine/Common/Cli/Option.php @@ -38,6 +38,7 @@ class Option private $_name; /** @var string Option default value */ + private $_defaultValue; /** @var string Option description */ private $description; @@ -69,7 +70,7 @@ class Option /** * Retrieves the CLI Option default value * - * @return string|null Option default value + * @return mixed Option default value */ public function getDefaultValue() { @@ -93,7 +94,10 @@ class Option */ public function __toString() { - return '--' . $this->_name - . (( ! is_null($this->_defaultValue)) ? '=' . $this->_defaultValue : ''); + $defaultValue = ( ! is_null($this->_defaultValue)) + ? '=' . (is_array($this->_defaultValue) ? implode(',', $this->_defaultValue) : $this->_defaultValue) + : ''; + + return '--' . $this->_name . $defaultValue; } } \ No newline at end of file diff --git a/lib/Doctrine/Common/Cli/OptionGroup.php b/lib/Doctrine/Common/Cli/OptionGroup.php index ce536b698..1f99924a8 100644 --- a/lib/Doctrine/Common/Cli/OptionGroup.php +++ b/lib/Doctrine/Common/Cli/OptionGroup.php @@ -467,7 +467,7 @@ class OptionGroup $optionStr = (string) $option; // Format Option string - $str .= $printer->format($optionStr, $style); + $str = $printer->format($optionStr, $style); // Include missing spaces $str .= str_repeat(' ', $maxOptionLength - strlen($optionStr)); diff --git a/tests/Doctrine/Tests/Common/AllTests.php b/tests/Doctrine/Tests/Common/AllTests.php index e78183d71..d5fd466da 100644 --- a/tests/Doctrine/Tests/Common/AllTests.php +++ b/tests/Doctrine/Tests/Common/AllTests.php @@ -30,6 +30,7 @@ class AllTests $suite->addTest(Collections\AllTests::suite()); $suite->addTest(Annotations\AllTests::suite()); $suite->addTest(Cache\AllTests::suite()); + $suite->addTest(Cli\AllTests::suite()); return $suite; } diff --git a/tests/Doctrine/Tests/Common/Cli/AllTests.php b/tests/Doctrine/Tests/Common/Cli/AllTests.php new file mode 100644 index 000000000..0f3a4850a --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cli/AllTests.php @@ -0,0 +1,32 @@ +addTestSuite('Doctrine\Tests\Common\Cli\OptionTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cli\OptionGroupTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cli\StyleTest'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'Common_Cli_AllTests::main') { + AllTests::main(); +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php b/tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php new file mode 100644 index 000000000..a91b3ebd4 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php @@ -0,0 +1,123 @@ +_printer = new NormalPrinter(); + + $this->_options[0] = new Option('name', null, 'First option description'); + $this->_options[1] = new Option('another-name', 'value', 'Second option description'); + $this->_options[2] = new Option('third-name', array('value1', 'value2'), 'Third option description'); + } + + public function testCommonFunctionality() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_0_N, $this->_options); + + $this->assertEquals(3, count($optionGroup->getOptions())); + + $this->assertEquals( + '--name First option description' . PHP_EOL . PHP_EOL . + '--another-name=value Second option description' . PHP_EOL . PHP_EOL . + '--third-name=value1,value2 Third option description' . PHP_EOL . PHP_EOL, + $optionGroup->formatWithDescription($this->_printer) + ); + + $optionGroup->clear(); + + $this->assertEquals(0, count($optionGroup->getOptions())); + $this->assertEquals('', $optionGroup->formatPlain($this->_printer)); + $this->assertEquals( + 'No available options' . PHP_EOL . PHP_EOL, + $optionGroup->formatWithDescription($this->_printer) + ); + + $optionGroup->addOption($this->_options[0]); + $optionGroup->addOption($this->_options[1]); + + $this->assertEquals(2, count($optionGroup->getOptions())); + } + + public function testCardinality0toN() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_0_N, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_0_N, $optionGroup->getCardinality()); + + $this->assertEquals( + '[--name] [--another-name=value] [--third-name=value1,value2]', + $optionGroup->formatPlain($this->_printer) + ); + } + + public function testCardinality0to1() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_0_1, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_0_1, $optionGroup->getCardinality()); + + $this->assertEquals( + '[--name | --another-name=value | --third-name=value1,value2]', + $optionGroup->formatPlain($this->_printer) + ); + } + + public function testCardinality1to1() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_1_1, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_1_1, $optionGroup->getCardinality()); + + $this->assertEquals( + '(--name | --another-name=value | --third-name=value1,value2)', + $optionGroup->formatPlain($this->_printer) + ); + } + + public function testCardinality1toN() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_1_N, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_1_N, $optionGroup->getCardinality()); + + $this->assertEquals( + '(--name --another-name=value --third-name=value1,value2)', + $optionGroup->formatPlain($this->_printer) + ); + } + + public function testCardinalityNtoN() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_N_N, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_N_N, $optionGroup->getCardinality()); + + $this->assertEquals( + '--name --another-name=value --third-name=value1,value2', + $optionGroup->formatPlain($this->_printer) + ); + } + + public function testCardinalityMtoN() + { + $optionGroup = new OptionGroup(OptionGroup::CARDINALITY_M_N, $this->_options); + + $this->assertEquals(OptionGroup::CARDINALITY_M_N, $optionGroup->getCardinality()); + + $this->assertEquals( + '--name --another-name=value --third-name=value1,value2', + $optionGroup->formatPlain($this->_printer) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cli/OptionTest.php b/tests/Doctrine/Tests/Common/Cli/OptionTest.php new file mode 100644 index 000000000..cbeff4b82 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cli/OptionTest.php @@ -0,0 +1,40 @@ +assertEquals('name', $option->getName()); + $this->assertEquals('value', $option->getDefaultValue()); + $this->assertEquals('Description', $option->getDescription()); + } + + public function testStringCastWithDefaultValue() + { + $option = new Option('name', 'value', 'Description'); + + $this->assertEquals('--name=value', (string) $option); + } + + public function testStringCastWithoutDefaultValue() + { + $option = new Option('name', null, 'Description'); + + $this->assertEquals('--name', (string) $option); + } + + public function testStringCastWithArrayDefaultValue() + { + $option = new Option('name', array('value1', 'value2'), 'Description'); + + $this->assertEquals('--name=value1,value2', (string) $option); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cli/StyleTest.php b/tests/Doctrine/Tests/Common/Cli/StyleTest.php new file mode 100644 index 000000000..730cd1bb0 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cli/StyleTest.php @@ -0,0 +1,19 @@ + true)); + + $this->assertEquals('BLACK', $style->getForeground()); + $this->assertEquals('WHITE', $style->getBackground()); + $this->assertEquals(array('BOLD' => true), $style->getOptions()); + } +} \ No newline at end of file