PHPStorm: PHPUnit setUp and tearDown at the top of your class

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:

Screenshot of the "Editor > Code Style > PHP" configuration page of PHPStorm. The arrangement tab is open and an arrow pointing on the "new rule" button (an icon with a plus sign on it)

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:

PHPStorm, still the arrangement tab, as the last screenshot. A new rule is open, as modifier "protected" is chosen and name is "setUp"

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

Leave a Reply