mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 18:27:23 +01:00
feat: add toHaveCamelCaseKeys
This commit is contained in:
@ -784,4 +784,26 @@ final class Expectation
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation is iterable and contains camelCase keys.
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toHaveCamelCaseKeys(string $message = ''): self
|
||||||
|
{
|
||||||
|
if (! is_iterable($this->value)) {
|
||||||
|
InvalidExpectationValue::expected('iterable');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->value as $k => $item) {
|
||||||
|
$this->and($k)->toBeCamelCase($message);
|
||||||
|
|
||||||
|
if (is_array($item)) {
|
||||||
|
$this->and($item)->toHaveCamelCaseKeys($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
tests/Features/Expect/toHaveCamelCaseKeys.php
Normal file
31
tests/Features/Expect/toHaveCamelCaseKeys.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Exceptions\InvalidExpectationValue;
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
$array = [
|
||||||
|
'camel' => true,
|
||||||
|
'camelCase' => [
|
||||||
|
'camel' => true,
|
||||||
|
'camelCase' => [
|
||||||
|
'camel' => true,
|
||||||
|
'camelCase' => true,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
test('pass', function () use ($array) {
|
||||||
|
expect($array)->toHaveCamelCaseKeys();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('not-an-array')->toHaveCamelCaseKeys();
|
||||||
|
})->throws(InvalidExpectationValue::class);
|
||||||
|
|
||||||
|
test('failures with message', function () use ($array) {
|
||||||
|
expect($array)->not->toHaveCamelCaseKeys('oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () use ($array) {
|
||||||
|
expect($array)->not->toHaveCamelCaseKeys();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user