8000 [testing] document improving test speed by reducing encoder work factor · symfony/symfony-docs@934d416 · GitHub
[go: up one dir, main page]

Skip to content

Commit 934d416

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

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

testing/http_authentication.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,66 @@ 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+
30+
encoders:
31+
# use your user class name here
32+
App\Entity\User:
33+
# This should be the same value as in config/packages/security.yaml
34+
algorithm: auto
35+
cost: 4 # Lowest possible value for bcrypt
36+
time_cost: 3 # Lowest possible value for argon
37+
memory_cost: 10 # Lowest possible value for argon
38+
39+
.. code-block:: xml
40+
41+
<!-- config/packages/test/security.xml -->
42+
<?xml version="1.0" encoding="UTF-8"?>
43+
<srv:container xmlns="http://symfony.com/schema/dic/security"
44+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
45+
xmlns:srv="http://symfony.com/schema/dic/services"
46+
xsi:schemaLocation="http://symfony.com/schema/dic/services
47+
https://symfony.com/schema/dic/services/services-1.0.xsd">
48+
49+
<config>
50+
<encoder class="App\Entity\User"
51+
algorithm="auto"
52+
cost="4"
53+
time_cost="3"
54+
memory_cost="10"
55+
/>
56+
</config>
57+
</srv:container>
58+
59+
.. code-block:: php
60+
61+
// config/packages/test/security.php
62+
use App\Entity\User;
63+
64+
$container->loadFromExtension('security', [
65+
'encoders' => [
66+
User::class => [
67+
'algorithm' => 'auto',
68+
'cost' => 4,
69+
'time_cost' => 3,
70+
'memory_cost' => 10,
71+
]
72+
],
73+
]);
74+
1575
Using a Faster Authentication Mechanism Only for Tests
1676
------------------------------------------------------
1777

0 commit comments

Comments
 (0)
0