rbdwllr/atlantic-city

A replica of the default WordPress plugin Hello Dolly, but for composer powered WordPress and for the song Atlantic City.

Installs: 337

Dependents: 1

Suggesters: 0

Security: 0

Stars: 5

Watchers: 1

Forks: 2

Open Issues: 0

Type:wordpress-plugin

0.3.0 2020-11-25 09:21 UTC

This package is auto-updated.

Last update: 2024-04-27 16:47:46 UTC


README

Actions Status codecov PHP Version Support

A Composer based WordPress plugin which displays lyrics from the song Atlantic City in the WordPress admin.

This plugin was created to show developers how to create WordPress plugins which are hosted on Packagist and work with Composer friendly WordPress frameworks like Dusty.

It also highlights how to write unit tests for WordPress plugins using WP_Mock and how to integrate with static analysis tools like PHPStan.

Composer Setup

There is only one difference between a WordPress plugin built with Composer compared to a standard PHP package, which is the package type. With WordPress plugins you set the type to wordpress-plugin in the Composer.json file.

"type": "wordpress-plugin",

This simply tells Composer friendly WordPress frameworks how to handle the package, instead of putting the package in the vendor folder it will put the package in the WordPress plugins directory. See WPackagist for more details on this.

Unit Tests with WP_Mock

To write unit tests for WordPress you need to use a package called WP_Mock. It is an extension of PHP Unit which allows you to mock WordPress custom function calls.

For instance this library's unit tests mock the is_rtl() function which tells WordPress if the site's text runs from right to left or left to right.

WP_Mock::userFunction('is_rtl', [
    'times' => 1,
    'return' => false
]);

To use WP_Mock you need to create a custom bootstrap file and extend the WP_Mock test case class rather than the PHP Unit one.

namespace Tests;

use App\MyWordPressCode;
use WP_Mock\Tools\TestCase;
use WP_Mock;

class MyWordPressTests extends TestCase
{
    ...
}

WP_Mock also comes with some nice features which allow you to check whether WordPress actions or filters have been called.

WP_Mock::expectActionAdded('action', 'parameters');

Other Code Analysis Tools

This plugin integrates with a number of static analysis tools, including PHP Code Sniffer and PHP Mess Detection, which will help improve the code quality of the WordPress plugins you produce.

It also shows you how to integrate with PHPStan to add some type safety to your code. This requires a small amount of customisation to handle WordPress so there is a configuration file and an integration with the szepeviktor/phpstan-wordpress package which extends PHPStan for WordPress.