Skip to content

Commit

Permalink
doc: adjust test performance section
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 14, 2023
1 parent a0bbf5b commit 8666777
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1658,57 +1658,63 @@ accordingly. Your database is still reset before running your test suite but the
Using `Global State`_ that creates both ORM and ODM factories when using DAMADoctrineTestBundle
is not supported.

Miscellaneous
.............
Disable Debug Mode
..................

In your ``.env.test`` file, you can set ``APP_DEBUG=0`` to have your tests run without debug mode. This can speed up
your tests considerably. You will need to ensure you cache is cleared before running the test suite. The best place to
do this is in your ``tests/bootstrap.php``:

.. code-block:: php
// tests/bootstrap.php
// ...
if (false === (bool) $_SERVER['APP_DEBUG']) {
// ensure fresh cache
(new Symfony\Component\Filesystem\Filesystem())->remove(__DIR__.'/../var/cache/test');
}
Reduce Password Encoder *Work Factor*
.....................................

If you have a lot of tests that work with encoded passwords, this will cause these tests to be unnecessarily slow.
You can improve the speed by reducing the *work factor* of your encoder:

.. code-block:: yaml
# config/packages/test/security.yaml
encoders:
# use your user class name here
App\Entity\User:
# This should be the same value as in config/packages/security.yaml
algorithm: auto
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon
Pre-Encode Passwords
....................

Pre-encode user passwords with a known value via ``bin/console security:encode-password`` and set this in
``ModelFactory::getDefaults()``. Add the known value as a ``const`` on your factory:

.. code-block:: php
class UserFactory extends ModelFactory
{
public const DEFAULT_PASSWORD = '1234'; // the password used to create the pre-encoded version below
protected function getDefaults(): array
{
return [
// ...
'password' => '$argon2id$v=19$m=65536,t=4,p=1$pLFF3D2gnvDmxMuuqH4BrA$3vKfv0cw+6EaNspq9btVAYc+jCOqrmWRstInB2fRPeQ',
];
}
}
1. Disable debug mode when running tests. In your ``.env.test`` file, you can set ``APP_DEBUG=0`` to have your tests
run without debug mode. This can speed up your tests considerably. You will need to ensure you cache is cleared
before running the test suite. The best place to do this is in your ``tests/bootstrap.php``:

.. code-block:: php
// tests/bootstrap.php
// ...
if (false === (bool) $_SERVER['APP_DEBUG']) {
// ensure fresh cache
(new Symfony\Component\Filesystem\Filesystem())->remove(__DIR__.'/../var/cache/test');
}
2. Reduce password encoder *work factor*. If you have a lot of tests that work with encoded passwords, this will cause
these tests to be unnecessarily slow. You can improve the speed by reducing the *work factor* of your encoder:

.. code-block:: yaml
# config/packages/test/security.yaml
encoders:
# use your user class name here
App\Entity\User:
# This should be the same value as in config/packages/security.yaml
algorithm: auto
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon
3. Pre-encode user passwords with a known value via ``bin/console security:encode-password`` and set this in
``ModelFactory::getDefaults()``. Add the known value as a ``const`` on your factory:

.. code-block:: php
class UserFactory extends ModelFactory
{
public const DEFAULT_PASSWORD = '1234'; // the password used to create the pre-encoded version below
protected function getDefaults(): array
{
return [
// ...
'password' => '$argon2id$v=19$m=65536,t=4,p=1$pLFF3D2gnvDmxMuuqH4BrA$3vKfv0cw+6EaNspq9btVAYc+jCOqrmWRstInB2fRPeQ',
];
}
}
Now, in your tests, when you need access to the unencoded password for a user created with ``UserFactory``, use
``UserFactory::DEFAULT_PASSWORD``.
Now, in your tests, when you need access to the unencoded password for a user created with ``UserFactory``, use
``UserFactory::DEFAULT_PASSWORD``.

Non-Kernel Tests
~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 8666777

Please sign in to comment.