Skip to content

Commit eb521e3

Browse files
committed
Add tests for setup/cleanup hooks and bootstrap file mechanism
PHPUnit: verify that the new setup and cleanup actions fire as a balanced before/after pair and that each call receives the Runtime_Environment_Setup instance as its argument. Behat: exercise the WP_PLUGIN_CHECK_BOOTSTRAP_FILE extension point in the WP-CLI path with a second --require file defining the constant. Cover the happy path, the missing-file warning path, and the no-op when the constant is not defined. See WordPress#1269.
1 parent d2a029b commit eb521e3

2 files changed

Lines changed: 73 additions & 50 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Feature: Test that WP_PLUGIN_CHECK_BOOTSTRAP_FILE is loaded in the WP-CLI path.
2+
3+
Scenario: Bootstrap file is required when the constant points to an existing file
4+
Given a WP install with the Plugin Check plugin
5+
And a wp-content/pcp-bootstrap.php file:
6+
"""
7+
<?php
8+
WP_CLI::log( 'PCP bootstrap loaded' );
9+
"""
10+
And a wp-content/pcp-config.php file:
11+
"""
12+
<?php
13+
define( 'WP_PLUGIN_CHECK_BOOTSTRAP_FILE', __DIR__ . '/pcp-bootstrap.php' );
14+
"""
15+
16+
When I run the WP-CLI command `plugin list --require=./wp-content/pcp-config.php --require=./wp-content/plugins/plugin-check/cli.php`
17+
Then STDOUT should contain:
18+
"""
19+
PCP bootstrap loaded
20+
"""
21+
22+
Scenario: Missing bootstrap file surfaces a warning and does not halt execution
23+
Given a WP install with the Plugin Check plugin
24+
And a wp-content/pcp-config.php file:
25+
"""
26+
<?php
27+
define( 'WP_PLUGIN_CHECK_BOOTSTRAP_FILE', __DIR__ . '/pcp-bootstrap-missing.php' );
28+
"""
29+
30+
When I run the WP-CLI command `plugin list --require=./wp-content/pcp-config.php --require=./wp-content/plugins/plugin-check/cli.php`
31+
Then STDERR should contain:
32+
"""
33+
WP_PLUGIN_CHECK_BOOTSTRAP_FILE
34+
"""
35+
And STDERR should contain:
36+
"""
37+
but the file does not exist
38+
"""
39+
40+
Scenario: Undefined constant is a no-op
41+
Given a WP install with the Plugin Check plugin
42+
43+
When I run the WP-CLI command `plugin list --require=./wp-content/plugins/plugin-check/cli.php`
44+
Then STDERR should not contain:
45+
"""
46+
WP_PLUGIN_CHECK_BOOTSTRAP_FILE
47+
"""

‎tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php‎

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -92,86 +92,62 @@ public function test_can_set_up_with_failing_filesystem() {
9292
$this->assertFalse( $runtime_setup->can_set_up() );
9393
}
9494

95-
public function test_before_runtime_setup_action_fires() {
95+
public function test_set_up_fires_setup_environment_hooks() {
9696
$this->set_up_mock_filesystem();
9797

98-
$fired = false;
99-
$context = null;
98+
$calls = array();
99+
100100
add_action(
101101
'wp_plugin_check_before_runtime_setup',
102-
function ( $ctx ) use ( &$fired, &$context ) {
103-
$fired = true;
104-
$context = $ctx;
102+
static function ( $environment_setup ) use ( &$calls ) {
103+
$calls[] = array( 'before', $environment_setup );
105104
}
106105
);
107106

108-
$runtime_setup = new Runtime_Environment_Setup();
109-
$runtime_setup->set_up();
110-
111-
$this->assertTrue( $fired );
112-
$this->assertIsArray( $context );
113-
$this->assertArrayHasKey( 'early_exit', $context );
114-
$this->assertFalse( $context['early_exit'] );
115-
}
116-
117-
public function test_after_runtime_setup_action_fires() {
118-
$this->set_up_mock_filesystem();
119-
120-
$fired = false;
121-
$context = null;
122107
add_action(
123108
'wp_plugin_check_after_runtime_setup',
124-
function ( $ctx ) use ( &$fired, &$context ) {
125-
$fired = true;
126-
$context = $ctx;
109+
static function ( $environment_setup ) use ( &$calls ) {
110+
$calls[] = array( 'after', $environment_setup );
127111
}
128112
);
129113

130114
$runtime_setup = new Runtime_Environment_Setup();
131115
$runtime_setup->set_up();
132116

133-
$this->assertTrue( $fired );
134-
$this->assertIsArray( $context );
135-
$this->assertArrayHasKey( 'early_exit', $context );
117+
$this->assertCount( 2, $calls );
118+
$this->assertSame( 'before', $calls[0][0] );
119+
$this->assertSame( 'after', $calls[1][0] );
120+
$this->assertSame( $runtime_setup, $calls[0][1] );
121+
$this->assertSame( $runtime_setup, $calls[1][1] );
136122
}
137123

138-
public function test_before_runtime_cleanup_action_fires() {
139-
$fired = false;
140-
$context = null;
124+
public function test_clean_up_fires_cleanup_environment_hooks() {
125+
$this->set_up_mock_filesystem();
126+
127+
$calls = array();
128+
141129
add_action(
142130
'wp_plugin_check_before_runtime_cleanup',
143-
function ( $ctx ) use ( &$fired, &$context ) {
144-
$fired = true;
145-
$context = $ctx;
131+
static function ( $environment_setup ) use ( &$calls ) {
132+
$calls[] = array( 'before', $environment_setup );
146133
}
147134
);
148135

149-
$runtime_setup = new Runtime_Environment_Setup();
150-
$runtime_setup->clean_up();
151-
152-
$this->assertTrue( $fired );
153-
$this->assertIsArray( $context );
154-
$this->assertArrayHasKey( 'early_exit', $context );
155-
$this->assertFalse( $context['early_exit'] );
156-
}
157-
158-
public function test_after_runtime_cleanup_action_fires() {
159-
$fired = false;
160-
$context = null;
161136
add_action(
162137
'wp_plugin_check_after_runtime_cleanup',
163-
function ( $ctx ) use ( &$fired, &$context ) {
164-
$fired = true;
165-
$context = $ctx;
138+
static function ( $environment_setup ) use ( &$calls ) {
139+
$calls[] = array( 'after', $environment_setup );
166140
}
167141
);
168142

169143
$runtime_setup = new Runtime_Environment_Setup();
170144
$runtime_setup->clean_up();
171145

172-
$this->assertTrue( $fired );
173-
$this->assertIsArray( $context );
174-
$this->assertArrayHasKey( 'early_exit', $context );
146+
$this->assertCount( 2, $calls );
147+
$this->assertSame( 'before', $calls[0][0] );
148+
$this->assertSame( 'after', $calls[1][0] );
149+
$this->assertSame( $runtime_setup, $calls[0][1] );
150+
$this->assertSame( $runtime_setup, $calls[1][1] );
175151
}
176152

177153
public function test_clean_up() {

0 commit comments

Comments
 (0)