Skip to content

Commit

Permalink
Merge branch '0.7' into 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Feb 20, 2015
2 parents 378e114 + fa90e9a commit 472be85
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 5 additions & 2 deletions library/Rules/AbstractRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ public function check($input)

public function validate($input)
{
$hasReference = $this->hasReference($input);
if ($input === '') {
return true;
}

$hasReference = $this->hasReference($input);
if ($this->mandatory && !$hasReference) {
return false;
}

return $this->decision('validate', $hasReference, $input) || $this->getReferenceValue($input) === '';
return $this->decision('validate', $hasReference, $input);
}
}
39 changes: 34 additions & 5 deletions tests/Rules/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,43 @@ public function testArrayWithPresentKeyShouldReturnTrue()
$this->assertTrue($validator->validate($obj));
}

public function testEmptyInputMustReturnTrue()
{
$validator = new Key('someEmptyKey');
$input = '';

$this->assertTrue($validator->assert($input));
$this->assertTrue($validator->check($input));
$this->assertTrue($validator->validate($input));
}

public function testArrayWithEmptyKeyShouldReturnTrue()
{
$validator = new Key('someEmptyKey');
$obj = array();
$obj['someEmptyKey'] = '';
$this->assertTrue($validator->assert($obj));
$this->assertTrue($validator->check($obj));
$this->assertTrue($validator->validate($obj));
$input = array();
$input['someEmptyKey'] = '';

$this->assertTrue($validator->assert($input));
$this->assertTrue($validator->check($input));
$this->assertTrue($validator->validate($input));
}

public function testShouldHaveTheSameReturnValueForAllValidators()
{
$rule = new Key('key', new NotEmpty());
$input = array('key' => '');

try {
$rule->assert($input);
$this->fail('`assert()` must throws exception');
} catch (\Exception $e) {}

try {
$rule->check($input);
$this->fail('`check()` must throws exception');
} catch (\Exception $e) {}

$this->assertFalse($rule->validate($input));
}

/**
Expand Down

0 comments on commit 472be85

Please sign in to comment.