Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Created August 15, 2019 09:14
Show Gist options
  • Save dotherightthing/41e712ff605a3a122f6afc4a76b6774e to your computer and use it in GitHub Desktop.
Save dotherightthing/41e712ff605a3a122f6afc4a76b6774e to your computer and use it in GitHub Desktop.
[Installing Composer & PHPUnit] Installing Composer so I can install PHP Unit for WordPress plugin testing. #php

Installing Composer & PHPUnit

Created: 2017.04.15

I have been looking at PHPUnit which seems like a great way of creating automated tests. However, it seems to be geared towards object oriented code, are there any alternatives for procedural code? You can test procedural code with PHPUnit. Unit tests are not tied to object-oriented programming. They test units of code. In OO, a unit of code is a method. In procedural PHP, I guess it's a whole script (file). While OO code is easier to maintain and to test, that doesn't mean procedural PHP cannot be tested. ... Actually, testing functions is a lot like testing object methods. You send it some input and validate if the output matches what you expect

Install Composer

  1. Download and install Composer
  2. Move Composer to your bin folder to access it globally: sudo mv composer.phar /usr/local/bin/composer
  3. Verify that it is installed properly: composer -V

To update Composer

  1. composer self-update

Install PHPUnit

Create composer.json

{}

Populate this file manually, or my running the require command:

(version 5.7 works work with PHP 5.6 - see https://phpunit.de/)

composer require --dev phpunit/phpunit ^5.7

Result:

{
  "require-dev": {
    "phpunit/phpunit": "^5.7"
  }
}

Download the dependencies:

composer install

Optional extras

The optional PHP_Invoker is a utility class for invoking callables with a timeout.

phpunit/php-invoker 1.1.4 requires ext-pcntl - the process control extension (PCNTL)

The installation instructions are complex so I'm leaving PHP_Invoker for now, but I may need it to test WP-AGM.

Testing via WP CLI

In Unit Tests for WordPress Plugins – Setting Up Our Testing Suite, WP-CLI is the recommended interface between the developer and tests. However I couldn't get this working with my multisite installation.

Install WP CLI

The command line interface for WordPress.

http://wp-cli.org/#install

Create tests

Navigate to your WordPress install’s main directory

wp scaffold plugin-tests my-plugin-name

This failed for me:

Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.

Neither of these pages worked:

I also tried this from MAMP Pro error establishing database connection:

#MAMP Madness
export PATH=/Applications/MAMP/Library/bin:$PATH
PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}bin:$PATH

Not testing via WP CLI!

To read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment