Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slugify Method to Str Class and toBeSlug Assertion to Expectation Class #1256

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ public function toHaveKeys(array $keys, string $message = ''): self
{
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);
}
Expand Down Expand Up @@ -1159,4 +1159,21 @@ public function toBeUrl(string $message = ''): self

return $this;
}

/**
* Asserts that the value can be converted to a slug
*
* @return self<TValue>
*/
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;
}
}
11 changes: 10 additions & 1 deletion src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function evaluable(string $code): string
{
$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);
Expand Down Expand Up @@ -116,4 +116,13 @@ public static function isUrl(string $value): bool
{
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, '-'));
}
}
24 changes: 24 additions & 0 deletions tests/Features/Expect/toBeSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect('This is a Test String!')->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);