Make 'toHaveKeys' accept multi-dimensional associative arrays

This commit is contained in:
Francescu Garoby
2022-04-29 09:54:12 +02:00
parent 6e120d60f6
commit 77b7181b08
4 changed files with 49 additions and 6 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Expectations;
use Pest\Expectation;
use Pest\Support\Arr;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;
@ -29,15 +30,19 @@ final class OppositeExpectation
/**
* Asserts that the value array not has the provided $keys.
*
* @param array<int, int|string> $keys
* @param array<int, int|string|array> $keys
*
* @return Expectation<TValue>
*/
public function toHaveKeys(array $keys): Expectation
{
foreach ($keys as $key) {
foreach ($keys as $k => $key) {
try {
$this->original->toHaveKey($key);
if (is_array($key)) {
$this->toHaveKeys(array_keys(Arr::dot($key, $k . '.')));
} else {
$this->original->toHaveKey($key);
}
} catch (ExpectationFailedException) {
continue;
}