Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Input / InputArgumentTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Input;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\InputArgument;
16
17 class InputArgumentTest extends TestCase
18 {
19     public function testConstructor()
20     {
21         $argument = new InputArgument('foo');
22         $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
23     }
24
25     public function testModes()
26     {
27         $argument = new InputArgument('foo');
28         $this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
29
30         $argument = new InputArgument('foo', null);
31         $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
32
33         $argument = new InputArgument('foo', InputArgument::OPTIONAL);
34         $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
35
36         $argument = new InputArgument('foo', InputArgument::REQUIRED);
37         $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
38     }
39
40     /**
41      * @dataProvider provideInvalidModes
42      */
43     public function testInvalidModes($mode)
44     {
45         if (method_exists($this, 'expectException')) {
46             $this->expectException('InvalidArgumentException');
47             $this->expectExceptionMessage(sprintf('Argument mode "%s" is not valid.', $mode));
48         } else {
49             $this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode));
50         }
51
52         new InputArgument('foo', $mode);
53     }
54
55     public function provideInvalidModes()
56     {
57         return array(
58             array('ANOTHER_ONE'),
59             array(-1),
60         );
61     }
62
63     public function testIsArray()
64     {
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');
71     }
72
73     public function testGetDescription()
74     {
75         $argument = new InputArgument('foo', null, 'Some description');
76         $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
77     }
78
79     public function testGetDefault()
80     {
81         $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
82         $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
83     }
84
85     public function testSetDefault()
86     {
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');
92
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');
96     }
97
98     /**
99      * @expectedException        \LogicException
100      * @expectedExceptionMessage Cannot set a default value except for InputArgument::OPTIONAL mode.
101      */
102     public function testSetDefaultWithRequiredArgument()
103     {
104         $argument = new InputArgument('foo', InputArgument::REQUIRED);
105         $argument->setDefault('default');
106     }
107
108     /**
109      * @expectedException        \LogicException
110      * @expectedExceptionMessage A default value for an array argument must be an array.
111      */
112     public function testSetDefaultWithArrayArgument()
113     {
114         $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
115         $argument->setDefault('default');
116     }
117 }