kambo / classopener
Mock/stub final classes and methods in PHPUnit.
Requires
- php: >=7.0.0
- nikic/php-parser: ^2.0
- roave/better-reflection: 1.2.0
Requires (Dev)
- phpunit/phpunit: ^6.5
- squizlabs/php_codesniffer: ^3.1
This package is auto-updated.
Last update: 2024-11-19 17:31:45 UTC
README
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