From 54e00dd4dc0e277a92afd4ee93d20519c0c4a1a1 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 12 Aug 2023 16:41:15 +0100 Subject: [PATCH 1/2] add expectations for uppercase, lowercase, alpha and alphanumeric --- src/Mixins/Expectation.php | 48 ++++++++++++++++++++++ tests/Features/Expect/toBeAlpha.php | 20 +++++++++ tests/Features/Expect/toBeAlphaNumeric.php | 20 +++++++++ tests/Features/Expect/toBeLowercase.php | 20 +++++++++ tests/Features/Expect/toBeUppercase.php | 20 +++++++++ 5 files changed, 128 insertions(+) create mode 100644 tests/Features/Expect/toBeAlpha.php create mode 100644 tests/Features/Expect/toBeAlphaNumeric.php create mode 100644 tests/Features/Expect/toBeLowercase.php create mode 100644 tests/Features/Expect/toBeUppercase.php diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index d3d8cbd3..19038bfe 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -961,4 +961,52 @@ final class Expectation return $this->exporter->shortenedExport($value); } + + /** + * Asserts that the value is uppercase. + * + * @return self + */ + public function toBeUppercase(string $message = ''): self + { + Assert::assertTrue(ctype_upper($this->value), $message); + + return $this; + } + + /** + * Asserts that the value is lowercase. + * + * @return self + */ + public function toBeLowercase(string $message = ''): self + { + Assert::assertTrue(ctype_lower($this->value), $message); + + return $this; + } + + /** + * Asserts that the value is alphanumeric. + * + * @return self + */ + public function toBeAlphaNumeric(string $message = ''): self + { + Assert::assertTrue(ctype_alnum($this->value), $message); + + return $this; + } + + /** + * Asserts that the value is alpha. + * + * @return self + */ + public function toBeAlpha(string $message = ''): self + { + Assert::assertTrue(ctype_alpha($this->value), $message); + + return $this; + } } diff --git a/tests/Features/Expect/toBeAlpha.php b/tests/Features/Expect/toBeAlpha.php new file mode 100644 index 00000000..659c2de1 --- /dev/null +++ b/tests/Features/Expect/toBeAlpha.php @@ -0,0 +1,20 @@ +toBeAlpha(); + expect('123')->not->toBeAlpha(); +}); + +test('failures', function () { + expect('123')->toBeAlpha(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('123')->toBeAlpha('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('abc')->not->toBeAlpha(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Features/Expect/toBeAlphaNumeric.php b/tests/Features/Expect/toBeAlphaNumeric.php new file mode 100644 index 00000000..5f4670e8 --- /dev/null +++ b/tests/Features/Expect/toBeAlphaNumeric.php @@ -0,0 +1,20 @@ +toBeAlphaNumeric(); + expect('-')->not->toBeAlphaNumeric(); +}); + +test('failures', function () { + expect('-')->toBeAlphaNumeric(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('-')->toBeAlphaNumeric('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('abc123')->not->toBeAlphaNumeric(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Features/Expect/toBeLowercase.php b/tests/Features/Expect/toBeLowercase.php new file mode 100644 index 00000000..4eda711b --- /dev/null +++ b/tests/Features/Expect/toBeLowercase.php @@ -0,0 +1,20 @@ +toBeLowercase(); + expect('UPPERCASE')->not->toBeLowercase(); +}); + +test('failures', function () { + expect('UPPERCASE')->toBeLowercase(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('UPPERCASE')->toBeLowercase('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('lowercase')->not->toBeLowercase(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Features/Expect/toBeUppercase.php b/tests/Features/Expect/toBeUppercase.php new file mode 100644 index 00000000..7a1345d7 --- /dev/null +++ b/tests/Features/Expect/toBeUppercase.php @@ -0,0 +1,20 @@ +toBeUppercase(); + expect('lowercase')->not->toBeUppercase(); +}); + +test('failures', function () { + expect('lowercase')->toBeUppercase(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('lowercase')->toBeUppercase('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('UPPERCASE')->not->toBeUppercase(); +})->throws(ExpectationFailedException::class); From f996a48dfa3e39172fdea0b234975dfee5200f3a Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 12 Aug 2023 18:14:38 +0100 Subject: [PATCH 2/2] fix refacto check --- src/Mixins/Expectation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 19038bfe..56e1da09 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -969,7 +969,7 @@ final class Expectation */ public function toBeUppercase(string $message = ''): self { - Assert::assertTrue(ctype_upper($this->value), $message); + Assert::assertTrue(ctype_upper((string) $this->value), $message); return $this; } @@ -981,7 +981,7 @@ final class Expectation */ public function toBeLowercase(string $message = ''): self { - Assert::assertTrue(ctype_lower($this->value), $message); + Assert::assertTrue(ctype_lower((string) $this->value), $message); return $this; } @@ -993,7 +993,7 @@ final class Expectation */ public function toBeAlphaNumeric(string $message = ''): self { - Assert::assertTrue(ctype_alnum($this->value), $message); + Assert::assertTrue(ctype_alnum((string) $this->value), $message); return $this; } @@ -1005,7 +1005,7 @@ final class Expectation */ public function toBeAlpha(string $message = ''): self { - Assert::assertTrue(ctype_alpha($this->value), $message); + Assert::assertTrue(ctype_alpha((string) $this->value), $message); return $this; }