4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Input;
14 use Symfony\Component\Console\Input\InputArgument;
16 class InputArgumentTest extends \PHPUnit_Framework_TestCase
18 public function testConstructor()
20 $argument = new InputArgument('foo');
21 $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
24 $argument = new InputArgument('foo');
25 $this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
27 $argument = new InputArgument('foo', null);
28 $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
30 $argument = new InputArgument('foo', InputArgument::OPTIONAL);
31 $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
33 $argument = new InputArgument('foo', InputArgument::REQUIRED);
34 $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
37 $argument = new InputArgument('foo', 'ANOTHER_ONE');
38 $this->fail('__construct() throws an Exception if the mode is not valid');
39 } catch (\Exception $e) {
40 $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
41 $this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage());
44 $argument = new InputArgument('foo', -1);
45 $this->fail('__construct() throws an Exception if the mode is not valid');
46 } catch (\Exception $e) {
47 $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
48 $this->assertEquals('Argument mode "-1" is not valid.', $e->getMessage());
52 public function testIsArray()
54 $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
55 $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
56 $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
57 $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
58 $argument = new InputArgument('foo', InputArgument::OPTIONAL);
59 $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
62 public function testGetDescription()
64 $argument = new InputArgument('foo', null, 'Some description');
65 $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
68 public function testGetDefault()
70 $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
71 $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
74 public function testSetDefault()
76 $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
77 $argument->setDefault(null);
78 $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
79 $argument->setDefault('another');
80 $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
82 $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
83 $argument->setDefault(array(1, 2));
84 $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
87 $argument = new InputArgument('foo', InputArgument::REQUIRED);
88 $argument->setDefault('default');
89 $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
90 } catch (\Exception $e) {
91 $this->assertInstanceOf('\Exception', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
92 $this->assertEquals('Cannot set a default value except for Parameter::OPTIONAL mode.', $e->getMessage());
96 $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
97 $argument->setDefault('default');
98 $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
99 } catch (\Exception $e) {
100 $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
101 $this->assertEquals('A default value for an array argument must be an array.', $e->getMessage());