stk2k/decoy

This package is abandoned and no longer maintained. No replacement package was suggested.

PHP lightweight mock framework

0.3.0 2019-05-25 08:47 UTC

This package is auto-updated.

Last update: 2019-11-19 02:42:50 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

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

stk2k

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.