awesomite/mock-finals

Mocking final classes and methods

v1.0.1 2020-07-25 16:48 UTC

This package is auto-updated.

Last update: 2024-03-26 00:59:18 UTC


README

Build Status

Mock Finals

Mock final classes and methods in your tests. Library overrides existing class loaders and removes all final occurrences in runtime using uopz_flags.

Installation

composer require --dev awesomite/mock-finals

Requirements

  • PHP ^7.1
  • uopz (pecl install uopz)

Use cases

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.

Example

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());
    }
}