Skip to content

Commit 4fe20be

Browse files
committed
fix(testing): clear validator context between test requests
Prevent stale validation state from leaking between tests by cleaning up ValidatorInterface and scene context entries in InputExtension.
1 parent 1874739 commit 4fe20be

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/Hyperf/Testing/Extension/InputExtension.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44

55
namespace Serendipity\Hyperf\Testing\Extension;
66

7+
use ArrayObject;
78
use FastRoute\Dispatcher;
89
use Hyperf\Context\Context;
10+
use Hyperf\Contract\ValidatorInterface;
911
use Hyperf\HttpMessage\Server\Request;
1012
use Hyperf\HttpServer\Router\Dispatched;
1113
use Hyperf\HttpServer\Router\Handler;
1214
use Hyperf\Validation\ValidationException;
1315
use Psr\Http\Message\ServerRequestInterface;
1416
use Serendipity\Presentation\Input;
1517

18+
use function array_keys;
19+
use function spl_object_hash;
20+
use function sprintf;
21+
use function str_contains;
22+
use function str_ends_with;
23+
1624
/**
1725
* @phpstan-ignore trait.unused
1826
*/
@@ -39,6 +47,17 @@ protected function tearDownInput(bool $isRequestSetUp): void
3947
$this->isRequestSetUp = $isRequestSetUp;
4048
Context::destroy(ServerRequestInterface::class);
4149
Context::destroy('http.request.parsedData');
50+
51+
$context = Context::getContainer();
52+
if ($context instanceof ArrayObject) {
53+
$keys = array_keys($context->getArrayCopy());
54+
foreach ($keys as $key) {
55+
if (str_contains($key, ValidatorInterface::class)
56+
|| str_ends_with($key, ':scene')) {
57+
Context::destroy($key);
58+
}
59+
}
60+
}
4261
}
4362

4463
/**
@@ -58,7 +77,13 @@ final protected function input(
5877
): mixed {
5978
if ($this->isRequestSetUp) {
6079
$this->setUpRequestContext($parsedBody, $queryParams, $params, $headers);
61-
return $this->make($class, $args);
80+
$input = $this->make($class, $args);
81+
82+
$hash = spl_object_hash($input);
83+
Context::destroy(sprintf('%s:%s', $hash, ValidatorInterface::class));
84+
Context::destroy(sprintf('%s:%s', $hash, 'scene'));
85+
86+
return $input;
6287
}
6388
static::fail('Request is not set up.');
6489
}

0 commit comments

Comments
 (0)