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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\InputArgument;
17 class InputArgumentTest extends TestCase
19 public function testConstructor()
21 $argument = new InputArgument('foo');
22 $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
25 public function testModes()
27 $argument = new InputArgument('foo');
28 $this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
30 $argument = new InputArgument('foo', null);
31 $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
33 $argument = new InputArgument('foo', InputArgument::OPTIONAL);
34 $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
36 $argument = new InputArgument('foo', InputArgument::REQUIRED);
37 $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
41 * @dataProvider provideInvalidModes
43 public function testInvalidModes($mode)
45 if (method_exists($this, 'expectException')) {
46 $this->expectException('InvalidArgumentException');
47 $this->expectExceptionMessage(sprintf('Argument mode "%s" is not valid.', $mode));
49 $this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode));
52 new InputArgument('foo', $mode);
55 public function provideInvalidModes()
63 public function testIsArray()
65 $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
66 $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
67 $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
68 $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
69 $argument = new InputArgument('foo', InputArgument::OPTIONAL);
70 $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
73 public function testGetDescription()
75 $argument = new InputArgument('foo', null, 'Some description');
76 $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
79 public function testGetDefault()
81 $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
82 $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
85 public function testSetDefault()
87 $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
88 $argument->setDefault(null);
89 $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
90 $argument->setDefault('another');
91 $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
93 $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
94 $argument->setDefault(array(1, 2));
95 $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
99 * @expectedException \LogicException
100 * @expectedExceptionMessage Cannot set a default value except for InputArgument::OPTIONAL mode.
102 public function testSetDefaultWithRequiredArgument()
104 $argument = new InputArgument('foo', InputArgument::REQUIRED);
105 $argument->setDefault('default');
109 * @expectedException \LogicException
110 * @expectedExceptionMessage A default value for an array argument must be an array.
112 public function testSetDefaultWithArrayArgument()
114 $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
115 $argument->setDefault('default');