You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/uid.rst
+59Lines changed: 59 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -433,6 +433,65 @@ of the UUID parameters::
433
433
}
434
434
}
435
435
436
+
MockUuidFactory
437
+
===============
438
+
439
+
.. versionadded:: 7.4
440
+
The :class:`Symfony\Component\Uid\Factory\MockUuidFactory` class was introduced in Symfony 7.4.
441
+
442
+
The :class:`Symfony\Component\Uid\Factory\MockUuidFactory` class allows you to control the UUIDs generated during your tests, making them predictable and reproducible.
443
+
444
+
Suppose you have a service that generates a UUID for each new user:
445
+
446
+
.. code-block:: php
447
+
448
+
use Symfony\Component\Uid\Factory\UuidFactory;
449
+
use Symfony\Component\Uid\Uuid;
450
+
451
+
class UserService
452
+
{
453
+
public function __construct(private UuidFactory $uuidFactory) {}
454
+
455
+
public function createUserId(): string
456
+
{
457
+
return $this->uuidFactory->create()->toRfc4122();
458
+
}
459
+
}
460
+
461
+
In your tests, you can use ``MockUuidFactory`` to inject predictable UUIDs and verify the expected behavior:
462
+
463
+
.. code-block:: php
464
+
465
+
use PHPUnit\Framework\TestCase;
466
+
use Symfony\Component\Uid\Factory\MockUuidFactory;
467
+
use Symfony\Component\Uid\UuidV4;
468
+
469
+
class UserServiceTest extends TestCase
470
+
{
471
+
public function testCreateUserIdReturnsExpectedUuid()
``MockUuidFactory`` is intended for use in tests only and should never be used in production.
488
+
489
+
.. note::
490
+
491
+
- 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.
492
+
- You can mix different UUID versions in the same sequence.
493
+
- Throws an exception if the sequence is exhausted or the type does not match.
0 commit comments