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

Conversation

timacdonald
Copy link
Member

This PR adds some testing helpers to the generation of UUIDs via the Str helper.

Currently in order to control UUID creation, you need to do the following...

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

// do stuff...

Str::uuid() === Str::uuid() === $uuid;

// cleanup...

Str::createUuidsNormally();

This locks the helper so that it only returns the one UUID throughout the whole process.

I find myself always having to re-learn how this works and that I have to capture a UUID first or needing to fake a specific UUID generation.

With this PR the following is now possible...

Str::freezeUuids();

// do stuff...

Str::uuid() === Str::uuid() === $uuid;

// cleanup...

Str::createUuidsNormally();

You may also pass a closure to only freeze creation for the duration of the closure...

Str::freezeUuids(function ($uuid) {
    // do stuff...
    Str::uuid() === Str::uuid() === $uuid;
});

Finally, it is also possible to provide a sequence of UUIDs to return.

Str::createUuidsUsingSequence([
    $zeroth = Str::uuid(),
    $first = Str::uuid(),
]);

Str::uuid() === $zeroth;
Str::uuid() === $first;
Str::uuid(); // back to random UUIDs

You may specify the keys if there are only certain instances you wish to sequence....

Str::createUuidsUsingSequence([
    0 => ($zeroth = Str::uuid()),
    3 => ($third = Str::uuid()),
]);

Str::uuid() === $zeroth;
Str::uuid(); // random
Str::uuid(); // random
Str::uuid() === $third;

If you would like to throw an exception when there is no more UUIDs in the array, you pass a closure to control what happens when finding an index that has no specified UUID...

Str::createUuidsUsingSequence([
    $zeroth = Str::uuid(),
    $first = Str::uuid(),
], fn () => throw new Exception('Sequence ended'));

Str::uuid() === $zeroth;
Str::uuid() === $first;
Str::uuid(); // THROWS

This closure is also called when there is a missing key if you specify specific keys in the sequence...

Str::createUuidsUsingSequence([
    0 => Str::uuid(),
    3 => Str::uuid(),
], fn () => throw new Exception('Sequence ended'));

Str::uuid() === $zeroth;
Str::uuid(); // THROWS

@timacdonald timacdonald changed the title add uuid testing helpers [9.x] Add additional uuid testing helpers Jun 2, 2022
@timacdonald
Copy link
Member Author

Please feel free to suggest / improve the naming. Went down the rabbit hole of "recycle" like words, but this felt the best at the end of the day for me.

@driesvints
Copy link
Member

Str::createUuidsNormally(); => Str::unfreezeUuids

Str::createUuidsUsingSequence => Str::freezeUuidsAs

* 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.

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


$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.

@ksassnowski
Copy link
Contributor

Str::createUuidsNormally(); => Str::unfreezeUuids

Str::createUuidsUsingSequence => Str::freezeUuidsAs

I think I prefer Str::createUuidsUsingSequence since the difference between Str::freezeUuidsAs and Str::freezeUuids isn't immediately obvious to me. Laravel also already uses "sequence" to describe basically the same behavior for model factories.

@timacdonald
Copy link
Member Author

If we dig this, I’ll submit another for Str::random();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants