rosio / wordpress-testing-harness
Makes testing plugins and themes with WordPress and Composer much easier
Requires
Suggests
- johnpbloch/wordpress: Good WordPress copy to work off of. This library is preconfigured to work with this package.
- mockery/mockery: Simplifies the mocking of objects
This package is not auto-updated.
Last update: 2024-11-19 02:32:03 UTC
README
This library is meant to make it easier to test plugins and themes with the WordPress runtime via PHPUnit.
This is basically just a rewrite of kurtpayne/wordpress-unit-tests.
Usage:
-
Add this library, your testing library, and your WordPress deployment of choice to your plugin or themes composer.php:
"autoload-dev": { "psr-4": { "Plugin\\SomePlugin\\": "tests/" } }, "require-dev": { "phpunit/phpunit": "~3.7", "rosio/wordpress-testing-harness": "dev-master", "johnpbloch/wordpress": "~3.8" },
You don't actually have to include WordPress, and instead have it download to the correct folder during testing (which means you can then test multiple versions of WordPress easily), but it's recommended to add one for easy local testing.
-
Configure your phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" beStrictAboutTestSize="true" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" > <testsuites> <testsuite name="Application Test Suite"> <directory suffix="Test.php">./tests/</directory> </testsuite> </testsuites> </phpunit>
-
Configure your phpunit's tests/bootstrap.php file:
<?php require_once __DIR__ . '/../vendor/autoload.php'; $bootstrapper = new Rosio\WordPressTestingHarness\Bootstrapper(array( 'bootstrap-path' => __FILE__, 'db-name' => 'wp_test', 'db-user' => 'root', 'db-pass' => 'root', 'db-host' => 'localhost', 'db-prefix' => 'wptests_', 'db-charset' => '', 'db-collate' => '', 'wplang' => '', 'wordpress-path' => __DIR__ . '/../wordpress', 'domain' => 'wp.localhost', 'admin-email' => 'admin@example.org', 'blog-title' => 'Tests', 'admin-password' => 'admin', 'always-reinstall' => true, 'php-binary' => 'php', 'testdata-path' => __DIR__ . '/../data', )); // Require in plugin require __DIR__ . '/../index.php';
-
Write your first test!
<?php namespace Plugin\SomePlugin; use WP_UnitTestCase; use \Mockery as m; class MainPluginTest extends WP_UnitTestCase { public function setUp() { } public function tearDown() { m::close(); } public function testPluginIsCreated() { $this->assertInstanceOf('Plugin\SomePlugin\MainPlugin', getSomePlugin()); } }