Building a Unit Test in Symfony 3 using JWT Token Authentication

When you’re working with JWT Token Authentication and you want to be a well behaved software developer and build out your Unit Tests (in this case we’re using phpunit), it can be somewhat challenging to tackle out of the box. My solution here uses an inherited “service” that generates the token that can be used in any subsequent requests:

In the interest of adhering to the DRY principle, this class exists specifically for the purpose of being called in every unit test as a means of authorizing each request (see below).

You may want to expand upon this to do some additional authorization or validation on the $token response, but in my use case I wanted the request to fail spectacularly if the $token were FALSE, so I’ve left it as such.

Leave a Reply

Your email address will not be published. Required fields are marked *