PHPUnit testing setup

@JJJ I’ve removed the connectivity check altogether. It seems the new version doesn’t ship with the ping command. Download the updated version and run the command again.

@JJJ The latest version (revision 15) should work with the latest Local version 2.4.2. If it still doesn’t work can you report the errors here. Thanks

1 Like

Works great! Thanks for doing that!

I have updated this script for Local Lightning.

Adding for new feature request site.

2 Likes

Great that there is an updated script, but has anyone been able to run the script of afragen successfully on a windows 10 install? I tried to setup PHPUnit using this script in git bash, but it doesn’t recognize apt-get so the setup script reports errors in line 244 and 247 and stops the script.

I can try ubuntu but that seems to create a separate env, so I don’t know how it interacts with local.

Could use some help with this.

Thanks

Hans

I tried Ubuntu now in Windows 10 WSL. I had to login as root so it could update some files and install php there also (otherwise line 151 issued an error) and even after all that it ended in an error about missing packages.

After this…
Updating composer…
You are already using composer version 1.10.8 (stable channel).

It says…
ERROR Missing packages. Make sure you’re connected to the internet.
Stopping script…

Ideas or solutions?

I tried to debug setup-phpunit.sh for my system (in WSL Bash) and found that the missing packages message was caused by packages_installed() on line 162, where it looks for /usr/local/bin/wget where wget in my system is located in /usr/bin/wget.

And after that it was a challenge to get $WP_TESTS_DIR and $WP_CORE_DIR to be read correctly.

After that it still broke on the mysql statements, even at the version check, and when I tried to solve this by reinstalling mysql it broke the db-connection of the server and I had to reinstall the entire site again in local.

Has anybody done it yet? Still need help.

If anyone does get it working with Windows I would welcome a PR.

Thanks Andy,

I noticed yesterday that I can run mysql in the site shell, so I need to run the script there and cannot use wsl as an alternative since it does not offer the same environment.

The problem might not be as much in the script as in the site shell itself that is lacking. And I’m not the first asking about that, but there seems great lack of support on that subject.

So I must run in site shell because of mysql, but I cannot run in site shell since it doesn’t speak linux and has no capacity to run bash scripts. And no one seems to have solved this or confirms that it is not meant to be or comes in a future release. Just silence.

For now, I don’t know a path forwards, so will leave it. I think an answer must come from Flywheel.

Thanks

Windows 10 - I can get both phpunit --version (7.5.9) and run of an isolated unit test for oscarotero/env for wp-config constants from .env files successfully with the following:

Once Per Windows Installation

Once Per Lightning Site Creation
See original thread regarding getting Git Bash Terminal to setup PHP environment via local’s script

  • Via Local open Site Shell once and then close the command prompt
  • Go to ~/AppData/Roaming/Local/ssh-entry/
  • Find the right .sh script that corresponds to what Local opens when opening Site Shell. Copy the filename/path to the .sh for your desired site. (Sort by date of creation, or the comments in each .sh make it relatively easy to find).
  • Go to your VS Code project workspace (same place that /app/public/ would contain your WP installation) and create ssh.sh with one line to execute the Local specific .sh file. Example
    “sh ~/AppData/Roaming/Local/ssh-entry/_LL1zMTxi.sh”

Every time you want to use PHPUnit (or WP CLI)

  • Open your bash terminal to ~/Local Sites/example.local/
  • sh ssh.sh
  • From that point you should be able to run phpunit --version

This wouldn’t cover Grunt or other dependencies you may require for execution. I’m exploring mocking tools like Automattic/wordbless to reduce the scope of each unit test to the actual function/class I care about.

Over a few Local versions I’ve seen a few errors pop up when running the “sh ssh.sh” step. Re-running the Local Open Site Shell via UI once has generally resolved those.

BrainMonkey - Abstraction on top of Mockery - Is making a lot of sense with my workflow and seems to play very nice within a LocalWP environment. Blog posts from swas.io and Torque.io were quite helpful.

A couple minor points to revise in the above for anyone that has a similar scenario where 1 Local Site maps to a git repo representing site that deploys but may have multiple plugins within that site which need their separate phpunit tests executed:

  • Skip installing PHPUnit into /usr/bin/phpunit

  • Use composer.json with separate blocks for prod (require) and dev (require-dev)
    {
    “require”: {
    “vlucas/phpdotenv”: “",
    “oscarotero/env”: "

    },
    “require-dev”: {
    “brain/monkey”: “2.*”,
    “phpunit/phpunit”: “^9.4”
    }
    }

  • Within your plugin(s) - setup phpunit.xml and a bootstrap file that points to your sites vendor/autoload.php file

  • Still use the ‘sh ssh.sh’ to get composer and PHP environment prep available to VS Code terminal

  • From ~/Local Sites/example.local/app/public/vendor/phpunit execute
    "./phpunit/phpunit -C …/…/wp-content/plugins/example-plugin/phpunit.xml