Phake - PHP Mocking Framework

Prev   Next

Custom Answers

While the answers provided in Phake should be able to cover most of the scenarios you will run into when using mocks in your unit tests there may occasionally be times when you need more control over what is returned from your mock methods. When this is the case, you can use a custom answer. All answers in Phake implement the Phake_Stubber_IAnswer interface. This interface defines a single method called getAnswer() that can be used to return what will be returned from a call to the method being stubbed. If you need to get access to how the method you are stubbing was invoked, there is a more complex set of interfaces that can be implemented: Phake_Stubber_Answers_IDelegator and Phake_Stubber_IAnswerDelegate.

Phake_Stubber_Answers_IDelegator extends Phake_Stubber_IAnswer and defines an additional method called processAnswer() that is used to perform processing on the results of getAnswer() prior to passing it on to the stub’s caller. Phake_Stubber_IAnswerDelegate defines an interface that allows you to create a callback that is called to generate the answer from the stub. It defines getCallBack() which allows you to generate a PHP callback based on the object, method, and arguments that a stub was called with. It also defines getArguments() which allows you to generate the arguments that will be passed to the callback based on the method name and arguments the stub was called with.

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.