assertArrayHasKeys + assertArrayHasNotKeys

master
Anthony Axenov 2022-01-21 05:52:25 +00:00
parent e9f993b396
commit ed013d9321
1 changed files with 31 additions and 6 deletions

View File

@ -7,11 +7,6 @@ namespace Tests;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
/**
* Useful phpunit asserts
*
* @see https://gist.github.com/anthonyaxenov/3c0d56b8884856a58c75a6f787006546/edit
*/
class BasicTestCase extends TestCase
{
/**
@ -106,4 +101,34 @@ class BasicTestCase extends TestCase
|| $this->checkExtendsClasses([Collection::class], $value)
);
}
}
/**
* Asserts that $array has all the $keys
*
* @param array $keys Keys to check
* @param array|ArrayAccess|Arrayable $array Array in which to check keys presence
* @return void
*/
public function assertArrayHasKeys(array $keys, array|ArrayAccess|Arrayable $array): void
{
$array instanceof Arrayable && $array = $array->toArray();
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $array);
}
}
/**
* Asserts that $array has not all the $keys
*
* @param array $keys Keys to check
* @param array|ArrayAccess|Arrayable $array Array in which to check keys absence
* @return void
*/
public function assertArrayHasNotKeys(array $keys, array|ArrayAccess|Arrayable $array): void
{
$array instanceof Arrayable && $array = $array->toArray();
foreach ($keys as $key) {
$this->assertArrayNotHasKey($key, $array);
}
}
}