Skip to content

Commit a543ce7

Browse files
authored
Merge pull request #74: Always run HttpFaker requests in http scope
2 parents 6fcd738 + c42883e commit a543ce7

File tree

7 files changed

+57
-28
lines changed

7 files changed

+57
-28
lines changed

composer.json

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,34 @@
4444
"nyholm/psr7": "^1.5",
4545
"mockery/mockery": "^1.5",
4646
"phpunit/phpunit": "^9.6 || ^10.0",
47-
"spiral/auth": "^3.12",
48-
"spiral/auth-http": "^3.12",
49-
"spiral/boot": "^3.12",
50-
"spiral/events": "^3.12",
51-
"spiral/console": "^3.12",
52-
"spiral/core": "^3.12",
53-
"spiral/http": "^3.12",
54-
"spiral/mailer": "^3.12",
55-
"spiral/queue": "^3.12",
56-
"spiral/session": "^3.12",
57-
"spiral/security": "^3.12",
58-
"spiral/tokenizer": "^3.12",
59-
"spiral/storage": "^3.12",
60-
"spiral/views": "^3.12",
61-
"spiral/translator": "^3.12",
62-
"spiral/scaffolder": "^3.12",
47+
"spiral/auth": "^3.14.3",
48+
"spiral/auth-http": "^3.14.3",
49+
"spiral/boot": "^3.14.3",
50+
"spiral/events": "^3.14.3",
51+
"spiral/console": "^3.14.3",
52+
"spiral/core": "^3.14.3",
53+
"spiral/http": "^3.14.3",
54+
"spiral/mailer": "^3.14.3",
55+
"spiral/queue": "^3.14.3",
56+
"spiral/session": "^3.14.3",
57+
"spiral/security": "^3.14.3",
58+
"spiral/tokenizer": "^3.14.3",
59+
"spiral/storage": "^3.14.3",
60+
"spiral/views": "^3.14.3",
61+
"spiral/translator": "^3.14.3",
62+
"spiral/scaffolder": "^3.14.3",
6363
"symfony/mime": "^6.0 || ^7.0"
6464
},
6565
"suggest": {
6666
"brianium/paratest": "Required to run tests in parallel (^6.0).",
6767
"ext-gd": "Required to use generate fake image files"
6868
},
6969
"require-dev": {
70-
"spiral/framework": "^3.12",
71-
"spiral/roadrunner-bridge": "^2.2 || ^3.0",
70+
"spiral/dumper": "^3.3",
71+
"spiral/framework": "^3.14.3",
72+
"spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0",
7273
"spiral-packages/league-event": "^1.0.1",
73-
"spiral/nyholm-bridge": "^1.2",
74+
"spiral/nyholm-bridge": "^1.3",
7475
"vimeo/psalm": "^5.9"
7576
},
7677
"autoload": {

src/Http/FakeHttp.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class FakeHttp
3737
private ?SessionInterface $session = null;
3838
private BinderInterface $binder;
3939

40+
/**
41+
* @param \Closure(\Closure $closure, array $bindings): mixed $scope Scope runner
42+
*/
4043
public function __construct(
4144
#[Proxy] private readonly ContainerInterface $container,
4245
private readonly FileFactory $fileFactory,
@@ -418,9 +421,7 @@ protected function handleRequest(ServerRequestInterface $request, array $binding
418421
return $this->getHttp()->handle($request);
419422
};
420423

421-
$scope = $this->scope;
422-
423-
return new TestResponse($scope($handler, $bindings));
424+
return new TestResponse(($this->scope)($handler, $bindings));
424425
}
425426

426427
protected function validateRequestData($data): void

src/TestCase.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Spiral\Core\ConfigsInterface;
1515
use Spiral\Core\Container;
1616
use Spiral\Core\ContainerScope;
17+
use Spiral\Core\Internal\Introspector;
1718
use Spiral\Core\Scope;
1819
use Spiral\Testing\Attribute\TestScope;
1920

@@ -173,7 +174,7 @@ public function initApp(array $env = [], Container $container = new Container())
173174
* @param array<string, string|array|callable|object> $bindings
174175
* @throws \Throwable
175176
*/
176-
public function runScoped(Closure $callback, array $bindings = []): mixed
177+
public function runScoped(Closure $callback, array $bindings = [], ?string $name = null): mixed
177178
{
178179
if ($this->environment) {
179180
$bindings[EnvironmentInterface::class] = $this->environment;
@@ -234,7 +235,7 @@ protected function runTest(): mixed
234235
}
235236

236237
$scopes = \is_array($scope->scope) ? $scope->scope : [$scope->scope];
237-
$result = $this->runScopes($scopes, function (): mixed {
238+
$result = self::runScopes($scopes, function (): mixed {
238239
return parent::runTest();
239240
}, $this->getContainer(), $scope->bindings);
240241

@@ -280,18 +281,23 @@ private function getTestScope(): ?TestScope
280281
return null;
281282
}
282283

283-
private function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed
284+
private static function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed
284285
{
286+
begin:
285287
if ($scopes === []) {
286288
return $container->runScope($bindings, $callback);
287289
}
288290

291+
289292
$scope = \array_shift($scopes);
293+
if ($scopes !== null && \in_array($scope, Introspector::scopeNames(), true)) {
294+
goto begin;
295+
}
290296

291297
return $container->runScope(
292298
new Scope($scope, []),
293299
function (Container $container) use ($scopes, $callback, $bindings): mixed {
294-
return $this->runScopes($scopes, $callback, $container, $bindings);
300+
return self::runScopes($scopes, $callback, $container, $bindings);
295301
},
296302
);
297303
}

src/Traits/InteractsWithHttp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final public function fakeHttp(): FakeHttp
2020
return $this->getContainer()->get(FactoryInterface::class)->make(FakeHttp::class, [
2121
'fileFactory' => $this->getFileFactory(),
2222
'scope' => function (\Closure $closure, array $bindings = []) {
23-
return $this->runScoped($closure, $bindings);
23+
return self::runScopes(['http'], $closure, $this->getContainer(), $bindings);
2424
}
2525
]);
2626
}

tests/app/Controller/GetController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Spiral\Testing\Tests\App\Controller;
66

77
use Psr\Http\Message\ServerRequestInterface;
8+
use Spiral\Core\Internal\Introspector;
89
use Spiral\Router\Annotation\Route;
910

1011
class GetController
@@ -20,4 +21,10 @@ public function headers(ServerRequestInterface $request): array
2021
{
2122
return $request->getHeaders();
2223
}
24+
25+
#[Route('/get/scopes', 'get.scopes')]
26+
public function scopes(ServerRequestInterface $request): array
27+
{
28+
return Introspector::scopeNames();
29+
}
2330
}

tests/src/Event/EventDispatcherTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Spiral\Testing\Tests\Event;
66

7+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
78
use PHPUnit\Framework\ExpectationFailedException;
89
use Spiral\Testing\Events\FakeEventDispatcher;
910
use Spiral\Testing\Tests\App\Event\AnotherEvent;
@@ -78,7 +79,7 @@ public function testAssertNotDispatchedSomeEventShouldThrowAnException(): void
7879
public function testAssertNothingDispatchedShouldThrowAnException(): void
7980
{
8081
$this->expectException(ExpectationFailedException::class);
81-
$this->expectExceptionMessage('3 unexpected events were dispatched.');
82+
$this->expectExceptionMessageMatches('/\d+ unexpected events were dispatched./');
8283

8384
$this->http->get('/dispatch/some');
8485

tests/src/Http/FakeHttpTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ public function testGetBodySame(): void
1717
$response->assertBodySame('[]');
1818
}
1919

20+
#[TestScope('http')]
21+
public function testHttpScopeDoesNotConflict(): void
22+
{
23+
$response = $this->fakeHttp()->get('/get/query-params');
24+
$response->assertBodySame('[]');
25+
}
26+
27+
public function testAutoHttpScope(): void
28+
{
29+
$response = $this->fakeHttp()->get('/get/scopes');
30+
$response->assertBodySame('["http-request","http","root"]');
31+
}
32+
2033
public function testGetWithQueryParams(): void
2134
{
2235
$response = $this->fakeHttp()->get('/get/query-params', ['foo' => 'bar', 'baz' => ['foo1' => 'bar1']]);

0 commit comments

Comments
 (0)