idimsh/better-bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

dev-master 2020-10-04 21:40 UTC

This package is auto-updated.

Last update: 2024-05-05 05:53:32 UTC


README

Downloads this Month Build Status License

#Introduction (idimsh) This is a redistribution of the famous package bypass-finals with minor modifications that includes:

  • making it inheritance ready, allowing subclasses of the main class to continue working.
  • code style uses spaces instead of tabs.
  • tests are not using Nette Tester, but PHPUnit

The original license remains untouched, and they are BSD and GNU both.
The reason for creating this package is an un answered pull request.

Introduction

Removes final keywords from source code on-the-fly and allows mocking of final methods and classes. It can be used together with any test tool such as PHPUnit, Mockery or Nette Tester.

Installation

The recommended way to install is through Composer:

composer require idimsh/better-bypass-finals --dev

It requires PHP version 7.1 and supports PHP up to 8.0.

Usage

Simply call this:

\idimsh\BypassFinals::enable();

or

\idimsh\BypassFinalsCatcher::enable();

You need to enable it before the classes you want to remove the final are loaded. So call it as soon as possible, preferably right after vendor/autoload.php in loaded.

Note that final internal PHP classes like Closure cannot be mocked.

You can choose to only bypass finals in specific files or directories:

idimsh\BypassFinals::setWhitelist([
    '*/Nette/*',
]);

This gives you finer control and can solve issues with certain frameworks and libraries.

There is also a PHP Unit listener available for usage inside phpunit.xml file, like:

<phpunit bootstrap="vendor/autoload.php">
    <listeners>
        <listener class="idimsh\BypassFinal\Listener\BypassFinalListener"/>
    </listeners>
</phpunit>

The Catcher version

The extension of the main class with the name: \idimsh\BypassFinalsCatcher Will catch syntax errors and echo the file name to STDERR. This is useful for tests that span thousand files, the default behaviour was a silent fatal parse error that does not report the file name.

There is also a listener that uses the catcher.

<phpunit bootstrap="vendor/autoload.php">
    <listeners>
        <listener class="idimsh\BypassFinal\Listener\BypassFinalCatcherListener"/>
    </listeners>
</phpunit>