Brain-WP/BrainMonkey

Trouble mocking internal function (gzencode)

mundschenk-at opened this issue · 6 comments

I've got an issue with mocking internal functions (which seems to be supported by the underlying patchwork library) in conjunction with phpunit.

I've added gzencode as an overridable internal to patchwork.json and set up the expectation via Functions\expect. This works fine until I try to generate coversge information, when the return value is not the expected false but null, which lets the test case bomb out.

I guess this is some conflict with the PHPUnit coverage handling code.

Have you tried to load patchwork manually in the PHPUnit bootstrap file before loading anything else (including composer autoload)?

The Patchwork file to load is vendor/antecedent/patchwork/Patchwork.php

Yes, but then the brain/monkey mocks for WordPress functions are broken.

I've did some tests, and had some problem loading Patchwork before autoload. This looks a bug at Patchwork (as in namespace import not handled correctly), I'll try to have time to report properly there.

For now, I've added this code to my PHPUnit bootstrap:

/* /tests/bootstrap.php */

class_alias( SebastianBergmann\CodeCoverage\Exception::class, 'PHPUnit\TextUI\CodeCoverageException' );
class_alias( SebastianBergmann\CodeCoverage\Filter::class, 'PHPUnit\TextUI\CodeCoverageFilter' );
class_alias( SebastianBergmann\CodeCoverage\Report\Clover::class, 'PHPUnit\TextUI\CloverReport' );
class_alias( SebastianBergmann\CodeCoverage\Report\Crap4j::class, 'PHPUnit\TextUI\Crap4jReport' );
class_alias( SebastianBergmann\CodeCoverage\Report\Html\Facade::class, 'PHPUnit\TextUI\HtmlReport' );
class_alias( SebastianBergmann\CodeCoverage\Report\PHP::class, 'PHPUnit\TextUI\PhpReport' );
class_alias( SebastianBergmann\CodeCoverage\Report\Text::class, 'PHPUnit\TextUI\TextReport' );
class_alias( SebastianBergmann\CodeCoverage\Report\Xml\Facade::class, 'PHPUnit\TextUI\XmlReport' );
class_alias( SebastianBergmann\Version::class, 'PHPUnit\Runner\VersionId' );

The tests worked fine, both patching core function (gzencode) and WordPress functions.

It also worked with coverage.

Thank you, I'll try that.

@gmazzap Unfortunately, I could not get this to work. While replacing the autoload with the class_aliases let's me run the tests, the internal functions are not actually patched. Adding require_once dirname( __DIR__ ) . '/vendor/antecedent/patchwork/Patchwork.php'; as well still breaks Mockery.

I recently tested redefining core PHP functions and it works well even with coverage without class alias or anything like that. I guess this was fixed upstream with the more modern version of the various libraries involved.