kambo/classopener

Mock/stub final classes and methods in PHPUnit.

v0.0.1 2018-02-18 12:58 UTC

This package is auto-updated.

Last update: 2024-04-19 16:21:18 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Software License Wouldn't it be great if you can mock/stub final classes and methods in PHPUnit? With this package you can do exactly that. Just extend your test case from ClassOpenerTestCase and add to the test method comment following annotation@disableFinal name\of\the\final\class After this you can continue with your regular mock/stub techniques.

But be aware, there are few gotchas as this is basically a hack packed in a nice package with ribbon.

Install

Prefered way to install library is with composer:

composer require kambo/classopener

Usage

Extend your test case from ClassOpenerTestCase and add to the test method comment following annotation@disableFinal name\of\the\final\class After this you can continue with your regular mock/stub techniques.

Example:

<?php

use Kambo\Testing\ClassOpener\PHPUnit\ClassOpenerTestCase;

class ClassOpenerTestCaseTest extends ClassOpenerTestCase
{
    /**
     * Disable final and mock the class
     *
     * @disableFinal Kambo\Tests\Testing\ClassOpener\Fixtures\Foo
     */
    public function testAnnotationMocking()
    {
        // You can use your traditional mocking/stubing techniques
        $fooMock = $this->getMockBuilder(Foo::class)
                        ->disableOriginalConstructor()
                        ->getMock();

        $fooMock->method('bar')->will($this->returnValue(false));

        $this->assertFalse($fooMock->bar());
    }
}

Gotchas

The "opening" of class must be done before the creation of any class instance. This premise can be easilly broken by the previously running tests. One of the possible solutions is to isolate the tests, which are using Class open with annotation @runInSeparateProcess

Example:

<?php

use Kambo\Testing\ClassOpener\PHPUnit\ClassOpenerTestCase;

class ClassOpenerTestCaseTest extends ClassOpenerTestCase
{
    /**
     * Disable final and mock the class
     *
     * @runInSeparateProcess
     * @disableFinal Kambo\Tests\Testing\ClassOpener\Fixtures\Foo
     */
    public function testAnnotationMocking()
    {

Credits

Based on the excelent article by the Mark Baker: Extending final Classes and Methods by manipulating the AST

License

The MIT License (MIT), https://opensource.org/licenses/MIT