Skip to content
Advertisement

Tag: phpunit

Mock private method with PHPUnit

I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example: How can I stub the result of the private method to test the some more code part of the public function. Solved partially reading here Answer Usually you just don’t test or mock the private & protected methods directy.

How to detect version of PHPUnit

I’m writing unit tests using an older version of PHPUnit (3.4) and thus can’t use all supported assertions listed in manual of 3.5 and 3.6. Though I could reengineer tests for instant support in my environment here, I’d like to make my tests dependent on current version of PHPUnit, so that it’s using assertsInstanceOf() as soon as my or any

PHPUnit: Verifying that array has a key with given value

Given the following class: And this testcase: How do I verify that $params (which is an array) and is passed to $Other->post() contains a key named ‘key1’ that has a value of ‘Value 1’? I do not want to verify all of the array – this is just a sample code, in actual code the passed array has a lot

Best practices to test protected methods with PHPUnit

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 these methods are static and short. Because most of the public methods make use of them, I will probably be able to safely remove the tests later. But for starting with

Advertisement