you should never need to change code to accommodate tests.
Why are you not using actingAs helper?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I've been using auth()->user()->username on many lines and noticed that when I want to do some tests I have to manually change all occurrences which is cumbersome.
(There are more properties which I sometimes need to change in multiple places but for this example I only use username)
For example:
class SomeController extends Controller
{
public username = auth()->user()->username;
...
}
Would that be a good practice to set the auth() details as the controller's property? This way I will only need to change it in one place if I ever need to test something.
Any downsides to that? Or is there a better way to do it?
Thanks
Please or to participate in this conversation.