From 790bdb16d790eed7c46fc1cf164cd80003b7680a Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 25 Nov 2023 15:24:52 +0100 Subject: [PATCH 1/3] refactor(WPTestCase) handle older PHPUnit versions --- src/TestCase/WPTestCase.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/TestCase/WPTestCase.php b/src/TestCase/WPTestCase.php index 3d90c79e3..491a6be28 100644 --- a/src/TestCase/WPTestCase.php +++ b/src/TestCase/WPTestCase.php @@ -77,12 +77,19 @@ public function __construct(?string $name = null, array $data = [], $dataName = $_wpTestsBackupStaticAttributesExcludeList; $backupGlobalsReflectionProperty = new \ReflectionProperty($this, 'backupGlobals'); + $backupGlobalsReflectionProperty->setAccessible(true); $isDefinedInThis = $backupGlobalsReflectionProperty->getDeclaringClass()->getName() !== WPTestCase::class; if (!$isDefinedInThis && isset($_wpTestsBackupGlobals) && is_bool($_wpTestsBackupGlobals)) { $this->backupGlobals = $_wpTestsBackupGlobals; } - $backupGlobalsExcludeListReflectionProperty = new \ReflectionProperty($this, 'backupGlobalsExcludeList'); + if (property_exists($this, 'backupGlobalsExcludeList')) { + $backupGlobalsExcludeListReflectionProperty = new \ReflectionProperty($this, 'backupGlobalsExcludeList'); + } else { + // Older versions of PHPUnit. + $backupGlobalsExcludeListReflectionProperty = new \ReflectionProperty($this, 'backupGlobalsBlacklist'); + } + $backupGlobalsExcludeListReflectionProperty->setAccessible(true); $isDefinedInThis = $backupGlobalsExcludeListReflectionProperty->getDeclaringClass() ->getName() !== WPTestCase::class; if (!$isDefinedInThis @@ -96,16 +103,26 @@ public function __construct(?string $name = null, array $data = [], $dataName = } $backupStaticAttributesReflectionProperty = new \ReflectionProperty($this, 'backupStaticAttributes'); + $backupStaticAttributesReflectionProperty->setAccessible(true); $isDefinedInThis = $backupStaticAttributesReflectionProperty->getDeclaringClass() ->getName() !== WPTestCase::class; if (!$isDefinedInThis && isset($_wpTestsBackupStaticAttributes) && is_bool($_wpTestsBackupStaticAttributes)) { $this->backupStaticAttributes = $_wpTestsBackupStaticAttributes; } - $backupStaticAttributesExcludeListReflectionProperty = new \ReflectionProperty( - $this, - 'backupStaticAttributesExcludeList' - ); + if (property_exists($this, 'backupStaticAttributesExcludeList')) { + $backupStaticAttributesExcludeListReflectionProperty = new \ReflectionProperty( + $this, + 'backupStaticAttributesExcludeList' + ); + } else { + // Older versions of PHPUnit. + $backupStaticAttributesExcludeListReflectionProperty = new \ReflectionProperty( + $this, + 'backupStaticAttributesBlacklist' + ); + } + $backupStaticAttributesExcludeListReflectionProperty->setAccessible(true); $isDefinedInThis = $backupStaticAttributesExcludeListReflectionProperty->getDeclaringClass() ->getName() !== WPTestCase::class; if (!$isDefinedInThis From 014a69ea3c120809ba79b72e434ffed1ecfedff0 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 25 Nov 2023 15:52:27 +0100 Subject: [PATCH 2/3] fix(Loop) push only on count > 0 --- src/Process/Loop.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Process/Loop.php b/src/Process/Loop.php index cf2dc1f75..48a9f3d9f 100644 --- a/src/Process/Loop.php +++ b/src/Process/Loop.php @@ -130,7 +130,9 @@ public function addWorkers(array $workers, array $options = []): Loop } } - array_push($this->workers, ...$builtWorkers); + if (count($builtWorkers)) { + array_push($this->workers, ...$builtWorkers); + } $this->sortWorkersByResource(); return $this; From beffe0da2a5b2c4444a070c31fe14711380a8aa2 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 25 Nov 2023 15:56:49 +0100 Subject: [PATCH 3/3] fix(worker-script) unset globals before run --- src/Process/Worker/worker-script.php | 14 +++++++------- src/TestCase/WPTestCase.php | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Process/Worker/worker-script.php b/src/Process/Worker/worker-script.php index 0987b4b80..157d1814a 100644 --- a/src/Process/Worker/worker-script.php +++ b/src/Process/Worker/worker-script.php @@ -4,11 +4,10 @@ use lucatume\WPBrowser\Process\Protocol\Response; use lucatume\WPBrowser\Process\SerializableThrowable; -$processSrcRoot = __DIR__ . '/..'; -require_once $processSrcRoot . '/Protocol/Parser.php'; -require_once $processSrcRoot . '/Protocol/Control.php'; -require_once $processSrcRoot . '/Protocol/Request.php'; -require_once $processSrcRoot . '/Protocol/ProtocolException.php'; +require_once __DIR__ . '/../Protocol/Parser.php'; +require_once __DIR__ . '/../Protocol/Control.php'; +require_once __DIR__ . '/../Protocol/Request.php'; +require_once __DIR__ . '/../Protocol/ProtocolException.php'; try { if (!isset($argv[1])) { @@ -22,8 +21,9 @@ } $request = Request::fromPayload($payload); - $serializableClosure = $request->getSerializableClosure(); - $returnValue = $serializableClosure(); + $_wpBrowserWorkerClosure = $request->getSerializableClosure(); + unset($payload, $request); + $returnValue = $_wpBrowserWorkerClosure(); } catch (Throwable $throwable) { $returnValue = new SerializableThrowable($throwable); } diff --git a/src/TestCase/WPTestCase.php b/src/TestCase/WPTestCase.php index 491a6be28..8c171043f 100644 --- a/src/TestCase/WPTestCase.php +++ b/src/TestCase/WPTestCase.php @@ -42,6 +42,7 @@ class WPTestCase extends Unit // Additional globals. '_wp_registered_theme_features', // wp-browser + '_wpBrowserWorkerClosure', '_wpTestsBackupGlobals', '_wpTestsBackupGlobalsExcludeList', '_wpTestsBackupStaticAttributes',