mardi 10 avril 2018

Using codeCoverageIgnore in PHPUnit

Sometimes you have code that you can't tests or doesn't seem to be useful to test and that you may want to ignore during code coverage analysis.

I'm not entirely sure when this applies. What are good signs that you should ignore a method from code coverage analysis.

Let me give an example to illustrate this "problem".

You have abstract class A:

abstract class A
{
    /**
     * Get a default value.
     *
     * @return string
     */
    protected static function getValue(): string
    {
        return 'X';
    }
}

class B extends A
{

    /**
     * I will override getValue() on class A.
     *
     * @return string
     */
    protected static function getValue(): string
    {
        return 'Y';
    }
}

Import here is that those static method return static values, they do not make calls to databases or set values dynamically.

It seems pointless to test that B truly returns Y. Especially because the return type needs to be compatible with that off A (you can't override a method to return a different type).

It seems to me that you would want to exclude this from analysis (or maybe there would be a different, better, approach to such problems).

I would be glad to have some advice on this issue.

Aucun commentaire:

Enregistrer un commentaire