X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fconsolidation%2Fconfig%2Ftests%2FConfigForCommandTest.php;fp=vendor%2Fconsolidation%2Fconfig%2Ftests%2FConfigForCommandTest.php;h=41da3076b347c91f819ae647b389518490915b9b;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/consolidation/config/tests/ConfigForCommandTest.php b/vendor/consolidation/config/tests/ConfigForCommandTest.php new file mode 100644 index 000000000..41da3076b --- /dev/null +++ b/vendor/consolidation/config/tests/ConfigForCommandTest.php @@ -0,0 +1,130 @@ + [ + 'global' => 'from-config', + ], + // Define some configuration settings for the options for + // the commands my:foo and my:bar. + 'command' => [ + 'my' => [ + // commands.my.options.* apply to all my:* commands. + 'options' => [ + 'dir' => '/etc/common', + 'priority' => 'normal', + ], + 'foo' => [ + // commands.my.foo.options.* apply only to the my:foo command. + 'options' => [ + 'name' => 'baz', + ], + ], + ], + ], + ]; + + $this->config = new Config($data); + } + + public function testInjection() + { + $command = new MyFooCommand(); + $input = new StringInput('my:foo'); + + list($status, $output) = $this->runCommandViaApplication($command, $input); + + $expectedOutput = <<< EOT +Enter my:foo +dir: /etc/common +name: baz +other: fish +EOT; + + $this->assertEquals(0, $status); + $this->assertEquals($expectedOutput, $output); + } + + public function testInjectionWithOverride() + { + $command = new MyFooCommand(); + $input = new StringInput('my:foo --name=Fred'); + + list($status, $output) = $this->runCommandViaApplication($command, $input); + + $expectedOutput = <<< EOT +Enter my:foo +dir: /etc/common +name: Fred +other: fish +EOT; + + $this->assertEquals(0, $status); + $this->assertEquals($expectedOutput, $output); + } + + public function testHelpDefaultInjection() + { + $command = new MyFooCommand(); + $input = new StringInput('help my:foo'); + + list($status, $output) = $this->runCommandViaApplication($command, $input); + + $expectedOutput = <<< EOT +What is the name of the thing we are naming [default: "baz"] +EOT; + + $this->assertEquals(0, $status); + $this->assertContains($expectedOutput, $output); + + $expectedOutput = <<< EOT +A certain global option. [default: "from-config"] +EOT; + + $this->assertContains($expectedOutput, $output); + } + + protected function runCommandViaApplication($command, $input) + { + $application = new Application('TestApplication', '0.0.0'); + $application->getDefinition() + ->addOption( + new InputOption('--global', null, InputOption::VALUE_REQUIRED, 'A certain global option.', 'hardcoded') + ); + + $output = new BufferedOutput(); + + $configInjector = new ConfigForCommand($this->config); + $configInjector->setApplication($application); + + $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); + $eventDispatcher->addSubscriber($configInjector); + $application->setDispatcher($eventDispatcher); + + $application->setAutoExit(false); + $application->add($command); + + $statusCode = $application->run($input, $output); + $commandOutput = trim($output->fetch()); + + return [$statusCode, $commandOutput]; + } +}