Commit 03d69eea authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Added some tests for CLI

parent d2b59d7a
...@@ -38,6 +38,7 @@ class Option ...@@ -38,6 +38,7 @@ class Option
private $_name; private $_name;
/** @var string Option default value */ /** @var string Option default value */
private $_defaultValue;
/** @var string Option description */ /** @var string Option description */
private $description; private $description;
...@@ -69,7 +70,7 @@ class Option ...@@ -69,7 +70,7 @@ class Option
/** /**
* Retrieves the CLI Option default value * Retrieves the CLI Option default value
* *
* @return string|null Option default value * @return mixed Option default value
*/ */
public function getDefaultValue() public function getDefaultValue()
{ {
...@@ -93,7 +94,10 @@ class Option ...@@ -93,7 +94,10 @@ class Option
*/ */
public function __toString() public function __toString()
{ {
return '--' . $this->_name $defaultValue = ( ! is_null($this->_defaultValue))
. (( ! is_null($this->_defaultValue)) ? '=' . $this->_defaultValue : ''); ? '=' . (is_array($this->_defaultValue) ? implode(',', $this->_defaultValue) : $this->_defaultValue)
: '';
return '--' . $this->_name . $defaultValue;
} }
} }
\ No newline at end of file
...@@ -467,7 +467,7 @@ class OptionGroup ...@@ -467,7 +467,7 @@ class OptionGroup
$optionStr = (string) $option; $optionStr = (string) $option;
// Format Option string // Format Option string
$str .= $printer->format($optionStr, $style); $str = $printer->format($optionStr, $style);
// Include missing spaces // Include missing spaces
$str .= str_repeat(' ', $maxOptionLength - strlen($optionStr)); $str .= str_repeat(' ', $maxOptionLength - strlen($optionStr));
......
...@@ -30,6 +30,7 @@ class AllTests ...@@ -30,6 +30,7 @@ class AllTests
$suite->addTest(Collections\AllTests::suite()); $suite->addTest(Collections\AllTests::suite());
$suite->addTest(Annotations\AllTests::suite()); $suite->addTest(Annotations\AllTests::suite());
$suite->addTest(Cache\AllTests::suite()); $suite->addTest(Cache\AllTests::suite());
$suite->addTest(Cli\AllTests::suite());
return $suite; return $suite;
} }
......
<?php
namespace Doctrine\Tests\Common\Cli;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Common_Cli_AllTests::main');
}
require_once __DIR__ . '/../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common CLI Tests');
$suite->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
<?php
namespace Doctrine\Tests\Common\Cli;
use Doctrine\Common\Cli\Printers\NormalPrinter,
Doctrine\Common\Cli\OptionGroup,
Doctrine\Common\Cli\Option;
require_once __DIR__ . '/../../TestInit.php';
class OptionGroupTest extends \Doctrine\Tests\DoctrineTestCase
{
private $_options = array();
public function setUp()
{
$this->_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
<?php
namespace Doctrine\Tests\Common\Cli;
use Doctrine\Common\Cli\Option;
require_once __DIR__ . '/../../TestInit.php';
class OptionTest extends \Doctrine\Tests\DoctrineTestCase
{
public function testGetMethods()
{
$option = new Option('name', 'value', 'Description');
$this->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
<?php
namespace Doctrine\Tests\Common\Cli;
use Doctrine\Common\Cli\Style;
require_once __DIR__ . '/../../TestInit.php';
class StyleTest extends \Doctrine\Tests\DoctrineTestCase
{
public function testGetMethods()
{
$style = new Style('BLACK', 'WHITE', array('BOLD' => true));
$this->assertEquals('BLACK', $style->getForeground());
$this->assertEquals('WHITE', $style->getBackground());
$this->assertEquals(array('BOLD' => true), $style->getOptions());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment