Best practices to test protected methods with PHPUnit

前端 未结 8 998
情深已故
情深已故 2020-11-28 17:14

I found the discussion on Do you test private method informative.

I have decided, that in some classes, I want to have protected methods, but test them. Some of thes

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 17:37

    I suggest following workaround for "Henrik Paul"'s workaround/idea :)

    You know names of private methods of your class. For example they are like _add(), _edit(), _delete() etc.

    Hence when you want to test it from aspect of unit-testing, just call private methods by prefixing and/or suffixing some common word (for example _addPhpunit) so that when __call() method is called (since method _addPhpunit() doesn't exist) of owner class, you just put necessary code in __call() method to remove prefixed/suffixed word/s (Phpunit) and then to call that deduced private method from there. This is another good use of magic methods.

    Try it out.

提交回复
热议问题