feat: add toHaveStudlyCaseKeys

This commit is contained in:
Maurizio
2023-08-17 20:51:26 +02:00
parent 5d81cf0d4c
commit 478144fb35
2 changed files with 53 additions and 0 deletions

View File

@ -806,4 +806,26 @@ final class Expectation
return $this;
}
/**
* Asserts that the given expectation is iterable and contains StudlyCase keys.
*
* @return self<TValue>
*/
public function toHaveStudlyCaseKeys(string $message = ''): self
{
if (! is_iterable($this->value)) {
InvalidExpectationValue::expected('iterable');
}
foreach ($this->value as $k => $item) {
$this->and($k)->toBeStudlyCase($message);
if (is_array($item)) {
$this->and($item)->toHaveStudlyCaseKeys($message);
}
}
return $this;
}
}

View File

@ -0,0 +1,31 @@
<?php
use Pest\Exceptions\InvalidExpectationValue;
use PHPUnit\Framework\ExpectationFailedException;
$array = [
'Studly' => true,
'StudlyCase' => [
'Studly' => true,
'StudlyCase' => [
'Studly' => true,
'StudlyCase' => true,
],
],
];
test('pass', function () use ($array) {
expect($array)->toHaveStudlyCaseKeys();
});
test('failures', function () {
expect('not-an-array')->toHaveStudlyCaseKeys();
})->throws(InvalidExpectationValue::class);
test('failures with message', function () use ($array) {
expect($array)->not->toHaveStudlyCaseKeys('oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');
test('not failures', function () use ($array) {
expect($array)->not->toHaveStudlyCaseKeys();
})->throws(ExpectationFailedException::class);