2017 © Pedro Peláez
 

library classopener

Mock/stub final classes and methods in PHPUnit.

image

kambo/classopener

Mock/stub final classes and methods in PHPUnit.

  • Sunday, February 18, 2018
  • by kambo
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Class opener - Mock/stub final classes and methods in PHPUnit.

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., (*1)

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

Install

Prefered way to install library is with composer:, (*3)

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., (*4)

Example:, (*5)

<?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, (*6)

Example:, (*7)

<?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, (*8)

License

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

The Versions

18/02 2018

dev-master

9999999-dev

Mock/stub final classes and methods in PHPUnit.

  Sources   Download

MIT

The Requires

 

The Development Requires

mock stub test double phpunit class final

18/02 2018

v0.0.1

0.0.1.0

Mock/stub final classes and methods in PHPUnit.

  Sources   Download

MIT

The Requires

 

The Development Requires

mock stub test double phpunit class final

18/02 2018

dev-develop

dev-develop

Mock/stub final classes and methods in PHPUnit.

  Sources   Download

MIT

The Requires

 

The Development Requires

mock stub test double phpunit class final