If you are like me, you prefer your setUp
and tearDown
methods in your PHPunit tests to be at the top of your classes, so it is easy to see what is going on. But PHPStorm orders it’s methods by default by modifiers, which makes setUp
and tearDown
normally end up at the bottom. But this can be easily changed.
Open your settings, go to Editor > Code Style > PHP and then the last tab “Arrangement”. Then click on constructor, so the new rule is added right below it and add a new rule:

Next add setUp
as name and protected, just to be sure. And hit apply. Now if you rearrange your code (in the macOS menu: Code > Rearrange code) the setUp methods ends up at the top.
And this is how the rule should look like:

If you have more setUp methods in your code and you get problems, try to be more strict with this rule, so it works as you like it!
To make tearDown
end up below it, just add a second rule!
One thought on “PHPStorm: PHPUnit setUp and tearDown at the top of your class”