dg / bypass-finals

Removes `final` and `readonly` 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 or Mockery.
Other
487 stars 30 forks source link

The class \Closure is marked final and its methods cannot be replaced. #20

Closed 4n70w4 closed 4 years ago

4n70w4 commented 4 years ago

Other final classes mocked sucessfully. But \Closure from PHP core don't mocked =(

https://www.php.net/manual/en/class.closure.php

    public function test() {
       BypassFinals::enable(
       Mockery::mock(\Closure::class)
    }
Mockery\Exception: The class \Closure is marked final and its methods cannot be replaced. Classes marked final can be passed in to \Mockery::mock() as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.

/var/www/app/vendor/mockery/mockery/library/Mockery/Generator/MockConfiguration.php:332
/var/www/app/vendor/mockery/mockery/library/Mockery/Generator/MockConfiguration.php:434
/var/www/app/vendor/mockery/mockery/library/Mockery/Generator/StringManipulationGenerator.php:74
/var/www/app/vendor/mockery/mockery/library/Mockery/Generator/CachingGenerator.php:40
/var/www/app/vendor/mockery/mockery/library/Mockery/Container.php:221
/var/www/app/vendor/mockery/mockery/library/Mockery.php:114
    public function test() {
       BypassFinals::enable(
       $this->createMock(\Closure::class);
    }
Class "Closure" is declared "final" and cannot be mocked.
composer info | grep dg
dg/bypass-finals                                 v1.2.1                                Removes final keyword from source code on-the-fly and allows moc...
composer info | grep phpunit
phpunit/php-code-coverage                        8.0.2                                 Library that provides collection, processing, and rendering func...
phpunit/php-file-iterator                        3.0.4                                 FilterIterator implementation that filters files based on a list...
phpunit/php-invoker                              3.0.2                                 Invoke callables with a timeout
phpunit/php-text-template                        2.0.2                                 Simple template engine.
phpunit/php-timer                                3.1.4                                 Utility class for timing
phpunit/php-token-stream                         4.0.3                                 Wrapper around PHP's tokenizer extension.
phpunit/phpunit                                  9.1.5                                 The PHP Unit Testing framework.
rregeer/phpunit-coverage-check                   0.3.1                                 Check the code coverage using the clover report of phpunit
php -v
PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
    with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans
dg commented 4 years ago

Unfortunately, this is a system limitation and there is nothing you can do about it.

4n70w4 commented 4 years ago

@dg description about limitations would like to see the notes in the readme, and not waste time to asking.

dg commented 4 years ago

I did not realize that there is such a limitation.