Phake - PHP Mocking Framework

Prev   

Setting the Mock Class Loader

When generating mock classes, Phake will load them into memory utilizing the PHP eval() function. This can make the code inside of mock classes difficult to debug or diagnose when errors occur in this code. Using the Phake::setMockLoader() method you can change this behavior to instead dump the generated class to a file and then require that file. This will allow for accurate and easily reaserchable errors when running tests. This shouldn't typically be required for most users of Phake, however if your are having errors or working on code for Phake itself it can be incredibly useful.

Phake::setMockLoader() accepts a single parameter of type Phake_ClassGenerator_ILoader. The default behavior is contained in the Phake_ClassGenerator_EvalLoader class. If you would instead like to dump the classes to files you can instead use the Phake_ClassGenerator_FileLoader class. The constructor accepts a single parameter containing the directory you would like to dump the classes to. The classes will be stored in files with the same name as the generated class.

Below is an example of the code required to dump mock classes into the /tmp folder.

Example 6.2. Setting the Mock Class Loader

<?php
require_once('Phake.php');
Phake::setLoader(Phake::CLIENT_PHPUNIT);
?>

Prev   
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.