Mock final classes and methods in your tests. Library overrides existing class loaders and removes all final
occurrences in runtime using uopz_flags
.
composer require --dev awesomite/mock-finals
- PHP ^7.1
- uopz (
pecl install uopz
)
In general, it's a bad practice to do so. However it may be helpful when you have to deal with legacy code or third party libraries.
Please read the following article to understand how to properly deal with final
keyword in your code.
class Greeter
{
final public function sayHello(): string
{
return 'hello';
}
}
class MyTest extends \PHPUnit\Framework\TestCase
{
public function testSayHello(): void
{
$mock = $this->getMockBuilder(Greeter::class)->getMock();
$mock
->expects($this->once())
->method('sayHello')
->willReturn('goodbye')
;
$this->assertSame('goodbye', $mock->sayHello());
}
}