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