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

feat: add toBeUlid() expectation #1326

Open
wants to merge 1 commit 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
16 changes: 16 additions & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,22 @@ public function toBeUuid(string $message = ''): self
return $this;
}

/**
* Asserts that the value is ULID.
*
* @return self<TValue>
*/
public function toBeUlid(string $message = ''): self
{
if (! is_string($this->value)) {
InvalidExpectationValue::expected('string');
}

Assert::assertTrue(Str::isUlid($this->value), $message);

return $this;
}

/**
* Asserts that the value is between 2 specified values
*
Expand Down
8 changes: 8 additions & 0 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public static function isUuid(string $value): bool
return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
}

/**
* Determine if a given value is a valid ULID.
*/
public static function isUlid(string $value): bool
{
return preg_match('/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/iD', $value) > 0;
}

/**
* Creates a describe block as `$describeDescription` → `$testDescription` format.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Features/Expect/toBeUlid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Pest\Exceptions\InvalidExpectationValue;
use PHPUnit\Framework\ExpectationFailedException;

test('failures with wrong type', function () {
expect([])->toBeUlid();
})->throws(InvalidExpectationValue::class, 'Invalid expectation value type. Expected [string].');

test('pass', function () {
expect('01JEWKD14JZJC6GKVQG8CAF93G')->toBeUlid();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I'm just walking around pest and I would like to say about case "00000000-0000-0000-0000-000000000001", We are using a lot this format a lot in our huge app.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@don-tre As far as I understand GUID has no difference with UUID, so you could still use expect('value')->toBeUuid() https://en.wikipedia.org/wiki/Universally_unique_identifier

But for better semantics you could add your own expectation in Pest.php

// Pest.php
expect()
    ->extend('toBeGuid', fn (string $message = '') => expect($this->value)
        ->toBeUuid($message));

// On your tests
expect('value')->toBeGuid()

});

test('failures', function () {
expect('foo')->toBeUlid();
})->throws(ExpectationFailedException::class);

test('failures with message', function () {
expect('bar')->toBeUlid('oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');

test('not failures', function () {
expect('foo')->not->toBeUlid();
});