Class opener - Mock/stub final classes and methods in PHPUnit.
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)