Table of Contents
The verification and stubbing functionality in Phake both rely heavily on parameter matching to help the system understand exactly which calls need to be verified or stubbed. Phake provides several options for setting up parameter matches.
The most common scenario for matching parameters as you use mock objects is matching on equal variables For this reason the default matcher will ensure that the parameter you pass to the mock method is equal (essentially using the '==' notation) to the parameter passed to the actual invocation before validating the call or returning the mocked stub. So going back to the card game demonstration from the introduction. Consider the following interface:
Example 5.1. Dealer Strategy Interface
<?php interface DealerStrategy { public function deal( CardCollection $deck, PlayerCollection $players); } ?>
Here we have a deal() method that accepts two parameters. If you want to verify that deal() was called, chances are very good that you want to verify the the parameters as well. To do this is as simple as passing those parameters to the deal() method on the Phake::verify($deal) object just as you would if you were calling the actual deal() method itself. Here is a short albeit silly example:
Example 5.2. Example of Default 'Equals' Matching
<?php //I don't have Concrete versions of // CardCollection or PlayerCollection yet $deck = Phake::mock('CardCollection'); $players = Phake::mock('PlayerCollection'); $dealer = Phake::mock('DealerStrategy'); $dealer->deal($deck, $players); Phake::verify($dealer)->deal($deck, $players); ?>
In this example, if I were to have accidentally made the call to deal() with a property that was set to null as the first parameter then my test would fail with the following exception:
Example 5.3. Failed Simple Equals Test
Expected DealerStrategy->fooWithArgument(equal to <object:CardCollection>, equal to <object:PlayerCollection>) to be called exactly 1 times, actually called 0 times. Other Invocations: PhakeTest_MockedClass->fooWithArgument(<null>, equal to <object:PlayerCollection>)
Determining the appropriate method to stub works in exactly the same way.
There may be cases when it is necessary to verify or stub parameters based on something slightly more complex then basic equality. This is what we will talk about next.
Phake was developed with PHPUnit in mind. It is not dependent on PHPUnit, however if PHPUnit is your testing framework of choice there is some special integration available. Any constraints made available by the PHPUnit framework will work seemlessly inside of Phake. Here is an example of how the PHPUnit constraints can be used:
Example 5.4. Using PHPUnit Matchers
<?php class TestPHPUnitConstraint extends PHPUnit_Framework_TestCase { public function testDealNumberOfCards() { $deck = Phake::mock('CardCollection'); $players = Phake::mock('PlayerCollection'); $dealer = Phake::mock('DealerStrategy'); $dealer->deal($deck, $players, 11); Phake::verify($dealer) ->deal($deck, $players, $this->greaterThan(10)); } } ?>
I have added another parameter to my deal() method that allows me to specify the number of cards to deal to each player. In the test above I wanted to verify that the number passed to this parameter was greater than 10.
For a list of the constraints you have available to you through PHPUnit, I recommend reading the <a>PHPUnit's documentation on assertions and constraints</a>. Any constraint that can be used with assertThat() in PHPUnit can also be used in Phake.