Skip to content

Commit 62c1599

Browse files
committed
fix(phpstan): use real Response class in test helper instead of anonymous classes
Replace anonymous class stubs in CreatesTestResponse with Symfony\Component\HttpFoundation\Response, fixing 3 PHPStan errors (missing generics, type mismatch, iterable type) at the root cause rather than suppressing them via ignore rules.
1 parent 923cf43 commit 62c1599

3 files changed

Lines changed: 8 additions & 61 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"require-dev": {
1313
"friendsofphp/php-cs-fixer": "^3.94",
1414
"illuminate/testing": "^11.0 || ^12.0",
15-
"phpstan/phpstan": "^2.0"
15+
"phpstan/phpstan": "^2.0",
16+
"symfony/http-foundation": "^8.0"
1617
},
1718
"suggest": {
1819
"illuminate/testing": "Required for the Laravel adapter (ValidatesOpenApiSchema trait)"

phpstan.neon.dist

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,4 @@ parameters:
1919
path: src/Laravel/OpenApiContractTestingServiceProvider.php
2020
-
2121
message: '#does not specify its types: TResponse#'
22-
paths:
23-
- src/Laravel/ValidatesOpenApiSchema.php
24-
- tests/Unit/ValidatesOpenApiSchemaTest.php
25-
- tests/Unit/ValidatesOpenApiSchemaDefaultSpecTest.php
26-
- tests/Unit/ValidatesOpenApiSchemaAttributeTest.php
27-
-
28-
message: '#expects TResponse of Symfony\\Component\\HttpFoundation\\Response#'
29-
paths:
30-
- tests/Unit/ValidatesOpenApiSchemaTest.php
31-
- tests/Unit/ValidatesOpenApiSchemaDefaultSpecTest.php
32-
- tests/Unit/ValidatesOpenApiSchemaAttributeTest.php
33-
-
34-
message: '#class@anonymous/tests/Helpers/CreatesTestResponse\.php.*no value type specified in iterable type array#'
35-
paths:
36-
- tests/Unit/ValidatesOpenApiSchemaTest.php
37-
- tests/Unit/ValidatesOpenApiSchemaDefaultSpecTest.php
38-
- tests/Unit/ValidatesOpenApiSchemaAttributeTest.php
22+
path: src/Laravel/ValidatesOpenApiSchema.php

tests/Helpers/CreatesTestResponse.php

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,20 @@
44

55
namespace Studio\OpenApiContractTesting\Tests\Helpers;
66

7-
use const CASE_LOWER;
8-
97
use Illuminate\Testing\TestResponse;
10-
11-
use function array_change_key_case;
12-
use function strtolower;
8+
use Symfony\Component\HttpFoundation\Response;
139

1410
trait CreatesTestResponse
1511
{
1612
/**
1713
* @param array<string, string> $headers
14+
*
15+
* @return TestResponse<Response>
1816
*/
1917
private function makeTestResponse(string $content, int $statusCode, array $headers = []): TestResponse
2018
{
21-
$headerBag = new class ($headers) {
22-
/** @var array<string, string> */
23-
private readonly array $headers;
24-
25-
/** @param array<string, string> $headers */
26-
public function __construct(array $headers)
27-
{
28-
$this->headers = array_change_key_case($headers, CASE_LOWER);
29-
}
30-
31-
public function get(string $key, ?string $default = null): ?string
32-
{
33-
return $this->headers[strtolower($key)] ?? $default;
34-
}
35-
};
36-
37-
$baseResponse = new class ($content, $statusCode, $headerBag) {
38-
public readonly object $headers;
39-
40-
public function __construct(
41-
private readonly string $content,
42-
private readonly int $statusCode,
43-
object $headers,
44-
) {
45-
$this->headers = $headers;
46-
}
47-
48-
public function getContent(): string
49-
{
50-
return $this->content;
51-
}
52-
53-
public function getStatusCode(): int
54-
{
55-
return $this->statusCode;
56-
}
57-
};
19+
$response = new Response($content, $statusCode, $headers);
5820

59-
return new TestResponse($baseResponse);
21+
return new TestResponse($response);
6022
}
6123
}

0 commit comments

Comments
 (0)