Automattic / wordbless

WorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment
Other
131 stars 6 forks source link

Fix autoloading #56

Closed leogermani closed 2 years ago

leogermani commented 2 years ago

Fixes the following error when using WorDBless as a dependency

and updates the README

Class WorDBless\Composer\InstallDropin is not autoloadable, can not call post-update-cmd script
kraftbj commented 2 years ago

In terms of autoloading, but still need to fix up the copy command.

> WorDBless\Composer\InstallDropin::copy
Script WorDBless\Composer\InstallDropin::copy handling the post-update-cmd event terminated with an exception

In InstallDropin.php line 17:

  copy(src/dbless-wpdb.php): Failed to open stream: No such file or directory  

(after running composer update automattic/wordbless@dev-update/fix-autoload in Jetpack's projects/packages/abtest with the updated post-install command.

kraftbj commented 2 years ago

~Do you think we need to define a constant to define the dir that we're in and use that, e.g. WDBLESS_DIR so it'll work both ways?~ ignore this

szepeviktor commented 2 years ago

when using WorDBless as a dependency

When a package is a dependency its post-install-cmd event is not triggered.

szepeviktor commented 2 years ago

README: Add this script to your composer.json:

I see! This is in the main project. Using a dependency's class in a Composer script is a mystery for me!

PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality. Callbacks can only autoload classes from psr-0, psr-4 and classmap definitions. If a defined callback relies on functions defined outside of a class, the callback itself is responsible for loading the file containing these functions.

(Composer docs)

🖱️ https://github.com/composer/composer/issues/8147#issuecomment-493026423

kraftbj commented 2 years ago

This worked for me now.

Steps to test:

  1. In the Jetpack repo, cd projects/packages/abtest.
  2. Ensure no vendor, wordpress, or composer.lock file (just for the sake of testing)
  3. Update abtest's composer.json to look for version "automattic/wordbless": "dev-update/fix-autoload",
  4. Run composer update

Expected CLI result should include:

...
10 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
36 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> WorDBless\Composer\InstallDropin::copy

and there should be a wordpress/wp-content/db.php file in the abtest package dir.

I'm going to approve this based on that, but @leogermani can you double-check me on ^ before we cut a new version.

leogermani commented 2 years ago

Worked for me as well