From a187eac8ef5a78c902ea389b91fa90388af8a69a Mon Sep 17 00:00:00 2001 From: mikey179 Date: Fri, 15 Aug 2014 21:31:24 +0200 Subject: [PATCH] improve naming --- CHANGELOG.md | 1 + src/main/php/ConsoleApp.php | 18 +- ...gumentsBindingModule.php => Arguments.php} | 20 +- .../helper/ConsoleAppUsingBindingModule.php | 11 + src/test/php/ConsoleAppTest.php | 16 +- .../php/ioc/ArgumentsBindingModuleTest.php | 365 ---------------- src/test/php/ioc/ArgumentsTest.php | 389 ++++++++++++++++++ 7 files changed, 441 insertions(+), 379 deletions(-) rename src/main/php/ioc/{ArgumentsBindingModule.php => Arguments.php} (89%) delete mode 100755 src/test/php/ioc/ArgumentsBindingModuleTest.php create mode 100755 src/test/php/ioc/ArgumentsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b92251b..60a5b00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * change of annotation value names in request broker for user input classes: `name` must now be `paramName`, group must now be `paramGroup` * changed all thrown stubbles/core exceptions to those recommended with stubbles/core 5.0.0 * removed `stubbles\console\Executor::getOutputStream()`, use `stubbles\console\Executor::out()` instead, was deprecated since 3.0.0 + * deprecated `stubbles\console\ConsoleApp::createArgumentsBindingModule()`, use `stubbles\console\ConsoleApp::bindArguments()` instead, will be removed with 5.0.0 * deprecated `stubbles\console\ConsoleApp::createConsoleBindingModule()`, console binding module will be added by default, will be removed with 5.0.0 diff --git a/src/main/php/ConsoleApp.php b/src/main/php/ConsoleApp.php index b34d562..9c196c9 100644 --- a/src/main/php/ConsoleApp.php +++ b/src/main/php/ConsoleApp.php @@ -8,7 +8,7 @@ * @package stubbles\console */ namespace stubbles\console; -use stubbles\console\ioc\ArgumentsBindingModule; +use stubbles\console\ioc\Arguments; use stubbles\console\ioc\ConsoleBindingModule; use stubbles\ioc\App; use stubbles\streams\OutputStream; @@ -135,11 +135,23 @@ protected static function getBindingsForApp($className, $projectPath) * creates argument binding module * * @api - * @return \stubbles\console\ioc\ArgumentsBindingModule + * @return \stubbles\console\ioc\Arguments + */ + protected static function bindArguments() + { + return new Arguments(self::$stubcli); + } + + /** + * creates argument binding module + * + * @api + * @return \stubbles\console\ioc\Arguments + * @deprecated since 4.0.0, use bindArguments() instead, will be removed with 5.0.0 */ protected static function createArgumentsBindingModule() { - return new ArgumentsBindingModule(self::$stubcli); + return self::bindArguments(); } /** diff --git a/src/main/php/ioc/ArgumentsBindingModule.php b/src/main/php/ioc/Arguments.php similarity index 89% rename from src/main/php/ioc/ArgumentsBindingModule.php rename to src/main/php/ioc/Arguments.php index 76d0f4e..1caaefa 100755 --- a/src/main/php/ioc/ArgumentsBindingModule.php +++ b/src/main/php/ioc/Arguments.php @@ -9,13 +9,13 @@ */ namespace stubbles\console\ioc; use stubbles\input\broker\RequestBroker; +use stubbles\input\console\BaseConsoleRequest; use stubbles\ioc\Binder; use stubbles\ioc\module\BindingModule; -use stubbles\lang\exception\ConfigurationException; /** * Binding module to configure the binder with arguments. */ -class ArgumentsBindingModule implements BindingModule +class Arguments implements BindingModule { /** * switch whether stubcli was used to run the command @@ -57,15 +57,15 @@ public function __construct($stubcliUsed = false) * * @api * @param string $options - * @return \stubbles\console\ioc\ArgumentsBindingModule + * @return \stubbles\console\ioc\Arguments */ public function withOptions($options) { + $this->options = $options; if ($this->stubcliUsed && !strstr($options, 'c')) { - $options .= 'c:'; + $this->options .= 'c:'; } - $this->options = $options; return $this; } @@ -74,7 +74,7 @@ public function withOptions($options) * * @api * @param string[] $options - * @return \stubbles\console\ioc\ArgumentsBindingModule + * @return \stubbles\console\ioc\Arguments */ public function withLongOptions(array $options) { @@ -90,7 +90,7 @@ public function withLongOptions(array $options) * sets class to store user input into * * @param string $className - * @return \stubbles\console\ioc\ArgumentsBindingModule + * @return \stubbles\console\ioc\Arguments */ public function withUserInput($className) { @@ -117,7 +117,7 @@ public function configure(Binder $binder) ->to($value); } - $request = new \stubbles\input\console\BaseConsoleRequest($args, $_SERVER); + $request = new BaseConsoleRequest($args, $_SERVER); $binder->bind('stubbles\input\Request') ->toInstance($request); $binder->bind('stubbles\input\console\ConsoleRequest') @@ -138,7 +138,7 @@ public function configure(Binder $binder) * returns parsed arguments * * @return array - * @throws \stubbles\lang\exception\ConfigurationException + * @throws \RuntimeException */ private function parseArgs() { @@ -149,7 +149,7 @@ private function parseArgs() $this->parseOptions(); $parsedVars = $this->getopt($this->options, $this->longopts); if (false === $parsedVars) { - throw new ConfigurationException('Error parsing "' . join(' ', $_SERVER['argv']) . '" with ' . $this->options . ' and ' . join(' ', $this->longopts)); + throw new \RuntimeException('Error parsing "' . join(' ', $_SERVER['argv']) . '" with ' . $this->options . ' and ' . join(' ', $this->longopts)); } return $this->fixArgs($_SERVER['argv'], $parsedVars); diff --git a/src/test/helper/ConsoleAppUsingBindingModule.php b/src/test/helper/ConsoleAppUsingBindingModule.php index df8a87b..ef042a9 100644 --- a/src/test/helper/ConsoleAppUsingBindingModule.php +++ b/src/test/helper/ConsoleAppUsingBindingModule.php @@ -16,11 +16,21 @@ */ class ConsoleAppUsingBindingModule extends ConsoleApp { + /** + * creates mode binding module + * + * @return ArgumentsBindingModule + */ + public static function returnBindArguments() + { + return self::bindArguments(); + } /** * creates mode binding module * * @return ArgumentsBindingModule + * @deprecated since 4.0.0 */ public static function getArgumentsBindingModule() { @@ -31,6 +41,7 @@ public static function getArgumentsBindingModule() * creates properties binding module * * @return ConsoleBindingModule + * @deprecated since 4.0.0 */ public static function getConsoleBindingModule() { diff --git a/src/test/php/ConsoleAppTest.php b/src/test/php/ConsoleAppTest.php index 7c43107..3e0bb20 100644 --- a/src/test/php/ConsoleAppTest.php +++ b/src/test/php/ConsoleAppTest.php @@ -361,14 +361,27 @@ public function commandReturnCodeIsReturned() ); } + /** + * @since 4.0.0 + * @test + */ + public function canCreateArguments() + { + $this->assertInstanceOf( + 'stubbles\console\ioc\Arguments', + ConsoleAppUsingBindingModule::returnBindArguments() + ); + } + /** * @since 2.0.0 * @test + * @deprecated since 4.0.0 */ public function canCreateArgumentsBindingModule() { $this->assertInstanceOf( - 'stubbles\console\ioc\ArgumentsBindingModule', + 'stubbles\console\ioc\Arguments', ConsoleAppUsingBindingModule::getArgumentsBindingModule() ); } @@ -376,6 +389,7 @@ public function canCreateArgumentsBindingModule() /** * @since 2.0.0 * @test + * @deprecated since 4.0.0 */ public function canCreateConsoleBindingModule() { diff --git a/src/test/php/ioc/ArgumentsBindingModuleTest.php b/src/test/php/ioc/ArgumentsBindingModuleTest.php deleted file mode 100755 index aec8012..0000000 --- a/src/test/php/ioc/ArgumentsBindingModuleTest.php +++ /dev/null @@ -1,365 +0,0 @@ -argumentsBindingModule = $this->getMock('stubbles\console\ioc\ArgumentsBindingModule', - ['getopt'] - ); - $this->argvBackup = $_SERVER['argv']; - } - - /** - * clean up test environment - */ - public function tearDown() - { - $_SERVER['argv'] = $this->argvBackup; - } - - /** - * @test - */ - public function argumentsAreBoundAsRecievedWhenNoOptionsDefined() - { - $_SERVER['argv'] = ['foo.php', 'bar', 'baz']; - $injector = $this->bindArguments(); - $this->assertTrue($injector->hasConstant('argv.0')); - $this->assertTrue($injector->hasConstant('argv.1')); - $this->assertEquals('bar', $injector->getConstant('argv.0')); - $this->assertEquals('baz', $injector->getConstant('argv.1')); - } - - /** - * @test - * @group issue_1 - */ - public function argumentsAreBoundAsListWhenNoOptionsDefined() - { - $_SERVER['argv'] = ['foo.php', 'bar', 'baz']; - $injector = $this->bindArguments(); - $this->assertTrue($injector->hasConstant('argv')); - $this->assertEquals(['argv.0' => 'bar', 'argv.1' => 'baz'], - $injector->getConstant('argv') - ); - } - - /** - * @test - */ - public function argumentsAreBoundAfterParsingWhenOptionsDefined() - { - $_SERVER['argv'] = ['foo.php', '-n', 'example', '--verbose', 'install']; - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('n:f::'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['n' => 'example', 'verbose' => false])); - $this->argumentsBindingModule->withOptions('n:f::') - ->withLongOptions(['verbose']); - $injector = $this->bindArguments(); - $this->assertTrue($injector->hasConstant('argv.n')); - $this->assertTrue($injector->hasConstant('argv.verbose')); - $this->assertTrue($injector->hasConstant('argv.0')); - $this->assertEquals('example', $injector->getConstant('argv.n')); - $this->assertFalse($injector->getConstant('argv.verbose')); - $this->assertEquals('install', $injector->getConstant('argv.0')); - } - - /** - * @test - * @group issue_1 - */ - public function argumentsAreBoundAsListAfterParsingWhenOptionsDefined() - { - $_SERVER['argv'] = ['foo.php', '-n', 'example', '--verbose', 'install']; - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('n:f::'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['n' => 'example', 'verbose' => false])); - $this->argumentsBindingModule->withOptions('n:f::') - ->withLongOptions(['verbose']); - $injector = $this->bindArguments(); - $this->assertTrue($injector->hasConstant('argv')); - $this->assertEquals(['n' => 'example', 'verbose' => false, 'argv.0' => 'install'], - $injector->getConstant('argv') - ); - } - - /** - * @test - */ - public function bindsRequestIfAvailable() - { - $this->assertTrue($this->bindArguments()->hasBinding('stubbles\input\Request')); - } - - /** - * @test - */ - public function bindsConsoleRequestIfAvailable() - { - $this->assertTrue($this->bindArguments()->hasBinding('stubbles\input\console\ConsoleRequest')); - } - - /** - * @test - */ - public function bindsRequestToConsoleRequest() - { - $this->assertInstanceOf('stubbles\input\console\ConsoleRequest', - $this->bindArguments()->getInstance('stubbles\input\Request') - ); - } - - /** - * @test - */ - public function bindsRequestToBaseConsoleRequest() - { - $this->assertInstanceOf('stubbles\input\console\BaseConsoleRequest', - $this->bindArguments()->getInstance('stubbles\input\Request') - ); - } - - /** - * @test - */ - public function bindsConsoleRequestToBaseConsoleRequest() - { - $this->assertInstanceOf('stubbles\input\console\BaseConsoleRequest', - $this->bindArguments()->getInstance('stubbles\input\console\ConsoleRequest') - ); - } - - /** - * @test - */ - public function bindsRequestAsSingleton() - { - $injector = $this->bindArguments(); - $this->assertSame($injector->getInstance('stubbles\input\Request'), - $injector->getInstance('stubbles\input\Request') - ); - } - - /** - * @test - */ - public function bindsConsoleRequestAsSingleton() - { - $injector = $this->bindArguments(); - $this->assertSame($injector->getInstance('stubbles\input\Request'), - $injector->getInstance('stubbles\input\console\ConsoleRequest') - ); - } - - /** - * binds arguments - * - * @return Injector - */ - protected function bindArguments() - { - $binder = new Binder(); - $this->argumentsBindingModule->configure($binder); - return $binder->getInjector(); - } - - /** - * binds request - * - * @return stubbles\input\Request - */ - private function bindRequest() - { - return $this->bindArguments()->getInstance('stubbles\input\Request'); - } - - /** - * @test - */ - public function argumentsAvailableViaRequestWhenNoOptionsDefined() - { - $_SERVER['argv'] = ['foo', 'bar', 'baz']; - $request = $this->bindRequest(); - $this->assertTrue($request->hasParam('argv.0')); - $this->assertTrue($request->hasParam('argv.1')); - $this->assertEquals('bar', $request->readParam('argv.0')->unsecure()); - $this->assertEquals('baz', $request->readParam('argv.1')->unsecure()); - } - - /** - * @test - */ - public function argumentsAvailableViaRequestAfterParsingWhenOptionsDefined() - { - $_SERVER['argv'] = ['foo.php', '-n', 'example', '--verbose', 'bar']; - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('n:f::'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['n' => 'example', 'verbose' => false])); - $this->argumentsBindingModule->withOptions('n:f::') - ->withLongOptions(['verbose']); - $request = $this->bindRequest(); - $this->assertTrue($request->hasParam('n')); - $this->assertTrue($request->hasParam('verbose')); - $this->assertTrue($request->hasParam('argv.0')); - $this->assertEquals('example', $request->readParam('n')->unsecure()); - $this->assertFalse($request->readParam('verbose')->unsecure()); - $this->assertEquals('bar', $request->readParam('argv.0')->unsecure()); - } - - /** - * @test - * @expectedException stubbles\lang\exception\ConfigurationException - */ - public function invalidOptionsThrowConfigurationException() - { - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('//'), $this->equalTo([])) - ->will($this->returnValue(false)); - $this->argumentsBindingModule->withOptions('//'); - $this->bindArguments(); - } - - /** - * @test - */ - public function optionsContainCIfStubCliEnabled() - { - $this->argumentsBindingModule = $this->getMock('stubbles\console\ioc\ArgumentsBindingModule', - ['getopt'], - [true] - ); - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('n:f::c:'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['n' => 'example', 'verbose' => false])); - $this->argumentsBindingModule->withOptions('n:f::') - ->withLongOptions(['verbose']); - $this->bindArguments(); - } - - /** - * @test - */ - public function optionsContainCIfStubCliEnabledAndOnlyLongOptionsSet() - { - $this->argumentsBindingModule = $this->getMock('stubbles\console\ioc\ArgumentsBindingModule', - ['getopt'], - [true] - ); - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('c:'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['verbose' => false])); - $this->argumentsBindingModule->withLongOptions(['verbose']); - $this->bindArguments(); - } - - /** - * @test - */ - public function optionsContainCIfStubCliEnabledAndLongOptionsSetFirst() - { - $this->argumentsBindingModule = $this->getMock('stubbles\console\ioc\ArgumentsBindingModule', - ['getopt'], - [true] - ); - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('n:f::c:'), $this->equalTo(['verbose'])) - ->will($this->returnValue(['n' => 'example', 'verbose' => false])); - $this->argumentsBindingModule->withLongOptions(['verbose']) - ->withOptions('n:f::'); - $this->bindArguments(); - } - - /** - * @test - */ - public function bindsNoUserInputByDefault() - { - $injector = $this->bindArguments(); - $this->assertFalse($injector->hasConstant('stubbles.console.input.class')); - } - - /** - * @test - */ - public function bindsUserInputIfSet() - { - $this->argumentsBindingModule->withUserInput('org\stubbles\console\test\BrokeredUserInput'); - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('vo:u:h'), $this->equalTo(['verbose', 'bar1:', 'bar2:', 'help'])) - ->will($this->returnValue([])); - $injector = $this->bindArguments(); - $this->assertTrue($injector->hasConstant('stubbles.console.input.class')); - $this->assertTrue($injector->hasBinding('org\stubbles\console\test\BrokeredUserInput')); - } - - /** - * @since 2.1.0 - * @test - */ - public function bindsUserInputAsSingleton() - { - $this->argumentsBindingModule->withUserInput('org\stubbles\console\test\BrokeredUserInput'); - $this->argumentsBindingModule->expects($this->once()) - ->method('getopt') - ->with($this->equalTo('vo:u:h'), $this->equalTo(['verbose', 'bar1:', 'bar2:', 'help'])) - ->will($this->returnValue(['bar2' => 'foo', 'o' => 'baz'])); - $binder = new Binder(); - $this->argumentsBindingModule->configure($binder); - $binder->bind('stubbles\streams\OutputStream') - ->named('stdout') - ->toInstance($this->getMock('stubbles\streams\OutputStream')); - $binder->bind('stubbles\streams\OutputStream') - ->named('stderr') - ->toInstance($this->getMock('stubbles\streams\OutputStream')); - $binder->bind('stubbles\streams\InputStream') - ->named('stdin') - ->toInstance($this->getMock('stubbles\streams\InputStream')); - $binder->bindMap('stubbles\input\broker\param\ParamBroker') - ->withEntry('Mock', $this->getMock('stubbles\input\broker\param\ParamBroker')); - $injector = $binder->getInjector(); - $this->assertSame($injector->getInstance('org\stubbles\console\test\BrokeredUserInput'), - $injector->getInstance('org\stubbles\console\test\BrokeredUserInput') - ); - } -} diff --git a/src/test/php/ioc/ArgumentsTest.php b/src/test/php/ioc/ArgumentsTest.php new file mode 100755 index 0000000..ab65578 --- /dev/null +++ b/src/test/php/ioc/ArgumentsTest.php @@ -0,0 +1,389 @@ +arguments = $this->getMock( + 'stubbles\console\ioc\Arguments', + ['getopt'] + ); + $this->argvBackup = $_SERVER['argv']; + } + + /** + * clean up test environment + */ + public function tearDown() + { + $_SERVER['argv'] = $this->argvBackup; + } + + /** + * @return array + */ + public function boundNonOptionArguments() + { + return [ + ['bar', 'argv.0'], + ['baz', 'argv.1'], + [['argv.0' => 'bar', 'argv.1' => 'baz'], 'argv'], + ]; + } + + /** + * @test + * @dataProvider boundNonOptionArguments + */ + public function argumentsAreBoundWhenNoOptionsDefined($expected, $constantName) + { + $_SERVER['argv'] = ['foo.php', 'bar', 'baz']; + $this->assertEquals( + $expected, + $this->bindArguments()->getConstant($constantName) + ); + } + + /** + * @return array + */ + public function boundOptionArguments() + { + return [ + ['example', 'argv.n'], + [false, 'argv.verbose'], + ['install', 'argv.0'], + [['n' => 'example', 'verbose' => false, 'argv.0' => 'install'], 'argv'] + ]; + } + + /** + * @test + * @dataProvider boundOptionArguments + */ + public function argumentsAreBoundAfterParsingWhenOptionsDefined($expected, $constantName) + { + $_SERVER['argv'] = ['foo.php', '-n', 'example', '--verbose', 'install']; + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('n:f::'), $this->equalTo(['verbose'])) + ->will($this->returnValue(['n' => 'example', 'verbose' => false])); + $this->arguments->withOptions('n:f::') + ->withLongOptions(['verbose']); + $this->assertEquals( + $expected, + $this->bindArguments()->getConstant($constantName) + ); + } + + /** + * @test + */ + public function bindsRequest() + { + $this->assertTrue( + $this->bindArguments()->hasBinding('stubbles\input\Request') + ); + } + + /** + * @test + */ + public function bindsConsoleRequest() + { + $this->assertTrue( + $this->bindArguments()->hasBinding('stubbles\input\console\ConsoleRequest') + ); + } + + /** + * @test + */ + public function bindsRequestToConsoleRequest() + { + $this->assertInstanceOf( + 'stubbles\input\console\ConsoleRequest', + $this->bindArguments()->getInstance('stubbles\input\Request') + ); + } + + /** + * @test + */ + public function bindsRequestToBaseConsoleRequest() + { + $this->assertInstanceOf( + 'stubbles\input\console\BaseConsoleRequest', + $this->bindArguments()->getInstance('stubbles\input\Request') + ); + } + + /** + * @test + */ + public function bindsConsoleRequestToBaseConsoleRequest() + { + $this->assertInstanceOf( + 'stubbles\input\console\BaseConsoleRequest', + $this->bindArguments()->getInstance('stubbles\input\console\ConsoleRequest') + ); + } + + /** + * @test + */ + public function bindsRequestAsSingleton() + { + $injector = $this->bindArguments(); + $this->assertSame( + $injector->getInstance('stubbles\input\Request'), + $injector->getInstance('stubbles\input\Request') + ); + } + + /** + * @test + */ + public function bindsConsoleRequestAsSingleton() + { + $injector = $this->bindArguments(); + $this->assertSame( + $injector->getInstance('stubbles\input\Request'), + $injector->getInstance('stubbles\input\console\ConsoleRequest') + ); + } + + /** + * binds arguments + * + * @return Injector + */ + protected function bindArguments() + { + $binder = new Binder(); + $this->arguments->configure($binder); + return $binder->getInjector(); + } + + /** + * binds request + * + * @return stubbles\input\Request + */ + private function bindRequest() + { + return $this->bindArguments()->getInstance('stubbles\input\Request'); + } + + /** + * @return array + */ + public function requestArgumentsWhenNoOptionsDefined() + { + return [ + ['bar', 'argv.0'], + ['baz', 'argv.1'] + ]; + } + + /** + * @test + * @dataProvider requestArgumentsWhenNoOptionsDefined + */ + public function argumentsAvailableViaRequestWhenNoOptionsDefined($expected, $paramName) + { + $_SERVER['argv'] = ['foo', 'bar', 'baz']; + $this->assertEquals( + $expected, + $this->bindRequest()->readParam($paramName)->unsecure() + ); + } + + /** + * @return array + */ + public function requestArgumentsWhenOptionsDefined() + { + return [ + ['example', 'n'], + [false, 'verbose'], + ['bar', 'argv.0'] + ]; + } + + /** + * @test + * @dataProvider requestArgumentsWhenOptionsDefined + */ + public function argumentsAvailableViaRequestAfterParsingWhenOptionsDefined($expected, $paramName) + { + $_SERVER['argv'] = ['foo.php', '-n', 'example', '--verbose', 'bar']; + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('n:f::'), $this->equalTo(['verbose'])) + ->will($this->returnValue(['n' => 'example', 'verbose' => false])); + $this->arguments->withOptions('n:f::') + ->withLongOptions(['verbose']); + $this->assertEquals( + $expected, + $this->bindRequest()->readParam($paramName)->unsecure() + ); + } + + /** + * @test + * @expectedException RuntimeException + */ + public function invalidOptionsThrowConfigurationException() + { + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('//'), $this->equalTo([])) + ->will($this->returnValue(false)); + $this->arguments->withOptions('//'); + $this->bindArguments(); + } + + /** + * @test + */ + public function optionsContainCIfStubCliEnabled() + { + $this->arguments = $this->getMock( + 'stubbles\console\ioc\Arguments', + ['getopt'], + [true] + ); + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('n:f::c:'), $this->equalTo(['verbose'])) + ->will($this->returnValue(['n' => 'example', 'verbose' => false])); + $this->arguments->withOptions('n:f::') + ->withLongOptions(['verbose']); + $this->bindArguments(); + } + + /** + * @test + */ + public function optionsContainCIfStubCliEnabledAndOnlyLongOptionsSet() + { + $this->arguments = $this->getMock( + 'stubbles\console\ioc\Arguments', + ['getopt'], + [true] + ); + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('c:'), $this->equalTo(['verbose'])) + ->will($this->returnValue(['verbose' => false])); + $this->arguments->withLongOptions(['verbose']); + $this->bindArguments(); + } + + /** + * @test + */ + public function optionsContainCIfStubCliEnabledAndLongOptionsSetFirst() + { + $this->arguments = $this->getMock( + 'stubbles\console\ioc\Arguments', + ['getopt'], + [true] + ); + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('n:f::c:'), $this->equalTo(['verbose'])) + ->will($this->returnValue(['n' => 'example', 'verbose' => false])); + $this->arguments->withLongOptions(['verbose']) + ->withOptions('n:f::'); + $this->bindArguments(); + } + + /** + * @test + */ + public function bindsNoUserInputByDefault() + { + $this->assertFalse( + $this->bindArguments()->hasConstant('stubbles.console.input.class') + ); + } + + /** + * @test + */ + public function bindsUserInputIfSet() + { + $this->arguments->withUserInput('org\stubbles\console\test\BrokeredUserInput'); + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('vo:u:h'), $this->equalTo(['verbose', 'bar1:', 'bar2:', 'help'])) + ->will($this->returnValue([])); + $injector = $this->bindArguments(); + $this->assertTrue($injector->hasConstant('stubbles.console.input.class')); + $this->assertTrue($injector->hasBinding('org\stubbles\console\test\BrokeredUserInput')); + } + + /** + * @since 2.1.0 + * @test + */ + public function bindsUserInputAsSingleton() + { + $this->arguments->withUserInput('org\stubbles\console\test\BrokeredUserInput'); + $this->arguments->expects($this->once()) + ->method('getopt') + ->with($this->equalTo('vo:u:h'), $this->equalTo(['verbose', 'bar1:', 'bar2:', 'help'])) + ->will($this->returnValue(['bar2' => 'foo', 'o' => 'baz'])); + $binder = new Binder(); + $this->arguments->configure($binder); + $binder->bind('stubbles\streams\OutputStream') + ->named('stdout') + ->toInstance($this->getMock('stubbles\streams\OutputStream')); + $binder->bind('stubbles\streams\OutputStream') + ->named('stderr') + ->toInstance($this->getMock('stubbles\streams\OutputStream')); + $binder->bind('stubbles\streams\InputStream') + ->named('stdin') + ->toInstance($this->getMock('stubbles\streams\InputStream')); + $binder->bindMap('stubbles\input\broker\param\ParamBroker') + ->withEntry('Mock', $this->getMock('stubbles\input\broker\param\ParamBroker')); + $injector = $binder->getInjector(); + $this->assertSame($injector->getInstance('org\stubbles\console\test\BrokeredUserInput'), + $injector->getInstance('org\stubbles\console\test\BrokeredUserInput') + ); + } +}