Skip to content

Commit 9f79944

Browse files
[9.x] Ensure freezeUuids always resets UUID creation after exception in callback (#44018)
* add freezeFor string helper * update freezeUuids method
1 parent 16e2c71 commit 9f79944

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Illuminate/Support/Str.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,9 +1285,11 @@ public static function freezeUuids(Closure $callback = null)
12851285
Str::createUuidsUsing(fn () => $uuid);
12861286

12871287
if ($callback !== null) {
1288-
$callback($uuid);
1289-
1290-
Str::createUuidsNormally();
1288+
try {
1289+
$callback($uuid);
1290+
} finally {
1291+
Str::createUuidsNormally();
1292+
}
12911293
}
12921294

12931295
return $uuid;

tests/Support/SupportStrTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,19 @@ public function testItCanFreezeUuidsInAClosure()
991991
Str::createUuidsNormally();
992992
}
993993

994+
public function testItCreatesUuidsNormallyAfterFailureWithinFreezeMethod()
995+
{
996+
try {
997+
Str::freezeUuids(function () {
998+
Str::createUuidsUsing(fn () => Str::of('1234'));
999+
$this->assertSame('1234', Str::uuid()->toString());
1000+
throw new \Exception('Something failed.');
1001+
});
1002+
} catch (\Exception $e) {
1003+
$this->assertNotSame('1234', Str::uuid()->toString());
1004+
}
1005+
}
1006+
9941007
public function testItCanSpecifyASequenceOfUuidsToUtilise()
9951008
{
9961009
Str::createUuidsUsingSequence([

0 commit comments

Comments
 (0)