Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb5c5035

Browse files
momito69OskarStark
authored andcommitted
[Uid] Add documentation for MockUuidFactory usage in tests
1 parent32ee550 commitb5c5035

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

‎components/uid.rst‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,66 @@ of the UUID parameters::
433433
}
434434
}
435435

436+
MockUuidFactory
437+
===============
438+
439+
..versionadded::7.4
440+
441+
The:class:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory` class was introduced in Symfony 7.4.
442+
443+
The:class:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory` class allows you to
444+
control the UUIDs generated during your tests, making them predictable and reproducible.
445+
446+
Suppose you have a service that generates a UUID for each new user::
447+
448+
use Symfony\Component\Uid\Factory\UuidFactory;
449+
use Symfony\Component\Uid\Uuid;
450+
451+
class UserService
452+
{
453+
public function __construct(
454+
private UuidFactory $uuidFactory
455+
){
456+
}
457+
458+
public function createUserId(): string
459+
{
460+
return $this->uuidFactory->create()->toRfc4122();
461+
}
462+
}
463+
464+
In your tests, you can use ``MockUuidFactory`` to inject predictable UUIDs and verify the expected behavior::
465+
466+
use PHPUnit\Framework\TestCase;
467+
use Symfony\Component\Uid\Factory\MockUuidFactory;
468+
use Symfony\Component\Uid\UuidV4;
469+
470+
class UserServiceTest extends TestCase
471+
{
472+
public function testCreateUserIdReturnsExpectedUuid()
473+
{
474+
$factory = new MockUuidFactory([
475+
UuidV4::fromString('11111111-1111-4111-8111-111111111111'),
476+
UuidV4::fromString('22222222-2222-4222-8222-222222222222'),
477+
]);
478+
479+
$service = new UserService($factory);
480+
481+
$this->assertSame('11111111-1111-4111-8111-111111111111', $service->createUserId());
482+
$this->assertSame('22222222-2222-4222-8222-222222222222', $service->createUserId());
483+
}
484+
}
485+
486+
..warning::
487+
488+
``MockUuidFactory`` is intended for use in tests only and should never be used in production.
489+
490+
..note::
491+
492+
- Supports the:method:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory::create`,:method:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory::randomBased`,:method:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory::timeBased`, and:method:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory::nameBased` methods.
493+
- You can mix different UUID versions in the same sequence.
494+
- Throws an exception if the sequence is exhausted or the type does not match.
495+
436496
.. _ulid:
437497

438498
ULIDs

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp