From e3bfcbe5f14cded87cf7f4a169bcc7646dd829ba Mon Sep 17 00:00:00 2001 From: tal7aouy Date: Mon, 16 Sep 2024 13:36:34 +0100 Subject: [PATCH 1/2] Add slugify method --- src/Mixins/Expectation.php | 19 ++++++++++++++++++- src/Support/Str.php | 11 ++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index f802bc11..eb72fec0 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -661,7 +661,7 @@ final class Expectation { foreach ($keys as $k => $key) { if (is_array($key)) { - $this->toHaveKeys(array_keys(Arr::dot($key, $k.'.')), $message); + $this->toHaveKeys(array_keys(Arr::dot($key, $k . '.')), $message); } else { $this->toHaveKey($key, message: $message); } @@ -1159,4 +1159,21 @@ final class Expectation return $this; } + + /** + * Asserts that the value can be converted to a slug + * + * @return self + */ + public function toBeSlug(string $message = ''): self + { + if ($message === '') { + $message = "Failed asserting that {$this->value} can be converted to a slug."; + } + + $slug = Str::slugify((string) $this->value); + Assert::assertNotEmpty($slug, $message); + + return $this; + } } diff --git a/src/Support/Str.php b/src/Support/Str.php index 754749e7..ccaf06f7 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -61,7 +61,7 @@ final class Str { $code = str_replace('_', '__', $code); - $code = self::PREFIX.str_replace(' ', '_', $code); + $code = self::PREFIX . str_replace(' ', '_', $code); // sticks to PHP8.2 function naming rules https://www.php.net/manual/en/functions.user-defined.php return (string) preg_replace('/[^a-zA-Z0-9_\x80-\xff]/', '_', $code); @@ -116,4 +116,13 @@ final class Str { return (bool) filter_var($value, FILTER_VALIDATE_URL); } + + /** + * Converts the given `$target` to a URL-friendly "slug". + */ + public static function slugify(string $target): string + { + $target = preg_replace('/[^a-zA-Z0-9]+/', '-', $target); + return strtolower(trim($target, '-')); + } } From 92bc1decd92b795ae644923f20eba2a60c82f932 Mon Sep 17 00:00:00 2001 From: tal7aouy Date: Mon, 16 Sep 2024 13:41:13 +0100 Subject: [PATCH 2/2] Add tests for toBeSlug method --- tests/Features/Expect/toBeSlug.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/Features/Expect/toBeSlug.php diff --git a/tests/Features/Expect/toBeSlug.php b/tests/Features/Expect/toBeSlug.php new file mode 100644 index 00000000..2d7c19f8 --- /dev/null +++ b/tests/Features/Expect/toBeSlug.php @@ -0,0 +1,24 @@ +toBeSlug() + ->and('Another Test String')->toBeSlug(); +}); + +test('failures', function () { + expect('')->toBeSlug(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('')->toBeSlug('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('failures with default message', function () { + expect('')->toBeSlug(); +})->throws(ExpectationFailedException::class, 'Failed asserting that can be converted to a slug.'); + +test('not failures', function () { + expect('This is a Test String!')->not->toBeSlug(); +})->throws(ExpectationFailedException::class);