Download the PHP package jsiefer/class-mocker without Composer
On this page you can find all versions of the php package jsiefer/class-mocker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package class-mocker
ClassMocker
[] (https://travis-ci.org/jsiefer/class-mocker) [] (https://coveralls.io/github/jsiefer/class-mocker?branch=master)
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
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.
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.
That's it, now once enabled, you can simple test your classes without requiring any original framework classes.
Advanced
There is a lot more to it when you want to mock a framework, e.g. class hierarchies, class constants and some basic classes.
The important part when mocking many classes, is that the whole PHPUnit Test needs to agree to the same implementation.
You can define class footprints and include JSON class footprint reference files for cloning an entire class tree.
Ideally you don't use this library directly but instead create a framework-mock library which provides all class references and constants etc that are required and use that library in your project for testing.
This project was initial created to mock Magento for UnitTests. Check out the mock-mage lib which uses this lib to mock the entire Magento Framework/Application.
All versions of class-mocker with dependencies
zendframework/zend-code Version ^3.0
phpunit/phpunit Version ~3.7||~4.0