How to mock different methods of the same class and how to mock one method with different arguments

I want to mock different methods of the same class and mock one method with different argument values. Using standard PHPUnit.

Mocks in PHPUnit are being created using getMock() method. It belongs to PHPUnit_Framework_TestCase class, that is described in .../PEAR/PHPUnit/Framework/TestCase.php file.
Here is the specification of PHPUnit_Framework_TestCase::getMock() method:

Here is the specification of PHPUnit_Framework_TestCase::getMockForAbstractClass() method:

 

1. Two methods of the same class
Say we have a class MyClass with some method root(), that calculates a root of n-grade that we need to test. Say that root’s grade is taken from getRootBase() method and a precision is set in program configuration and taken from getConfig('precision') method. Thus to test method root() we need to mock two other methods: getRootBase() and getConfig('precision')

Now lets create two method mocks. It’s quite simple.

2. One method with different argument values
Say we have a multiplication method multi() that needs a type and a modificator from configuration. Thus to test our multi() we need a mock for getConfig() that will simulate it with different arguments. I use for this returnCallback() that analyses arguments and returns the right value.

Now lets mock getConfig(), that will return different values depends on argument either ‘multitype’ or ‘multimodificator’.