feat: adds toMatchArray

This commit is contained in:
Nuno Maduro
2020-11-23 21:26:02 +01:00
parent cdf0a38145
commit 7bea51fe09
3 changed files with 60 additions and 3 deletions

View File

@ -408,10 +408,17 @@ final class Expectation
* Asserts that the value array has the provided $key.
*
* @param string|int $key
* @param mixed $value
*/
public function toHaveKey($key): Expectation
public function toHaveKey($key, $value = null): Expectation
{
Assert::assertArrayHasKey($key, $this->value);
$array = (array) $this->value;
Assert::assertArrayHasKey($key, $array);
if (func_num_args() > 1) {
Assert::assertEquals($value, $array[$key]);
}
return $this;
}
@ -490,6 +497,20 @@ final class Expectation
return $this;
}
/**
* Asserts that the value array matches the given array subset.
*
* @param array<int|string, mixed> $array
*/
public function toMatchArray($array): Expectation
{
foreach ($array as $property => $value) {
$this->toHaveKey($property, $value);
}
return $this;
}
/**
* Asserts that the value object matches a subset
* of the properties of an given object.