PHPackages                             jsiefer/class-mocker - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. jsiefer/class-mocker

ActiveLibrary[Testing &amp; Quality](/categories/testing)

jsiefer/class-mocker
====================

Can mock entire frameworks or namespaces

0.3.4(9y ago)01361MITPHPPHP ~5.6||~7.0||~7.1

Since Mar 22Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jsiefer/class-mocker)[ Packagist](https://packagist.org/packages/jsiefer/class-mocker)[ RSS](/packages/jsiefer-class-mocker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (6)Used By (1)

ClassMocker
===========

[](#classmocker)

\[[![Build Status](https://camo.githubusercontent.com/adf206a61a4a617eededeb081d7628a35db066f2b5a89f35c930f0f8aab4b1f7/68747470733a2f2f7472617669732d63692e6f72672f6a7369656665722f636c6173732d6d6f636b65722e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/adf206a61a4a617eededeb081d7628a35db066f2b5a89f35c930f0f8aab4b1f7/68747470733a2f2f7472617669732d63692e6f72672f6a7369656665722f636c6173732d6d6f636b65722e7376673f6272616e63683d6d6173746572)\] () \[[![Coverage Status](https://camo.githubusercontent.com/269bceaeefff99a14ba1fbbfebb02f881e1ab48a64f02a1acd9fa3a02159b30e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a7369656665722f636c6173732d6d6f636b65722f62616467652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/269bceaeefff99a14ba1fbbfebb02f881e1ab48a64f02a1acd9fa3a02159b30e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a7369656665722f636c6173732d6d6f636b65722f62616467652e7376673f6272616e63683d6d6173746572)\] ()

Introduction
------------

[](#introduction)

A simple helper library that lets you mock an entire frameworks or namespaces. This is helpful for writing unit test for extensions or plugins for libraries that do not support unit tests.

The idea is to automatically mock entire namespaces. The classes are then generated on the fly as soon as they are required.

You can register traits to add special functionality to certain classes if required.

Alternatively you can create a class footprint reference file that will old information such as class hierarchy, interfaces or constants.

Example
-------

[](#example)

A simple example, imagine you are writing a plugin or extension for a framework which does not support unit testing very well but it requires you to extend from classes that are hard to mock without initializing the whole framework.

```
/**
 * My awesome sample plugin
 */
class MyPlugin extends Example_Namespace_AbstractPlugin
{
    /**
     * Return full name
     *
     * @return string
     */
    public function getName()
    {
        if (!$this->_isLoaded) {
            throw new Exception("Not yet loaded");
        }
        $firstname = $this->getFirstname();
        $lastname = $this->getLastname();

        return $firstname . ' ' . $lastname;
    }
}
```

Now lets assume in order to test the above method you need to initialize the entire framework which may take a few seconds and defeats the purpose of quick unit tests.

The class-mocker lib lets you generate any missing class matching a pattern (e.g. `Example_Namespace_*`) which is then generated on the fly for you.

All generated class will also implementing the `PHPUnit_Framework_MockObject_MockObject` interface and give you access to `expects()` and `method()` methods for testing.

To enable the class-mocker you need a custom bootstrap file for your PHPUnit test project and define the classes that you want to generate on the fly.

```
