Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/rector.php export-ignore
/renovate.json export-ignore
/tests export-ignore
/phpunit.integration.xml export-ignore
4 changes: 3 additions & 1 deletion .github/workflows/harness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ permissions: {}

jobs:
harness:
uses: Pollora/harness/.github/workflows/php-package.yml@v0.1.34
uses: Pollora/harness/.github/workflows/php-package.yml@main
with:
wordpress-integration: tests/Integration
permissions:
contents: read
21 changes: 21 additions & 0 deletions phpunit.integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Configuration of the integration suite: `vendor/bin/pest -c phpunit.integration.xml`. It is kept apart from
phpunit.xml so that the unit suite (Brain Monkey doubles) never loads a real WordPress, and the other way round.
Exclude tests/Integration from the suites of phpunit.xml.
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/Integration/bootstrap.php"
colors="true"
backupGlobals="false"
cacheDirectory=".phpunit.cache/integration"
failOnWarning="true"
failOnRisky="true"
>
<testsuites>
<testsuite name="Integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
</phpunit>
39 changes: 39 additions & 0 deletions tests/Integration/WordPressLoadedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* Proves the suite runs against a real WordPress and not against doubles: if the bootstrap did not load it, or if a
* Brain Monkey stub were defining these functions, one of these tests fails.
*/
it('has loaded WordPress', function (): void {
expect(function_exists('register_post_meta'))->toBeTrue()
->and(defined('ABSPATH'))->toBeTrue()
->and(get_bloginfo('name') !== '')->toBeTrue();
});

it('talks to the database of the installed site', function (): void {
global $wpdb;

expect($wpdb)->toBeInstanceOf(wpdb::class)
->and(get_option('siteurl'))->toBeString()
->and(get_option('siteurl') !== '')->toBeTrue()
->and(is_blog_installed())->toBeTrue();
});

it('fires the real hooks', function (): void {
$seen = 0;
add_action('pollora_harness_probe', function () use (&$seen): void {
$seen++;
});

do_action('pollora_harness_probe');

expect($seen)->toBe(1)
->and(has_action('pollora_harness_probe'))->toBeTrue();
});

it('registers post meta, which fails without WordPress', function (): void {
expect(register_post_meta('post', 'pollora_harness_meta', ['type' => 'string', 'single' => true, 'show_in_rest' => true]))->toBeTrue()
->and(registered_meta_key_exists('post', 'pollora_harness_meta', 'post'))->toBeTrue();
});
39 changes: 39 additions & 0 deletions tests/Integration/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* Bootstrap of the integration suite: loads a real WordPress, once, before the first test.
*
* Copy it to tests/Integration/bootstrap.php (the phpunit.integration.xml next to it points there; the paths below
* assume that location). The `WordPress integration` job of the harness workflow installs WordPress and exports WP_PATH (the directory that holds
* wp-load.php); locally, install WordPress anywhere and export WP_PATH, or pass it on the command line:
*
* WP_PATH=/path/to/wordpress vendor/bin/pest -c phpunit.integration.xml
*
* Composer's autoloader is loaded first, by this file, so that the classes under test and Pest itself are available.
* WordPress is then loaded with `wp-load.php`: the site is installed, its plugins are active, its hooks fire.
*/
$autoload = dirname(__DIR__, 2).'/vendor/autoload.php';

if (! is_file($autoload)) {
fwrite(STDERR, "Composer's autoloader is missing: run composer install.\n");
exit(1);
}

require_once $autoload;

$wordpressPath = getenv('WP_PATH') ?: (getenv('WP_CORE_DIR') ?: '');

if ($wordpressPath === '' || ! is_file($wordpressPath.'/wp-load.php')) {
fwrite(STDERR, "WordPress not found: set WP_PATH to the directory that holds wp-load.php (got \"{$wordpressPath}\").\n");
exit(1);
}

// No theme output, no cron request: a test starts what it needs itself.
define('WP_USE_THEMES', false);
defined('DISABLE_WP_CRON') || define('DISABLE_WP_CRON', true);

require_once $wordpressPath.'/wp-load.php';

unset($autoload, $wordpressPath);
Loading