Skip to content
Merged
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
33 changes: 16 additions & 17 deletions components/uid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,16 @@ of the UUID parameters::
}
}

MockUuidFactory
===============
Testing UUIDs
~~~~~~~~~~~~~

.. versionadded:: 7.4

The :class:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory` class was introduced in Symfony 7.4.

The :class:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory` class allows you to
control the UUIDs generated during your tests, making them predictable and reproducible.
Many UUID versions include random or time-dependent parts, which makes them
hard to use in tests. The :class:`Symfony\\Component\\Uid\\Factory\\MockUuidFactory`
class allows you to control the UUIDs generated during your tests, making them
predictable and reproducible.

Suppose you have a service that generates a UUID for each new user::
First, make sure that your service generates UUID values using the
:class:`Symfony\\Component\\Uid\\Factory\\UuidFactory`::

use Symfony\Component\Uid\Factory\UuidFactory;
use Symfony\Component\Uid\Uuid;
Expand All @@ -473,15 +472,16 @@ Suppose you have a service that generates a UUID for each new user::
}
}

In your tests, you can use ``MockUuidFactory`` to inject predictable UUIDs and verify the expected behavior::
Then, in your tests, you can use ``MockUuidFactory`` to define a sequence of the
UUIDs that will be returned each time you generate one::

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Factory\MockUuidFactory;
use Symfony\Component\Uid\UuidV4;

class UserServiceTest extends TestCase
{
public function testCreateUserIdReturnsExpectedUuid()
public function testCreateUserId(): void
{
$factory = new MockUuidFactory([
UuidV4::fromString('11111111-1111-4111-8111-111111111111'),
Expand All @@ -495,15 +495,14 @@ In your tests, you can use ``MockUuidFactory`` to inject predictable UUIDs and v
}
}

.. warning::

``MockUuidFactory`` is intended for use in tests only and should never be used in production.
In addition to the ``create()`` method, the ``MockUuidFactory`` also works for
the ``randomBased()``, ``timeBased()``, and ``nameBased()`` methods. You can even
mix different UUID versions in the same sequence.

.. note::

- 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.
- You can mix different UUID versions in the same sequence.
- Throws an exception if the sequence is exhausted or the type does not match.
``MockUuidFactory`` throws an exception if the sequence is exhausted or the
available UUID types don't match the requested type.

.. _ulid:

Expand Down