From 5d81cf0d4c3aa2ad67a0933b8f6a2c4623468d1e Mon Sep 17 00:00:00 2001 From: Maurizio Date: Thu, 17 Aug 2023 20:51:14 +0200 Subject: [PATCH] feat: add `toHaveCamelCaseKeys` --- src/Expectation.php | 22 +++++++++++++ tests/Features/Expect/toHaveCamelCaseKeys.php | 31 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/Features/Expect/toHaveCamelCaseKeys.php diff --git a/src/Expectation.php b/src/Expectation.php index c5f4c411..df725059 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -784,4 +784,26 @@ final class Expectation return $this; } + + /** + * Asserts that the given expectation is iterable and contains camelCase keys. + * + * @return self + */ + 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; + } } diff --git a/tests/Features/Expect/toHaveCamelCaseKeys.php b/tests/Features/Expect/toHaveCamelCaseKeys.php new file mode 100644 index 00000000..4fc73428 --- /dev/null +++ b/tests/Features/Expect/toHaveCamelCaseKeys.php @@ -0,0 +1,31 @@ + 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);