mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #1256 from mazesec/add-slugify-method
Add slugify Method to Str Class and toBeSlug Assertion to Expectation Class
This commit is contained in:
@ -660,7 +660,7 @@ final class Expectation
|
|||||||
{
|
{
|
||||||
foreach ($keys as $k => $key) {
|
foreach ($keys as $k => $key) {
|
||||||
if (is_array($key)) {
|
if (is_array($key)) {
|
||||||
$this->toHaveKeys(array_keys(Arr::dot($key, $k.'.')), $message);
|
$this->toHaveKeys(array_keys(Arr::dot($key, $k . '.')), $message);
|
||||||
} else {
|
} else {
|
||||||
$this->toHaveKey($key, message: $message);
|
$this->toHaveKey($key, message: $message);
|
||||||
}
|
}
|
||||||
@ -1158,4 +1158,21 @@ final class Expectation
|
|||||||
|
|
||||||
return $this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,7 +58,7 @@ final class Str
|
|||||||
{
|
{
|
||||||
$code = str_replace('_', '__', $code);
|
$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
|
// 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);
|
return (string) preg_replace('/[^a-zA-Z0-9_\x80-\xff]/', '_', $code);
|
||||||
@ -117,4 +117,13 @@ final class Str
|
|||||||
{
|
{
|
||||||
return (bool) filter_var($value, FILTER_VALIDATE_URL);
|
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
tests/Features/Expect/toBeSlug.php
Normal file
24
tests/Features/Expect/toBeSlug.php
Normal file
@ -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);
|
||||||
Reference in New Issue
Block a user