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

[9.x] Add additional uuid testing helpers #42619

Merged
merged 4 commits into from
Jun 2, 2022
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
58 changes: 57 additions & 1 deletion src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Support;

use Closure;
use Illuminate\Support\Traits\Macroable;
use JsonException;
use League\CommonMark\GithubFlavoredMarkdownConverter;
Expand Down Expand Up @@ -39,7 +40,7 @@ class Str
/**
* The callback that should be used to generate UUIDs.
*
* @var callable
* @var callable|null
*/
protected static $uuidFactory;

Expand Down Expand Up @@ -1112,6 +1113,61 @@ public static function createUuidsUsing(callable $factory = null)
static::$uuidFactory = $factory;
}

/**
* Set the sequence that will be used to generate UUIDs.
*
* @param array $sequence
* @param callable|null $whenMissing
Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance to change the callable type hint to something static analysis better understands? The recent updates to the Collections doc blocks were a really nice QoL improvement and I'd love for more code to be annotated like that :)

/**
 * @param null|callable(): UuidInterface $whenMissing
 */

Same for all the other methods that take a callback as a parameter.

* @return void
*/
public static function createUuidsUsingSequence(array $sequence, $whenMissing = null)
{
$next = 0;

$whenMissing ??= function () use (&$next) {
$factoryCache = static::$uuidFactory;

static::$uuidFactory = null;

$uuid = static::uuid();

static::$uuidFactory = $factoryCache;

$next++;

return $uuid;
};

static::createUuidsUsing(function () use (&$next, $sequence, $whenMissing) {
if (array_key_exists($next, $sequence)) {
return $sequence[$next++];
}

return $whenMissing();
});
}

/**
* Always return the same UUID when generating new UUIDs.
*
* @param \Closure|null $callback
* @return \Ramsey\Uuid\UuidInterface
*/
public static function freezeUuids(Closure $callback = null)
{
$uuid = Str::uuid();

Str::createUuidsUsing(fn () => $uuid);

if ($callback !== null) {
$callback($uuid);

Str::createUuidsNormally();
}

return $uuid;
}

/**
* Indicate that UUIDs should be created normally and not using a custom factory.
*
Expand Down
89 changes: 89 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,95 @@ public function testTransliterateStrict(string $value, string $expected): void
{
$this->assertSame($expected, Str::transliterate($value, '?', true));
}

public function testItCanFreezeUuids()
{
$this->assertNotSame((string) Str::uuid(), (string) Str::uuid());
$this->assertNotSame(Str::uuid(), Str::uuid());

$uuid = Str::freezeUuids();

$this->assertSame($uuid, Str::uuid());
$this->assertSame(Str::uuid(), Str::uuid());
$this->assertSame((string) $uuid, (string) Str::uuid());
$this->assertSame((string) Str::uuid(), (string) Str::uuid());

Str::createUuidsNormally();

$this->assertNotSame(Str::uuid(), Str::uuid());
$this->assertNotSame((string) Str::uuid(), (string) Str::uuid());
}

public function testItCanFreezeUuidsInAClosure()
{
$uuids = [];

$uuid = Str::freezeUuids(function ($uuid) use (&$uuids) {
$uuids[] = $uuid;
$uuids[] = Str::uuid();
$uuids[] = Str::uuid();
});

$this->assertSame($uuid, $uuids[0]);
$this->assertSame((string) $uuid, (string) $uuids[0]);
$this->assertSame((string) $uuids[0], (string) $uuids[1]);
$this->assertSame($uuids[0], $uuids[1]);
$this->assertSame((string) $uuids[0], (string) $uuids[1]);
$this->assertSame($uuids[1], $uuids[2]);
$this->assertSame((string) $uuids[1], (string) $uuids[2]);
$this->assertNotSame(Str::uuid(), Str::uuid());
$this->assertNotSame((string) Str::uuid(), (string) Str::uuid());

Str::createUuidsNormally();
}

public function testItCanSpecifyASquenceOfUuidsToUtilise()
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo in Squence

{
Str::createUuidsUsingSequence([
0 => ($zeroth = Str::uuid()),
1 => ($first = Str::uuid()),
// just generate a random one here...
3 => ($third = Str::uuid()),
// continue to generate random uuids...
]);

$retrieved = Str::uuid();
$this->assertSame($zeroth, $retrieved);
$this->assertSame((string) $zeroth, (string) $retrieved);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the second assertion necessary? We're already checking that they're the same instance.


$retrieved = Str::uuid();
$this->assertSame($first, $retrieved);
$this->assertSame((string) $first, (string) $retrieved);

$retrieved = Str::uuid();
$this->assertFalse(in_array($retrieved, [$zeroth, $first, $third], true));
$this->assertFalse(in_array((string) $retrieved, [(string) $zeroth, (string) $first, (string) $third], true));

$retrieved = Str::uuid();
$this->assertSame($third, $retrieved);
$this->assertSame((string) $third, (string) $retrieved);

$retrieved = Str::uuid();
$this->assertFalse(in_array($retrieved, [$zeroth, $first, $third], true));
$this->assertFalse(in_array((string) $retrieved, [(string) $zeroth, (string) $first, (string) $third], true));

Str::createUuidsNormally();
}

public function testItCanSpecifyAFallbackForASequence()
{
Str::createUuidsUsingSequence([Str::uuid(), Str::uuid()], fn () => throw new \Exception('Out of Uuids.'));
Str::uuid();
Str::uuid();

try {
$this->expectExceptionMessage('Out of Uuids.');
Str::uuid();
$this->fail();
} finally {
Str::createUuidsNormally();
}
}
}

class StringableObjectStub
Expand Down