The verification of __call() was discussed in the previous chapter. Magic methods can also be stubbed in much the same way. If you want to verify a particular invocation of __call() you can stub the actual method call by mocking the method passed in as the first parameter.
Consider the following class.
Example 2.17. A Magic Class
<?php class MagicClass { public function __call($method, $args) { return '__call'; } } ?>
You could stub an invocation of the __call() method through a userspace call to magicCall() with the following code.
Example 2.18. Implicitly Stub of __call()
<?php class MagicClassTest extends PHPUnit_Framework_TestCase { public function testMagicCall() { $mock = Phake::mock('MagicClass'); Phake::when($mock)->magicCall()->thenReturn(42); $this->assertEquals(42, $mock->magicCall()); } } ?>
If for any reason you need to explicitly stub calls to __call() then you can use Phake::whenCallMethodWith().
Example 2.19. Explicitly Stubbing __call()
<?php class MagicClassTest extends PHPUnit_Framework_TestCase { public function testMagicCall() { $mock = Phake::mock('MagicClass'); Phake::whenCallMethodWith('magicCall')->isCalledOn($mock)->thenReturn(42); $this->assertEquals(42, $mock->magicCall()); } } ?>