From c3c7831d04255707e40f8afab087b404d4927e66 Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 13 Jun 2024 15:29:26 -0400 Subject: [PATCH] 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');