Phake - PHP Mocking Framework

Prev   Next

Custom Parameter Matchers

An alternative to using argument capturing is creating custom matchers. All parameter matchers implement the interface Phake_Matchers_IArgumentMatcher. You can create custom implementations of this interface. This is especially useful if you find yourself using a similar capturing pattern over and over again. If I were to rewriting the test above using a customer argument matcher it would look something like this.

Example 5.10. Custom Argument Matcher

<?php
class FiftyTwoCardDeckMatcher implements Phake_Matchers_IArgumentMatcher
{
  public function matches($argument)
  {
    return ($argument instanceof CardCollection
        && $argument->getNumberOfCards() == 52);
  }

  public function __toString()
  {
    return '<object:CardCollection with 52 cards>';
  }
}

class MyBestPokerGameTest extends PHPUnit_Framework_TestCase
{
  public function testDealCards()
  {
    $dealer = Phake::mock('MyPokerDealer');
    $players = Phake::mock('PlayerCollection');

    $cardGame = new MyPokerGame($dealer, $players);

    Phake::verify($dealer)->deal(new 52CardDeckMatcher(), $players);
  }
}
?>

Prev   Next
Introduction to Phake
1. Getting Started
Support
2. Method Stubbing
Basic Method Stubbing
How Phake::when() Works
Overwriting Existing Stubs
Stubbing Multiple Calls
Stubbing Consecutive Calls
Stubbing Reference Parameters
Partial Mocks
Setting Default Stubs
Stubbing Magic Methods
3. Method Verification
Basic Method Verification
Verifying Method Parameters
Verifying Multiple Invocations
Verifying Calls Happen in a Particular Order
Verifying No Interaction with a Mock so Far
Verifying No Further Interaction with a Mock
Verifying Magic Methods
4. Answers
Throwing Exceptions
Calling the Parent
Capturing a Return Value
Custom Answers
5. Method Parameter Matchers
Using PHPUnit Matchers
Using Hamcrest Matchers
Parameter Capturing
Custom Parameter Matchers
6. Configuration
Setting the Phake Client
Setting the Mock Class Loader

Copyright © 2011 Mike Lively

This page uses the Perfect 'Left Menu' 2 Column Liquid Layout by Matthew James Taylor.