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
- Download and install Composer
- Move Composer to your
bin
folder to access it globally:sudo mv composer.phar /usr/local/bin/composer
- Verify that it is installed properly:
composer -V
composer self-update
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
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
.
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.
The command line interface for WordPress.
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:
- http://ifmeister.com/how-to-solve-wp-cli-for-mamp-errors/
- https://tommcfarlin.com/installing-wp-cli-with-mamp/
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