Best practices to test protected methods with PHPUnit

前端 未结 8 1002
情深已故
情深已故 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:53

    I'm going to throw my hat into the ring here:

    I've used the __call hack with mixed degrees of success. The alternative I came up with was to use the Visitor pattern:

    1: generate a stdClass or custom class (to enforce type)

    2: prime that with the required method and arguments

    3: ensure that your SUT has an acceptVisitor method which will execute the method with the arguments specified in the visiting class

    4: inject it into the class you wish to test

    5: SUT injects the result of operation into the visitor

    6: apply your test conditions to the Visitor's result attribute

提交回复
热议问题