[2.0] Added some tests for CLI
This commit is contained in:
parent
d2b59d7a72
commit
03d69eea77
@ -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
|
|||||||
/**
|
/**
|
||||||
* 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
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
|||||||
$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;
|
||||||
}
|
}
|
||||||
|
32
tests/Doctrine/Tests/Common/Cli/AllTests.php
Normal file
32
tests/Doctrine/Tests/Common/Cli/AllTests.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?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();
|
||||||
|
}
|
123
tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php
Normal file
123
tests/Doctrine/Tests/Common/Cli/OptionGroupTest.php
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
<?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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
40
tests/Doctrine/Tests/Common/Cli/OptionTest.php
Normal file
40
tests/Doctrine/Tests/Common/Cli/OptionTest.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
19
tests/Doctrine/Tests/Common/Cli/StyleTest.php
Normal file
19
tests/Doctrine/Tests/Common/Cli/StyleTest.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user