Running unit tests, code style checks and linters

Table of contents

Availability overview

wordpress-seowordpress-seo-premiumwpseo-newswpseo-videowpseo-woocommerce
phpunit
phplint
phpcs
JavaScript tests
eslint

PHP

Unit tests

Running unit tests in PHP can be done via PHPUnit. Please make sure you've read the guide on setting up PHPUnit before running any tests.

'Old' integration tests in terminal

  • composer integration-test

Brainmonkey tests in terminal

  • (First time) composer global require phpunit/phpunit:5.7
  • composer test

Coverage

Configuration: Run pecl install xdebug to install debug. To run the tests including the coverage, click the 'mesh' with green play button in the upper right corner of PhpStorm.

If you get an error "Failed loading /usr/local/Cellar/php@7.1/7.1.26/lib/php/20160303/xdebug.so" or something similar it is possible your extension directory needs to be configured. Run php --ini to find your php.ini file and open it.

To load extensions installed by PECL, we need to point the extension_dir in our php.ini to the extension directory created by PECL. This directory can be found by running: pecl config-get ext_dir Now uncomment and update the extension_dir in your php.ini: extension_dir = /usr/local/lib/php/pecl/<php_api_version>

Linting

To check for syntax errors, run find -L . -path ./vendor -prune -o -path ./node_modules -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l

Coding standards

We use a combination of coding standards to check our code against: WPCS, PHPCompatibility, and our own YoastCS sniffs.

Configuration

  1. composer install
  2. composer config-yoastcs.

Checking

Free
  • To check everything: composer check-cs
  • When you only want to output the errors (not the warnings): composer check-cs-errors
  • To auto-fix errors and warnings: composer fix-cs

Premium

  • All the commands from Free work for Premium as well.
  • To check only the premium folder: composer premium-check-cs

News

  • To check everything: grunt phpcs

Video

  • To check everything: vendor/bin/phpcs

WooCommerce

  • To check everything: grunt phpcs

JavaScript

Unit tests

Run yarn test

Coverage

Run yarn test --coverage

Linting

Run grunt eslint