Category: PHP

As explained in the Symfony testing, a unit test is a test against a single PHP class, all dependencies should not be tested within this class, so people use mock technic as much as tested services need dependencies, one of common problems is when a service depends on the connected user to do some stuff (check roles, inject user in other entity, … To ensure every created post has an author, we can either create a service with the create method or implement a listener to the doctrine event (PrePersist) in order to inject the connected user as author (I choosed for this example a simple PostService) $post->setAuthor($this->security->getUser()) : Injects the connected user as the post’s author. Create a simple unit test against create method to check if the post gets inserted in the database (How to test interacting with the database) and has an author : Note that this test fails as PostService::create() try to inject null in Post entity since User doesn’t exist.

So how we can inject a user in the token storage to let PostService retrieve it ?

To make it possible to inject a user in token storage, I’ll create(as mentioned above) a TokenStorageDecorator and make it available as a service (thanks to autowiring)
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners