Skip to content

Commit 1dddc33

Browse files
committed
[testing] document improving test speed by reducing encoder work factor
1 parent 5514b60 commit 1dddc33

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

testing/http_authentication.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,65 @@ OAuth authentication services.
1212
This article explains the two most popular techniques to avoid these issues and
1313
create fast tests when using authentication.
1414

15+
Improving Password Encoder Speed in Tests
16+
-----------------------------------------
17+
18+
By default, password encoders are resource intensive and take time. This is
19+
important to generate secure password hashes. In tests however, secure hashes
20+
are not important, waste resources and increase test times. You can reduce
21+
the *work factor* for your encoders by adding the following *only in your test
22+
environment*:
23+
24+
.. configuration-block::
25+
26+
.. code-block:: yaml
27+
28+
# config/packages/test/security.yaml
29+
encoders:
30+
# use your user class name here
31+
App\Entity\User:
32+
# This should be the same value as in config/packages/security.yaml
33+
algorithm: auto
34+
cost: 4 # Lowest possible value for bcrypt
35+
time_cost: 3 # Lowest possible value for argon
36+
memory_cost: 10 # Lowest possible value for argon
37+
38+
.. code-block:: xml
39+
40+
<!-- config/packages/test/security.xml -->
41+
<?xml version="1.0" encoding="UTF-8"?>
42+
<srv:container xmlns="http://symfony.com/schema/dic/security"
43+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44+
xmlns:srv="http://symfony.com/schema/dic/services"
45+
xsi:schemaLocation="http://symfony.com/schema/dic/services
46+
https://symfony.com/schema/dic/services/services-1.0.xsd">
47+
48+
<config>
49+
<encoder class="App\Entity\User"
50+
algorithm="auto"
51+
cost="4"
52+
time_cost="3"
53+
memory_cost="10"
54+
/>
55+
</config>
56+
</srv:container>
57+
58+
.. code-block:: php
59+
60+
// config/packages/test/security.php
61+
use App\Entity\User;
62+
63+
$container->loadFromExtension('security', [
64+
'encoders' => [
65+
User::class => [
66+
'algorithm' => 'auto',
67+
'cost' => 4,
68+
'time_cost' => 3,
69+
'memory_cost' => 10,
70+
]
71+
],
72+
]);
73+
1574
Using a Faster Authentication Mechanism Only for Tests
1675
------------------------------------------------------
1776

0 commit comments

Comments
 (0)