php - PHPMD throws TooManyPublicMethods for Unit Tests

one text

I created a php unit test for my Doctrine ORM entity class that has 13 public getter and setter methods. These getter/setter methods not only get/set entity property, but has some extra logic within, so writing tests seems like a no-brainer.

If a getter is named setToken then my test would be named testSettingToken. Now Entity class itself does not get PHPMD.TooManyPublicMethods warning since methods starting get and set are ignored by the rule, while my unit test class thows it since methods starts with test.

Looking at the PHPUnit documentation and examples online, it seems like a common practise starting test methods with prefix of test. So why doesn't PHPMD ignore test methods the way it does it with getters and setters?

  • Should I separate the test class into multiple keeping the test prefix?
  • Should I remove test prefix to have test methods start as get/set?

I also noticed that there is a possibility to determine ignorepattern in the phpmd configuration file:

<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
  <properties>
    <property name="ignorepattern" value="(^(set|get|is|has|test))i"/>
  </properties>
</rule>

I trully avoid updating rulesets since I am trying to follow coding standards as much as possible. Could this still be the best practice?

Source