41da3076b347c91f819ae647b389518490915b9b
[yaffs-website] / vendor / consolidation / config / tests / ConfigForCommandTest.php
1 <?php
2 namespace Consolidation\Config\Inject;
3
4 use Consolidation\Config\Config;
5 use Consolidation\TestUtils\MyFooCommand;
6 use Symfony\Component\Console\Application;
7 use Symfony\Component\Console\Command\Command;
8 use Symfony\Component\Console\Input\InputOption;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\ArgvInput;
11 use Symfony\Component\Console\Input\StringInput;
12 use Symfony\Component\Console\Output\BufferedOutput;
13 use Symfony\Component\Console\Output\OutputInterface;
14
15 class ConfigForCommandTest extends \PHPUnit_Framework_TestCase
16 {
17     protected $config;
18
19     protected function setUp()
20     {
21         $data = [
22             // Global options
23             'options' => [
24                 'global' => 'from-config',
25             ],
26             // Define some configuration settings for the options for
27             // the commands my:foo and my:bar.
28             'command' => [
29                 'my' => [
30                     // commands.my.options.* apply to all my:* commands.
31                     'options' => [
32                         'dir' => '/etc/common',
33                         'priority' => 'normal',
34                     ],
35                     'foo' => [
36                         // commands.my.foo.options.* apply only to the my:foo command.
37                         'options' => [
38                             'name' => 'baz',
39                         ],
40                     ],
41                 ],
42             ],
43         ];
44
45         $this->config = new Config($data);
46     }
47
48     public function testInjection()
49     {
50         $command = new MyFooCommand();
51         $input = new StringInput('my:foo');
52
53         list($status, $output) = $this->runCommandViaApplication($command, $input);
54
55         $expectedOutput = <<< EOT
56 Enter my:foo
57 dir: /etc/common
58 name: baz
59 other: fish
60 EOT;
61
62         $this->assertEquals(0, $status);
63         $this->assertEquals($expectedOutput, $output);
64     }
65
66     public function testInjectionWithOverride()
67     {
68         $command = new MyFooCommand();
69         $input = new StringInput('my:foo --name=Fred');
70
71         list($status, $output) = $this->runCommandViaApplication($command, $input);
72
73         $expectedOutput = <<< EOT
74 Enter my:foo
75 dir: /etc/common
76 name: Fred
77 other: fish
78 EOT;
79
80         $this->assertEquals(0, $status);
81         $this->assertEquals($expectedOutput, $output);
82     }
83
84     public function testHelpDefaultInjection()
85     {
86         $command = new MyFooCommand();
87         $input = new StringInput('help my:foo');
88
89         list($status, $output) = $this->runCommandViaApplication($command, $input);
90
91         $expectedOutput = <<< EOT
92 What is the name of the thing we are naming [default: "baz"]
93 EOT;
94
95         $this->assertEquals(0, $status);
96         $this->assertContains($expectedOutput, $output);
97
98         $expectedOutput = <<< EOT
99 A certain global option. [default: "from-config"]
100 EOT;
101
102         $this->assertContains($expectedOutput, $output);
103     }
104
105     protected function runCommandViaApplication($command, $input)
106     {
107         $application = new Application('TestApplication', '0.0.0');
108         $application->getDefinition()
109             ->addOption(
110                 new InputOption('--global', null, InputOption::VALUE_REQUIRED, 'A certain global option.', 'hardcoded')
111             );
112
113         $output = new BufferedOutput();
114
115         $configInjector = new ConfigForCommand($this->config);
116         $configInjector->setApplication($application);
117
118         $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
119         $eventDispatcher->addSubscriber($configInjector);
120         $application->setDispatcher($eventDispatcher);
121
122         $application->setAutoExit(false);
123         $application->add($command);
124
125         $statusCode = $application->run($input, $output);
126         $commandOutput = trim($output->fetch());
127
128         return [$statusCode, $commandOutput];
129     }
130 }