Skip to content

Commit

Permalink
Add a function to assert values in array
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosV2 committed Jun 23, 2017
1 parent a7e1a3b commit d9ba39b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ Available documentation:
- [One of](https://github.com/carlosV2/Can/blob/master/docs/ConstraintsExtension/one_of.md)
- [All of](https://github.com/carlosV2/Can/blob/master/docs/ConstraintsExtension/all_of.md)
- [Optional and](https://github.com/carlosV2/Can/blob/master/docs/ConstraintsExtension/optional_and.md)
- [Equals to](https://github.com/carlosV2/Can/blob/master/docs/ConstraintsExtension/equals_to.md)
- [like](https://github.com/carlosV2/Can/blob/master/docs/ConstraintsExtension/like.md)
6 changes: 6 additions & 0 deletions docs/PrimitiveTypesExtension/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ This function checks if the given array contains other non-registered keys. This
call to `withKey` in order to know which keys it needs to check.

You can use this function as `To::beArray()->withKey($key)->withNoOtherKeys()`.

### withOneValueExpected

This function checks if there is at least one value in the array that matches the given asserter.

You can use this function as `To::beArray()->withOneValueExpected(To::be*())`.
15 changes: 15 additions & 0 deletions spec/Extension/PrimitiveTypesExtension/ArrayAsserterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use carlosV2\Can\AsserterInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ArrayAsserterSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -85,4 +86,18 @@ function it_does_not_allow_the_expected_to_be_called_without_a_key(AsserterInter
{
$this->shouldThrow('\BadMethodCallException')->duringExpected($asserter);
}

function it_ensures_it_contains_expected_values(AsserterInterface $asserterK1, AsserterInterface $asserterK2)
{
$asserterK1->check('k1')->willReturn(true);
$asserterK1->check(Argument::type('string'))->willReturn(false);
$asserterK2->check('k2')->willReturn(true);
$asserterK2->check(Argument::type('string'))->willReturn(false);

$this->withOneValueExpected($asserterK1)->shouldReturn($this);
$this->withOneValueExpected($asserterK2)->shouldReturn($this);

$this->check(['k1', 'k2'])->shouldReturn(true);
$this->check(['k1', 'k3'])->shouldReturn(false);
}
}
30 changes: 30 additions & 0 deletions src/Extension/PrimitiveTypesExtension/ArrayAsserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class ArrayAsserter implements AsserterInterface
*/
private $max;

/**
* @var AsserterInterface[]
*/
private $singleValuesAsserters;

/**
* @var AsserterInterface
*/
Expand All @@ -40,6 +45,7 @@ public function __construct()
{
$this->keys = [];
$this->otherKeysAllowed = true;
$this->singleValuesAsserters = [];
}

/**
Expand Down Expand Up @@ -91,6 +97,18 @@ public function withKey($key)
return $this;
}

/**
* @param AsserterInterface $valueAsserter
*
* @return ArrayAsserter
*/
public function withOneValueExpected(AsserterInterface $valueAsserter)
{
$this->singleValuesAsserters[] = $valueAsserter;

return $this;
}

/**
* @param AsserterInterface $asserter
*
Expand Down Expand Up @@ -146,6 +164,18 @@ public function check($data)
}
}

if (count($this->singleValuesAsserters) > 0) {
foreach ($this->singleValuesAsserters as $singleValueAsserter) {
foreach ($data as $value) {
if ($singleValueAsserter->check($value)) {
continue 2;
}
}

return false;
}
}

foreach ($this->keys as $key => $asserter) {
if (!is_null($asserter)) {
if (!$asserter->check(@$data[$key])) {
Expand Down

0 comments on commit d9ba39b

Please sign in to comment.