Skip to content

Commit dfc88c1

Browse files
docs: Add information about how to significantly improve the test suite (#1472)
* docs: Add information about how to significantly improve the test suite Hi, this iPR adds an inromation for the page about `JWT` authentication about how to make the tests fast. The issue boils down to the very slow password hashers that are used in tests by default. Each time we start a new project, or review the current one, we have to do it, because tests are extremely slow. This is only one of the many optimizations available for the end developer, but let's start here. Other ones are listed in by blog post: https://maks-rafalko.github.io/blog/2021-11-21/symfony-tests-performance/#using-more-simple-password-hasher * Update wording in core/jwt.md Co-authored-by: Alan Poulain <[email protected]> Co-authored-by: Alan Poulain <[email protected]>
1 parent 0bc822a commit dfc88c1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

core/jwt.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,26 @@ class AuthenticationTest extends ApiTestCase
344344
```
345345

346346
Refer to [Testing the API](../distribution/testing.md) for more information about testing API Platform.
347+
348+
### Improving Tests Suite Speed
349+
350+
Since now we have a `JWT` authentication, functional tests require us to log in each time we want to test an API endpoint. This is where [Password Hashers](https://symfony.com/doc/current/security/passwords.html) come into play.
351+
352+
Hashers are used for 2 reasons:
353+
354+
1. To generate a hash for a raw password (`self::$container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')`)
355+
2. To verify a password during authentication
356+
357+
While hashing and verifying 1 password is quite a fast operation, doing it hundreds or even thousands of times in a tests suite becomes a bottleneck, because reliable hashing algorithms are slow by their nature.
358+
359+
To significantly improve the test suite speed, we can use more simple password hasher specifically for the `test` environment.
360+
361+
```yaml
362+
# override in api/config/packages/test/security.yaml for test env
363+
security:
364+
password_hashers:
365+
App\Entity\User:
366+
algorithm: md5
367+
encode_as_base64: false
368+
iterations: 0
369+
```

0 commit comments

Comments
 (0)