2 namespace Consolidation\Config\Inject;
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;
15 class ConfigForCommandTest extends \PHPUnit_Framework_TestCase
19 protected function setUp()
24 'global' => 'from-config',
26 // Define some configuration settings for the options for
27 // the commands my:foo and my:bar.
30 // commands.my.options.* apply to all my:* commands.
32 'dir' => '/etc/common',
33 'priority' => 'normal',
36 // commands.my.foo.options.* apply only to the my:foo command.
45 $this->config = new Config($data);
48 public function testInjection()
50 $command = new MyFooCommand();
51 $input = new StringInput('my:foo');
53 list($status, $output) = $this->runCommandViaApplication($command, $input);
55 $expectedOutput = <<< EOT
62 $this->assertEquals(0, $status);
63 $this->assertEquals($expectedOutput, $output);
66 public function testInjectionWithOverride()
68 $command = new MyFooCommand();
69 $input = new StringInput('my:foo --name=Fred');
71 list($status, $output) = $this->runCommandViaApplication($command, $input);
73 $expectedOutput = <<< EOT
80 $this->assertEquals(0, $status);
81 $this->assertEquals($expectedOutput, $output);
84 public function testHelpDefaultInjection()
86 $command = new MyFooCommand();
87 $input = new StringInput('help my:foo');
89 list($status, $output) = $this->runCommandViaApplication($command, $input);
91 $expectedOutput = <<< EOT
92 What is the name of the thing we are naming [default: "baz"]
95 $this->assertEquals(0, $status);
96 $this->assertContains($expectedOutput, $output);
98 $expectedOutput = <<< EOT
99 A certain global option. [default: "from-config"]
102 $this->assertContains($expectedOutput, $output);
105 protected function runCommandViaApplication($command, $input)
107 $application = new Application('TestApplication', '0.0.0');
108 $application->getDefinition()
110 new InputOption('--global', null, InputOption::VALUE_REQUIRED, 'A certain global option.', 'hardcoded')
113 $output = new BufferedOutput();
115 $configInjector = new ConfigForCommand($this->config);
116 $configInjector->setApplication($application);
118 $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
119 $eventDispatcher->addSubscriber($configInjector);
120 $application->setDispatcher($eventDispatcher);
122 $application->setAutoExit(false);
123 $application->add($command);
125 $statusCode = $application->run($input, $output);
126 $commandOutput = trim($output->fetch());
128 return [$statusCode, $commandOutput];