/** * Add a nbArgument objects. * * @param nbArgument $argument A nbArgument object */ public function addArgument(nbArgument $argument) { if (isset($this->arguments[$argument->getName()])) { throw new InvalidArgumentException(sprintf('[nbArgumentSet::addArgument] An argument with name "%s" already exist.', $argument->getName())); } if ($this->hasAnArrayArgument) { throw new InvalidArgumentException('[nbArgumentSet::addArgument] Cannot add an argument after an array argument.'); } if ($argument->isRequired() && $this->hasAnOptionalArgument) { throw new InvalidArgumentException('[nbArgumentSet::addArgument] Cannot add a required argument after an optional one.'); } if ($argument->isArray()) { $this->hasAnArrayArgument = true; } if ($argument->isRequired()) { ++$this->requiredCount; } else { $this->hasAnOptionalArgument = true; } $this->arguments[$argument->getName()] = $argument; }
$argument = new nbArgument('command', nbArgument::OPTIONAL, 'The command to execute', 'description'); $t->is($argument->getName(), 'command', '->getName() argument name is "command"'); $t->is($argument->isRequired(), false, '->isRequired() argument is not required'); //$t->is($argument->getValue(), "description", "argument value is 'description'"); //$t->is($argument->toString(), "[command]", "argument toString is '[command]'"); $t->comment('nbArgumentTest - Test ctor'); try { $argument = new nbArgument('foo', 'ANOTHER_ONE'); $t->fail('->__construct() throws an Exception if the mode is not valid'); } catch (Exception $e) { $t->pass('->__construct() throws an Exception if the mode is not valid'); } $t->comment('nbArgumentTest - Test if an argument is an array'); $argument = new nbArgument('foo', nbArgument::IS_ARRAY); $t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array'); $argument = new nbArgument('foo', nbArgument::OPTIONAL | nbArgument::IS_ARRAY); $t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array'); $argument = new nbArgument('foo', nbArgument::OPTIONAL); $t->ok(!$argument->isArray(), '->isArray() returns false if the argument can not be an array'); $t->comment('nbArgumentTest - Test default value (->setDefault())'); $argument->setValue('value'); $t->is($argument->getValue(), 'value', '->getValue() returns "value"'); $t->comment('nbArgumentTest - Test to string'); $argument = new nbArgument('foo', nbArgument::OPTIONAL); $t->is((string) $argument, '[foo]', '->__toString() returns "[foo]"'); $argument = new nbArgument('foo', nbArgument::REQUIRED); $t->is((string) $argument, 'foo', '->__toString() returns "foo"'); $argument = new nbArgument('foo', nbArgument::IS_ARRAY); $t->is((string) $argument, '[foo1] ... [fooN]', '->__toString() returns "[foo1] ... [fooN]"'); $argument = new nbArgument('foo', nbArgument::REQUIRED | nbArgument::IS_ARRAY); $t->is((string) $argument, 'foo1 ... fooN', '->__toString() returns "foo1 ... fooN"');