Skip to content
Merged
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 @@ -1142,6 +1142,22 @@ public function toBeUuid(string $message = ''): self
return $this;
}

/**
* Asserts that the value is a 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 @@ -98,6 +98,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-9A-HJKMNP-TV-Z]{26}$/', $value) > 0;
}

/**
* Creates a describe block as `$describeDescription` → `$testDescription` format.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Features/Expect/toBeUlid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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('01ARZ3NDEKTSV4RRFFQ69G5FAV')->toBeUlid();
expect('01BX5ZZKBKACTAV9WEVGEMMVRE')->toBeUlid();
expect('7ZZZZZZZZZ0000000000000000')->toBeUlid();
});

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();
});