stk2k / decoy
PHP lightweight mock framework
Installs: 3 507
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.0.0
- calgamo/exception: ~0.2
- calgamo/filesystem: ~0.2
- calgamo/util: ~0.2
- mikey179/vfsstream: 1.3.*
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^6.3.0
README
Description
This library will help you for testing abstract classes or interfaces by generating concrete class in runtime.
Feature
- Mock class autogeneration
Decoy will generate mock code in your cache directory dynamically. You can use the auto-generated class which is extended/implemened from your abstract class/interface.
- Type safe
Of course, the auto-generated classes are all type safe.
- No File System Requirement
Decoy uses memory stream wrapper(mikey179/vfsStream), so you do not need to prepare local file system directory.
Requirement
PHP 7.0 or later
Install
The recommended way to install stk2k/decoy is through Composer.
composer require stk2k/decoy
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
Usage
1. Abstract class mocking
You can create mock code by calling Decoy#mock, and then call autoload to load the auto-generated code. Autogenerated class name is prefixed by 'Concrete'.
This is a simple example for PHPUnit:
require __DIR__ . '/vendor/autoload.php'; use PHPUnit\Framework\TestCase; use Decoy\Decoy; use Decoy\Exception\DecoyException; use Sample\AbstractSample; // This your class which you want to mock use Sample\ConcreteAbstractSample; // This class is mocked class for your abstract class class AbstractSampleTest extends TestCase { /** * @throws \Decoy\Exception\DecoyException */ public function setUp() { (new Decoy())->mock(AbstractSample::class)->autoload(); } public function testWantsToTestThisMethod() { // you can use autogenerated mocked class $mocked = new ConcreteAbstractSample; $this->assertTrue($mocked->wantsToTestThisMethod()); } }
2. Interface mocking
You can also create concrete class from interface in the same way.
require __DIR__ . '/vendor/autoload.php'; use PHPUnit\Framework\TestCase; use Decoy\Decoy; use Decoy\Exception\DecoyException; use Sample\InterfaceSample; // This your interface which you want to mock use Sample\ConcreteInterfaceSample; // This class is mocked class for your interface class AbstractSampleTest extends TestCase { /** * @throws \Decoy\Exception\DecoyException */ public function setUp() { (new Decoy())->mock('Sample\InterfaceSample')->autoload(); } public function testWantsToTestThisMethod() { // you can use autogenerated mocked class $mocked = new ConcreteInterfaceSample; $this->assertTrue($mocked->wantsToTestThisMethod()); } }
Tips
1. Export autogenerated file to local file
Autogenerated concrete class file and its autoloader file will be generated in memory, so you can not find the files in your local file system. But if you want to see autogenerated source codes, use AutogeneratedFile#export() method.
This code will export concrete class source code to tmp directory.
(new Decoy())->mock('Sample\InterfaceSample')->export(__DIR__ . '/tmp');
And this code will export autoloader source code to tmp directory.
(new Decoy())->mock('Sample\InterfaceSample')->autoload()->export(__DIR__ . '/tmp');
License
This library is licensed under the MIT license.
Author
Disclaimer
This software is no warranty.
We are not responsible for any results caused by the use of this software. Please use the responsibility of the your self.