antecedent / patchwork

Method redefinition (monkey-patching) functionality for PHP.
https://antecedent.github.io/patchwork/
MIT License
437 stars 39 forks source link

Code added to output #151

Open szaleq opened 8 months ago

szaleq commented 8 months ago

I'm testing a method, which echoes some content (it's a widget for an Elementor WordPress plugin). Since this method does not return anything, and the only way to check if it works properly is to check what it prints to the output, I've designed a test which uses output buffering to capture what the method echoes and test its value. This worked well but suddenly this tests started to fail because there is some patchwork code added to my buffered output - in each test the output is suffixed with this string: \Patchwork\CodeManipulation\Stream::reinstateWrapper();.

Here is a simplified test code (using Pest):

it('renders the widget', function () {
    ob_start();

    $this->widget->render();

    expect(ob_get_clean())->toEqual('assumed_render_result');
});

For example, if my widget's render() method had this code:

echo 'assumed_render_result';

the test should pass. Instead it fails, because the actual output is: assumed_render_result;\Patchwork\CodeManipulation\Stream::reinstateWrapper();

Why is that? Is is some bug in Patchwork or am I missing something?

Here I found this code in source: https://github.com/antecedent/patchwork/blob/16a1ab81559aabf14acb616141e801b32777f085/src/CodeManipulation/Actions/CodeManipulation.php#L16 but I don't understand why it gets appended to my output buffering result and how to avoid id.

anomiex commented 8 months ago

Can you provide a minimal test case (e.g. as a zip file or small github repo) that reproduces this?

antecedent commented 7 months ago

This does seem like a bug in Patchwork. I agree that a minimal test case would be very helpful in pinning it down.