Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into craft5
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Sep 27, 2024
2 parents b57f4d8 + 195ae93 commit e93c34e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"src/helpers/Craft.php",
"src/helpers/Http.php",
"src/helpers/Model.php",
"src/helpers/Queue.php",
"src/helpers/Test.php"
]
},
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace markhuot\craftpest\helpers\queue;

use Craft;

if (! function_exists('queue')) {
function queue($callback = null): mixed
{
$result = null;

if ($callback) {
$result = $callback();
}

Craft::$app->queue->run();

return $result;
}
}
33 changes: 33 additions & 0 deletions src/test/CleanupRequestState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace markhuot\craftpest\test;

use Craft;
use markhuot\craftpest\web\Application;

trait CleanupRequestState
{
/**
* Cleanup any request state that may be left over from previous tests.
*
* This is normally cleaned up (and run) after a request goes through Craft's router.
* However, if a test isn't interacting with the route and is just calling actions or
* running queues then the after request callbacks won't be cleared.
*/
public function tearDownCleanupRequestState(): void
{
$app = Craft::$app;

if ($app instanceof Application) {
$reflect = new \ReflectionClass($app);
while ($reflect && ! $reflect->hasProperty('afterRequestCallbacks')) {
$reflect = $reflect->getParentClass();
}

if ($reflect) {
$property = $reflect->getProperty('afterRequestCallbacks');
$property->setValue($app, []);
}
}
}
}
1 change: 1 addition & 0 deletions src/test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
{
use ActingAs,
Benchmark,
CleanupRequestState,
CookieState,
DatabaseAssertions,
Dd,
Expand Down
17 changes: 17 additions & 0 deletions tests/RequestStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

test('sets request state', function () {
Craft::$app->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');

0 comments on commit e93c34e

Please sign in to comment.