2010-03-18 07:48:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-03-18 07:48:03 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Console\Input;
|
2010-03-18 07:48:03 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
class InputArgumentTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testConstructor()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo');
|
|
|
|
$this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
|
|
|
|
|
|
|
|
// mode argument
|
|
|
|
$argument = new InputArgument('foo');
|
2010-11-27 15:54:50 +00:00
|
|
|
$this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', null);
|
2010-11-27 15:54:50 +00:00
|
|
|
$this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
|
2010-11-27 15:54:50 +00:00
|
|
|
$this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::REQUIRED);
|
2010-11-27 15:54:50 +00:00
|
|
|
$this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', 'ANOTHER_ONE');
|
|
|
|
$this->fail('__construct() throws an Exception if the mode is not valid');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
|
|
|
|
$this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage());
|
|
|
|
}
|
2011-08-30 12:45:17 +01:00
|
|
|
try {
|
|
|
|
$argument = new InputArgument('foo', -1);
|
|
|
|
$this->fail('__construct() throws an Exception if the mode is not valid');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
|
|
|
|
$this->assertEquals('Argument mode "-1" is not valid.', $e->getMessage());
|
|
|
|
}
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
public function testIsArray()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
|
|
|
|
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
|
|
|
|
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
|
|
|
|
$this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
public function testGetDescription()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', null, 'Some description');
|
|
|
|
$this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testGetDefault()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
|
|
|
|
$this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
public function testSetDefault()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
|
|
|
|
$argument->setDefault(null);
|
|
|
|
$this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
|
|
|
|
$argument->setDefault('another');
|
|
|
|
$this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
|
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
|
|
|
|
$argument->setDefault(array(1, 2));
|
|
|
|
$this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', InputArgument::REQUIRED);
|
|
|
|
$argument->setDefault('default');
|
|
|
|
$this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\Exception', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
|
|
|
|
$this->assertEquals('Cannot set a default value except for Parameter::OPTIONAL mode.', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
|
|
|
|
$argument->setDefault('default');
|
|
|
|
$this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
|
|
|
|
$this->assertEquals('A default value for an array argument must be an array.', $e->getMessage());
|
|
|
|
}
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
}
|