PHPackages                             myplanetdigital/function\_mock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. myplanetdigital/function\_mock

AbandonedLibrary

myplanetdigital/function\_mock
==============================

Framework that helps mocking functions for unit testing PHP scripts

353.8k↓37.5%3[1 issues](https://github.com/myplanetdigital/function_mock/issues)[1 PRs](https://github.com/myplanetdigital/function_mock/pulls)PHP

Since Mar 7Pushed 12y ago15 watchersCompare

[ Source](https://github.com/myplanetdigital/function_mock)[ Packagist](https://packagist.org/packages/myplanetdigital/function_mock)[ RSS](/packages/myplanetdigital-function-mock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Function Mocking Framework
==========================

[](#function-mocking-framework)

[![Build Status](https://camo.githubusercontent.com/4ac98923c4889bac1428bae0390a7de9a7b2930af839aaccfc05fa882b01194d/68747470733a2f2f7472617669732d63692e6f72672f6d79706c616e65746469676974616c2f66756e6374696f6e5f6d6f636b2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/myplanetdigital/function_mock)

Generic PHP framework to help generate function stubs that haven't been defined in a given file. Use in conjunction with [PHPUnit](https://github.com/sebastianbergmann/phpunit/) for unit testing. See [this article](http://martinfowler.com/articles/mocksArentStubs.html) for more information about mocks and stubs.

Background
----------

[](#background)

This framework was spurred from a desire to write PHPUnit tests for Drupal CMS modules. Since Drupal (7 and earlier) is not object-oriented, it makes use of functions predominantly, and often calls other functions that are assumed to have been imported by Drupal. Some of these access the database, making it hard to isolate, unfortunately.

Since everything in Drupal is enclosed in a function, and PHPUnit cannot mock functions directly (it can only mock classes), this framework was created to allow you to generate actual 'mocks' for the functions so that they can be stubbed. Doing it this way allows you to test a .module file by itself, for example, without having to include those other dependent files.

Creating mocks
--------------

[](#creating-mocks)

To create a mock, use `FunctionMock::createMockFunctionDefinition($functionName)` with the name of the function to be mocked:

```
e.g. FunctionMock::createMockFunctionDefinition('external_method');

```

What this does under the hood is actually create and evaluate a new function called `external_method()`. The implementation of it allows its return value to be stubbed to whatever you'd like, via the `FunctionMock::stub($functionName, $stubValue)` method.

Stubbing mocks
--------------

[](#stubbing-mocks)

To stub a mock's return value, use the `FunctionMock::stub(...)` method.

There are two versions, one that sets what the method should return if called in general: `FunctionMock::stub($functionName, $stubValue)`

```
e.g. FunctionMock::stub('external_method', 'abc');

```

Then, if the following is executed:

```
$result = external_method();

```

`$result` returns `'abc'`.

The other version takes an array for the third argument, specifying the return value for an exact argument match: `FunctionMock::stub($functionName, $stubValue, $paramList)`

```
e.g. FunctionMock::stub('external_method', 'def', array('param1', 'param2'));

```

Now, if the following were to be called:

```
$result = external_method('param1', 'param2');

```

The value for `$result` would be `'def'`.

If you want to reset all the stubbed values, call `FunctionMock::resetStubs()`, which clears out all the stubbed value for each of the mocks.

Putting it all together - a PHPUnit example
-------------------------------------------

[](#putting-it-all-together---a-phpunit-example)

Let's use an example out of Drupal's Block module:

```
/**
 * Implements hook_block_info().
 */
function block_block_info() {
  $blocks = array();

  $result = db_query('SELECT bid, info FROM {block_custom} ORDER BY info');
  foreach ($result as $block) {
    $blocks[$block->bid]['info'] = $block->info;
    // Not worth caching.
    $blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
  }
  return $blocks;
}

```

Notice that `block_block_info()` cannot be easily tested without also testing `db_query()` as well, which accesses the database.

The key to unit testing is to assume that all of its dependent classes and functions are already working, so you'll want to assume that `db_query()` works just fine, mock it since it's an external function, and stub its return value accordingly.

Given you have [PHPUnit installed](http://phpunit.de/manual/3.7/en/installation.html), you can write a test case like so:

```

```

Auto-generating mocks
---------------------

[](#auto-generating-mocks)

Although you can generate a mock function for each one you need, you can also have function\_mock autogenerate all the functions it can based on the files you're testing. For that, use `FunctionMock::generateMockFunctions($srcFileList)` and provide a list of all the source files you want to have tested. This method will search within the scope of `$srcFileList` and determine which functions don't have an implementation for them, creating mocks for each one.

Here's an example of how you could use it:

```
