Brain-WP/BrainMonkey

Error: `Call to undefined function Patchwork\replace()` after composer update

Closed this issue · 5 comments

Hi again.

I have a weird situation here: in a plugin I defined Monkey as dev dependency like so:

  "require": {
    "php": ">=5.3.0"
  },
  "autoload": {
    "psr-4": {
      "ShortcodeReplace\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "ShortcodeReplace\\Test\\Helper\\": "tests/Helper/"
    }
  },
  "require-dev": {
    "php": ">=5.4.0",
    "brain/monkey": "~1.2"
  }

When I installed the dependencies in the past, I got Monkey at 1.2.1 and Patchwork at 1.4.0.
Today I wanted to update the dependencies:

david@vaio:/var/www/.../wp-content/plugins/edit-post-shortcode-replace$ composer update 
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing antecedent/patchwork (1.4.0)
  - Installing antecedent/patchwork (1.3.5)
    Loading from cache

  - Removing brain/monkey (1.2.1)
  - Installing brain/monkey (1.3.2)
    Loading from cache

Writing lock file
Generating autoload files

Now when I'm trying to run the tests, I see this:

$ phpunit 
PHPUnit 4.8.23 by Sebastian Bergmann and contributors.


Fatal error: Call to undefined function Patchwork\replace() in /var/www/projects/inpsyde/mi-cockpit-ae/wp-content/plugins/edit-post-shortcode-replace/vendor/brain/monkey/src/Monkey/Functions.php on line 77

My phpunit bootstrap file is just vendor/autoload.php. The problem still persists after I cleared the composer cache.

Any ideas, what this is about?

Hm, at the moment I cannot eliminate that this is an issue of weird caching. See antecedent/patchwork#39 I'll have a closer look on that.

I don't think it is something related to cache.

The reason why I moved Patchwork from 1.4 to 1.3 is that 1.4 raises errors when some functions is defined before Patchwork is loaded.

I think your problem are more related to the changes in #6

I want to ask:

  • are you relying on Brain Monkey to load Patchwork or are you loading it manually in your bootstrap?
  • in the first case, can you try if loading Patchwork manually solve the issue?
  • in the second case, can you post the relevant part of your bootstrap file?

Just noticed the problem was not limited to the latest release of Brain Monkey. I specified the version constraint to ~1.2.0 and ran composer update (because I have had to add other dependencies) and got the same issue.

are you relying on Brain Monkey to load Patchwork or are you loading it manually in your bootstrap?

I'm relying on Brain Monkey.

in the first case, can you try if loading Patchwork manually solve the issue?

The problem is solved when I do this in my bootstrap.php (as described in the readme.md):

<?php

$autoload  = $base_dir . '/vendor/autoload.php';
$patchwork = $base_dir . '/vendor/antecedent/patchwork/Patchwork.php';

# require patchwork first
if ( file_exists( $patchwork ) )
    require_once $patchwork;

if ( file_exists( $autoload ) )
    require_once $autoload;

It solves the problem for both 1.2.1 and 1.3.2 of Brain Monkey.

Wait, I withdraw! I noticed (way to late) that my tests didn't even call Brain\Monkey::setUp() neither setUpWp() nor tearDown(). So the issue here resides in front of the monitor. This is awkward. I'm sorry!

facepalm

:D no problem, glad it is solved.