From e31cfb4dc24e934dfc25b196c96a7bfdbf27d5fe Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 13 Jun 2024 14:03:08 -0400 Subject: [PATCH 1/4] add queue helper --- composer.json | 1 + src/helpers/Queue.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/helpers/Queue.php diff --git a/composer.json b/composer.json index 54d8307..8352e05 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "src/helpers/Craft.php", "src/helpers/Http.php", "src/helpers/Model.php", + "src/helpers/Queue.php", "src/helpers/Test.php" ] }, diff --git a/src/helpers/Queue.php b/src/helpers/Queue.php new file mode 100644 index 0000000..8452b59 --- /dev/null +++ b/src/helpers/Queue.php @@ -0,0 +1,15 @@ +queue->run(); + + return $result; + } +} From be82ddec0d7555974b7a76796818eb78f9d793ba Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 13 Jun 2024 14:05:05 -0400 Subject: [PATCH 2/4] pint --- src/helpers/Queue.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/helpers/Queue.php b/src/helpers/Queue.php index 8452b59..b4bdfaa 100644 --- a/src/helpers/Queue.php +++ b/src/helpers/Queue.php @@ -5,7 +5,8 @@ use Craft; if (! function_exists('queue')) { - function queue($callback) { + function queue($callback) + { $result = $callback(); Craft::$app->queue->run(); From c3c7831d04255707e40f8afab087b404d4927e66 Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 13 Jun 2024 15:29:26 -0400 Subject: [PATCH 3/4] cleanup request state --- src/test/CleanupRequestState.php | 33 ++++++++++++++++++++++++++++++++ src/test/TestCase.php | 1 + tests/RequestStateTest.php | 17 ++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/test/CleanupRequestState.php create mode 100644 tests/RequestStateTest.php diff --git a/src/test/CleanupRequestState.php b/src/test/CleanupRequestState.php new file mode 100644 index 0000000..ebfb783 --- /dev/null +++ b/src/test/CleanupRequestState.php @@ -0,0 +1,33 @@ +hasProperty('afterRequestCallbacks')) { + $reflect = $reflect->getParentClass(); + } + + if ($reflect) { + $property = $reflect->getProperty('afterRequestCallbacks'); + $property->setValue($app, []); + } + } + } +} diff --git a/src/test/TestCase.php b/src/test/TestCase.php index af18af0..e9eb4ed 100644 --- a/src/test/TestCase.php +++ b/src/test/TestCase.php @@ -13,6 +13,7 @@ class TestCase extends \PHPUnit\Framework\TestCase { use ActingAs, Benchmark, + CleanupRequestState, CookieState, DatabaseAssertions, Dd, diff --git a/tests/RequestStateTest.php b/tests/RequestStateTest.php new file mode 100644 index 0000000..e74654c --- /dev/null +++ b/tests/RequestStateTest.php @@ -0,0 +1,17 @@ +onAfterRequest(fn () => throw new Exception('An after request callback was not cleared.')); + + expect(true)->toBeTrue(); +}); + +test('expects request state to be empty', function () { + $app = Craft::$app; + $reflect = new ReflectionClass($app); + while ($reflect && ! $reflect->hasProperty('afterRequestCallbacks')) { + $reflect = $reflect->getParentClass(); + } + + expect($reflect->getProperty('afterRequestCallbacks')->getValue($app))->toBeEmpty(); +})->depends('sets request state'); From 3811ab7dc2bc478a28a870e5f2d0ea6ea1d2497a Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 13 Jun 2024 15:31:40 -0400 Subject: [PATCH 4/4] queue cleanup --- src/helpers/Queue.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/helpers/Queue.php b/src/helpers/Queue.php index b4bdfaa..60de9d6 100644 --- a/src/helpers/Queue.php +++ b/src/helpers/Queue.php @@ -5,9 +5,13 @@ use Craft; if (! function_exists('queue')) { - function queue($callback) + function queue($callback = null): mixed { - $result = $callback(); + $result = null; + + if ($callback) { + $result = $callback(); + } Craft::$app->queue->run();