Skip to content

Commit

Permalink
Fix #8 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed committed Sep 20, 2018
1 parent 1781c1a commit 06656dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Containers/ParsedInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function validate(ValidationInterface $validator): SafeInput {
} unset($key, $input);

foreach ($validator->getOptionalInputs() as $key => $input) {
if (isset($data[$key])) {
if (array_key_exists($key, $data)) {
try {
$clean_out[$key] = $input->setValue($data[$key])
->evaluate();
Expand Down
40 changes: 35 additions & 5 deletions tests/Containers/ParsedInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,36 @@ public function testOptionalParametersWithDefaultsUseDefaults()
$this->assertSame($default, $ret['short'], "'short' did not yield its default value");
}

/**
* @covers ::validate
*/
public function testOptionalParametersWithValidNullWorksWhenProvided()
{
$io = $this->getMockIO(true, null);
$io->expects($this->never())
->method('getDefaultValue');
$this->addOptional('key', $io);
$parsed = new ParsedInput(['key' => null]);
$ret = $parsed->validate($this->getValidation());
$this->assertInstanceOf(SafeInput::class, $ret);
$this->assertNull($ret['key'], 'key should have been null literal');
}

/**
* @covers ::validate
*/
public function testOptionalParametersWithValidNullWorksWhenNotProvided()
{
$io = $this->createMock(InputObject::class);
$io->expects($this->atLeastOnce())
->method('getDefaultValue')
->willReturn(false);
$this->addOptional('key', $io);
$parsed = new ParsedInput([]);
$ret = $parsed->validate($this->getValidation());
$this->assertInstanceOf(SafeInput::class, $ret);
$this->assertFalse($ret['key'], 'key should have been false literal');
}

// ----(Validation:Nesting)-------------------------------------------------

Expand Down Expand Up @@ -358,17 +388,17 @@ private function getValidation() {
return $validation;
} // getValidation

private function addRequired($key, InputObject $type) {
private function addRequired(string $key, InputObject $type) {
$this->required[$key] = $type;
} // addRequired

private function addOptional($key, InputObject $type) {
private function addOptional(string $key, InputObject $type) {
$this->optional[$key] = $type;
} // addOptional

private function getMockIO($valid, $ret = null) {
$mock = $this->getMockBuilder('Firehed\Input\Objects\InputObject')
->setMethods(['evaluate'])
private function getMockIO(bool $valid, $ret = null) {
$mock = $this->getMockBuilder(InputObject::class)
->setMethods(['evaluate', 'getDefaultValue'])
->getMockForAbstractClass();

if ($valid) {
Expand Down

0 comments on commit 06656dc

Please sign in to comment.