Function call logging with Patchwork 01

A small pace forward in stream wrapper based function call logging.

Quick set up

I’ve had little time to fiddle on the ad-hoc adapters front and was able to put up a proof of concept test for my idea.
I’ve required Patchwork in my composer.json file

composer require antecedent/patchwork

and, when asked for a version constraint, entered ~1.3 as per Jordi Boggiano explicit advice.
After that a quick test setup with Codeception took me to my first test.

Setting up

The Patchwork guide clarifies that the library file must be included before any function meant to be replaced is included so I’ve put required the file in the unit tests tests/unit/_bootstrap.php file

// Here you can initialize variables that will be available to your tests
include_once  __DIR__ . '/../../vendor/antecedent/patchwork/Patchwork.php'; 

and went on to write my first test in the tests/unit/PatchworkTest.php file


function get_post_meta() {
    return 'foo';

class PatchworkTest extends \PHPUnit_Framework_TestCase {

    protected function setUp() {

     * @test
     * it should log the call to the get_post_meta function
    public function it_should_log_the_call_to_the_get_post_meta_function() {
        $calls = 0;
        Patchwork\replace( 'get_post_meta', function () use ( &$calls ) {
            $calls ++;
        } );


        $this->assertEquals( 3, $calls );

while uncomplicated the test works and helps me in setting my ducks in a line for the next moves. Function call logging first test passes


I will be testing the idea further trying to make the Patchwork based function logger discern the function caller to filter out unwanted calls and listen to calls from specific folders.