Skip to content

Commit 4f06efe

Browse files
committed
Add WpTestsStarter::addLivePlugin() #10
1 parent 40cb86a commit 4f06efe

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/WpTestsStarter.php

+79
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class WpTestsStarter
1515

1616
private DbUrlParser $dbUrlParser;
1717

18+
/**
19+
* @var callable[]
20+
*/
21+
private static $livePlugins = [];
22+
1823
/**
1924
* @var callable[]
2025
*/
@@ -56,6 +61,7 @@ public function bootstrap()
5661
$this->defineConstants();
5762
$this->declareGlobals();
5863
$this->writeConfigFile();
64+
$this->installLivePlugin();
5965

6066
$wpBoostrapFile = $this->baseDir . '/tests/phpunit/includes/bootstrap.php';
6167
require_once $wpBoostrapFile;
@@ -188,11 +194,63 @@ public function addActivePlugin(string $plugin): self
188194
return $this;
189195
}
190196

197+
public function addLivePlugin(callable $plugin): self
198+
{
199+
self::$livePlugins[] = $plugin;
200+
201+
return $this;
202+
}
203+
191204
public function useTablePrefix(string $prefix): self
192205
{
193206
return $this->useGlobalVar('table_prefix', $prefix);
194207
}
195208

209+
public static function runLivePlugins(): void
210+
{
211+
foreach(self::$livePlugins as $livePlugin) {
212+
$livePlugin();
213+
}
214+
}
215+
216+
private function installLivePlugin(): void
217+
{
218+
if(!self::$livePlugins) {
219+
return;
220+
}
221+
222+
$pluginCode = <<<'PHP'
223+
<?php
224+
225+
declare(strict_types=1);
226+
227+
/**
228+
* Plugin Name: Wp Tests Starter live plugin
229+
*/
230+
namespace Inpsyde\WpTestsStarter;
231+
232+
if (!class_exists(\Inpsyde\WpTestsStarter\WpTestsStarter::class)) {
233+
return;
234+
}
235+
236+
add_action(
237+
'muplugins_loaded',
238+
[\Inpsyde\WpTestsStarter\WpTestsStarter::class, 'runLivePlugins']
239+
);
240+
PHP;
241+
242+
$muPluginDir = $this->muPluginDir();
243+
if(!is_dir($muPluginDir)) {
244+
mkdir($muPluginDir, 0755, true);
245+
}
246+
247+
$pluginFile = $muPluginDir . '/wp-tests-starter-live-plugin.php';
248+
file_put_contents(
249+
$pluginFile,
250+
$pluginCode
251+
);
252+
}
253+
196254
private function defineConstants(): void
197255
{
198256
foreach ($this->constants as $constant => $value) {
@@ -232,6 +290,27 @@ private function generateSalts()
232290
}
233291
}
234292

293+
private function muPluginDir(): ?string
294+
{
295+
if(defined('WPMU_PLUGIN_DIR')) {
296+
return (string) WPMU_PLUGIN_DIR;
297+
}
298+
299+
if(array_key_exists('WPMU_PLUGIN_DIR', $this->constants)) {
300+
return (string) $this->constants['WPMU_PLUGIN_DIR'];
301+
}
302+
303+
if(defined('WP_CONTENT_DIR')) {
304+
return WP_CONTENT_DIR . '/mu-plugins';
305+
}
306+
307+
if(array_key_exists('WP_CONTENT_DIR', $this->constants)) {
308+
return $this->constants['WP_CONTENT_DIR'] . '/mu-plugins';
309+
}
310+
311+
return $this->baseDir . '/src/wp-content/mu-plugins';
312+
}
313+
235314
private function writeConfigFile()
236315
{
237316
$configFile = $this->getConfigFile();

tests/WpIntegration/WpTestsStarterTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function testBootstrap(): void
3030
);
3131
$testee->useWpPluginDir(self::$pluginDir)
3232
->addActivePlugin(self::$testPlugin)
33+
->addLivePlugin(static function(): void {
34+
defined('WPTS_LIVE_PLUGIN_RUN') or define('WPTS_LIVE_PLUGIN_RUN', true);
35+
})
3336
->bootstrap();
3437

3538
// test if the environment is available
@@ -41,6 +44,7 @@ class_exists(\WP_UnitTestCase::class),
4144
$this->wpDbAssertions();
4245
$this->installedAssertions();
4346
$this->pluginAssertions();
47+
$this->livePluginAssertions();
4448
}
4549

4650
private function wpDbAssertions(): void
@@ -90,4 +94,14 @@ private function pluginAssertions(): void
9094
defined('WPTS_TEST_PLUGIN_LOADED')
9195
);
9296
}
97+
98+
private function livePluginAssertions(): void
99+
{
100+
self::assertFileExists(
101+
WPMU_PLUGIN_DIR . '/wp-tests-starter-live-plugin.php'
102+
);
103+
self::assertTrue(
104+
defined('WPTS_LIVE_PLUGIN_RUN')
105+
);
106+
}
93107
}

0 commit comments

Comments
 (0)