• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Symfony/Bundle/Test/Response.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\Bundle\Test;
15

16
use Symfony\Component\BrowserKit\Response as BrowserKitResponse;
17
use Symfony\Component\HttpClient\Exception\ClientException;
18
use Symfony\Component\HttpClient\Exception\JsonException;
19
use Symfony\Component\HttpClient\Exception\RedirectionException;
20
use Symfony\Component\HttpClient\Exception\ServerException;
21
use Symfony\Component\HttpClient\Exception\TransportException;
22
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
23
use Symfony\Contracts\HttpClient\ResponseInterface;
24

25
/**
26
 * HTTP Response.
27
 *
28
 * @internal
29
 *
30
 * Partially copied from \Symfony\Component\HttpClient\Response\ResponseTrait
31
 *
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 */
34
final class Response implements ResponseInterface
35
{
36
    private readonly array $headers;
37
    private array $info;
38
    private readonly string $content;
39
    private ?array $jsonData = null;
40

41
    public function __construct(private readonly HttpFoundationResponse $httpFoundationResponse, private readonly BrowserKitResponse $browserKitResponse, array $info)
42
    {
UNCOV
43
        $this->headers = $httpFoundationResponse->headers->all();
245✔
44

45
        // Compute raw headers
UNCOV
46
        $responseHeaders = [];
245✔
UNCOV
47
        foreach ($this->headers as $key => $values) {
245✔
UNCOV
48
            foreach ($values as $value) {
245✔
UNCOV
49
                $responseHeaders[] = \sprintf('%s: %s', $key, $value);
245✔
50
            }
51
        }
52

UNCOV
53
        $this->content = (string) $httpFoundationResponse->getContent();
245✔
UNCOV
54
        $this->info = [
245✔
UNCOV
55
            'http_code' => $httpFoundationResponse->getStatusCode(),
245✔
UNCOV
56
            'error' => null,
245✔
UNCOV
57
            'response_headers' => $responseHeaders,
245✔
UNCOV
58
        ] + $info;
245✔
59
    }
60

61
    public function getInfo(?string $type = null): mixed
62
    {
UNCOV
63
        if ($type) {
5✔
UNCOV
64
            return $this->info[$type] ?? null;
5✔
65
        }
66

UNCOV
67
        return $this->info;
1✔
68
    }
69

70
    /**
71
     * Checks the status, and try to extract message if appropriate.
72
     */
73
    private function checkStatusCode(): void
74
    {
UNCOV
75
        if (500 <= $this->info['http_code']) {
123✔
UNCOV
76
            throw new ServerException($this);
1✔
77
        }
78

UNCOV
79
        if (400 <= $this->info['http_code']) {
122✔
UNCOV
80
            throw new ClientException($this);
1✔
81
        }
82

UNCOV
83
        if (300 <= $this->info['http_code']) {
121✔
UNCOV
84
            throw new RedirectionException($this);
1✔
85
        }
86
    }
87

88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getContent(bool $throw = true): string
92
    {
UNCOV
93
        if ($throw) {
211✔
UNCOV
94
            $this->checkStatusCode();
123✔
95
        }
96

UNCOV
97
        return $this->content;
208✔
98
    }
99

100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getStatusCode(): int
104
    {
UNCOV
105
        return $this->info['http_code'];
6✔
106
    }
107

108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getHeaders(bool $throw = true): array
112
    {
UNCOV
113
        if ($throw) {
3✔
UNCOV
114
            $this->checkStatusCode();
3✔
115
        }
116

UNCOV
117
        return $this->headers;
3✔
118
    }
119

120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function toArray(bool $throw = true): array
124
    {
UNCOV
125
        if ('' === $content = $this->getContent($throw)) {
160✔
UNCOV
126
            throw new TransportException('Response body is empty.');
1✔
127
        }
128

UNCOV
129
        if (null !== $this->jsonData) {
159✔
UNCOV
130
            return $this->jsonData;
4✔
131
        }
132

UNCOV
133
        $contentType = $this->headers['content-type'][0] ?? 'application/json';
159✔
134

UNCOV
135
        if (!preg_match('/\bjson\b/i', $contentType)) {
159✔
UNCOV
136
            throw new JsonException(\sprintf('Response content-type is "%s" while a JSON-compatible one was expected.', $contentType));
1✔
137
        }
138

139
        try {
UNCOV
140
            $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
158✔
UNCOV
141
        } catch (\JsonException $e) {
1✔
UNCOV
142
            throw new JsonException($e->getMessage(), $e->getCode());
1✔
143
        }
144

UNCOV
145
        if (!\is_array($content)) {
157✔
UNCOV
146
            throw new JsonException(\sprintf('JSON content was expected to decode to an array, %s returned.', \gettype($content)));
1✔
147
        }
148

UNCOV
149
        return $this->jsonData = $content;
156✔
150
    }
151

152
    /**
153
     * Returns the internal HttpKernel response.
154
     */
155
    public function getKernelResponse(): HttpFoundationResponse
156
    {
UNCOV
157
        return $this->httpFoundationResponse;
1✔
158
    }
159

160
    /**
161
     * Returns the internal BrowserKit response.
162
     */
163
    public function getBrowserKitResponse(): BrowserKitResponse
164
    {
UNCOV
165
        return $this->browserKitResponse;
1✔
166
    }
167

168
    /**
169
     * {@inheritdoc}.
170
     */
171
    public function cancel(): void
172
    {
UNCOV
173
        $this->info['error'] = 'Response has been canceled.';
1✔
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc