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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Symfony/Bundle/Test/Client.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\Bundle\FrameworkBundle\KernelBrowser;
17
use Symfony\Component\BrowserKit\CookieJar;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\HttpClient\HttpClientTrait;
20
use Symfony\Component\HttpKernel\KernelInterface;
21
use Symfony\Component\HttpKernel\Profiler\Profile;
22
use Symfony\Component\Security\Core\User\UserInterface;
23
use Symfony\Contracts\HttpClient\HttpClientInterface;
24
use Symfony\Contracts\HttpClient\ResponseInterface;
25
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
26

27
/**
28
 * Convenient test client that makes requests to a Kernel object.
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
final class Client implements HttpClientInterface
33
{
34
    use ClientTrait, HttpClientTrait {
35
        ClientTrait::withOptions insteadof HttpClientTrait;
36
    }
37

38
    /**
39
     * @see HttpClientInterface::OPTIONS_DEFAULTS
40
     */
41
    public const API_OPTIONS_DEFAULTS = [
42
        'auth_basic' => null,
43
        'auth_bearer' => null,
44
        'query' => [],
45
        'headers' => ['accept' => ['application/ld+json']],
46
        'body' => '',
47
        'json' => null,
48
        'base_uri' => 'http://localhost',
49
        'extra' => [],
50
    ];
51

52
    private array $defaultOptions = self::API_OPTIONS_DEFAULTS;
53

54
    private ?Response $response = null;
55

56
    /**
57
     * @param array $defaultOptions Default options for the requests
58
     *
59
     * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
60
     */
61
    public function __construct(private readonly KernelBrowser $kernelBrowser, array $defaultOptions = [])
62
    {
UNCOV
63
        $kernelBrowser->followRedirects(false);
×
UNCOV
64
        if ($defaultOptions) {
×
65
            $this->setDefaultOptions($defaultOptions);
×
66
        }
67
    }
68

69
    /**
70
     * Sets the default options for the requests.
71
     *
72
     * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
73
     */
74
    public function setDefaultOptions(array $defaultOptions): void
75
    {
UNCOV
76
        [, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::API_OPTIONS_DEFAULTS);
×
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function request(string $method, string $url, array $options = []): ResponseInterface
83
    {
UNCOV
84
        $basic = $options['auth_basic'] ?? null;
×
UNCOV
85
        [$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
×
UNCOV
86
        $resolvedUrl = implode('', $url);
×
UNCOV
87
        $server = [];
×
88

89
        // Convert headers to a $_SERVER-like array
UNCOV
90
        foreach (self::extractHeaders($options) as $key => $value) {
×
UNCOV
91
            $normalizedHeaderName = strtoupper(str_replace('-', '_', $key));
×
UNCOV
92
            $header = \in_array($normalizedHeaderName, ['CONTENT_TYPE', 'REMOTE_ADDR'], true) ? $normalizedHeaderName : \sprintf('HTTP_%s', $normalizedHeaderName);
×
93
            // BrowserKit doesn't support setting several headers with the same name
UNCOV
94
            $server[$header] = $value[0] ?? '';
×
95
        }
96

UNCOV
97
        if ($basic) {
×
UNCOV
98
            $credentials = \is_array($basic) ? $basic : explode(':', (string) $basic, 2);
×
UNCOV
99
            $server['PHP_AUTH_USER'] = $credentials[0];
×
UNCOV
100
            $server['PHP_AUTH_PW'] = $credentials[1] ?? '';
×
101
        }
102

UNCOV
103
        $info = [
×
UNCOV
104
            'response_headers' => [],
×
UNCOV
105
            'redirect_count' => 0,
×
UNCOV
106
            'redirect_url' => null,
×
UNCOV
107
            'start_time' => 0.0,
×
UNCOV
108
            'http_method' => $method,
×
UNCOV
109
            'http_code' => 0,
×
UNCOV
110
            'error' => null,
×
UNCOV
111
            'user_data' => $options['user_data'] ?? null,
×
UNCOV
112
            'url' => $resolvedUrl,
×
UNCOV
113
            'primary_port' => 'http:' === $url['scheme'] ? 80 : 443,
×
UNCOV
114
        ];
×
UNCOV
115
        $this->kernelBrowser->request($method, $resolvedUrl, $options['extra']['parameters'] ?? [], $options['extra']['files'] ?? [], $server, $options['body'] ?? null);
×
116

UNCOV
117
        return $this->response = new Response($this->kernelBrowser->getResponse(), $this->kernelBrowser->getInternalResponse(), $info);
×
118
    }
119

120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
124
    {
UNCOV
125
        throw new \LogicException('Not implemented yet');
×
126
    }
127

128
    /**
129
     * Gets the latest response.
130
     *
131
     * @internal
132
     */
133
    public function getResponse(): ?Response
134
    {
UNCOV
135
        return $this->response;
×
136
    }
137

138
    /**
139
     * Gets the underlying test client.
140
     *
141
     * @internal
142
     */
143
    public function getKernelBrowser(): KernelBrowser
144
    {
UNCOV
145
        return $this->kernelBrowser;
×
146
    }
147

148
    // The following methods are proxy methods for KernelBrowser's ones
149

150
    /**
151
     * Returns the container.
152
     */
153
    public function getContainer(): ContainerInterface
154
    {
155
        return $this->kernelBrowser->getContainer();
×
156
    }
157

158
    /**
159
     * Returns the CookieJar instance.
160
     */
161
    public function getCookieJar(): CookieJar
162
    {
163
        return $this->kernelBrowser->getCookieJar();
×
164
    }
165

166
    /**
167
     * Returns the kernel.
168
     */
169
    public function getKernel(): KernelInterface
170
    {
UNCOV
171
        return $this->kernelBrowser->getKernel();
×
172
    }
173

174
    /**
175
     * Gets the profile associated with the current Response.
176
     *
177
     * @return Profile|false A Profile instance
178
     */
179
    public function getProfile(): Profile|false
180
    {
UNCOV
181
        return $this->kernelBrowser->getProfile();
×
182
    }
183

184
    /**
185
     * Enables the profiler for the very next request.
186
     *
187
     * If the profiler is not enabled, the call to this method does nothing.
188
     */
189
    public function enableProfiler(): void
190
    {
UNCOV
191
        $this->kernelBrowser->enableProfiler();
×
192
    }
193

194
    /**
195
     * Disables kernel reboot between requests.
196
     *
197
     * By default, the Client reboots the Kernel for each request. This method
198
     * allows to keep the same kernel across requests.
199
     */
200
    public function disableReboot(): void
201
    {
UNCOV
202
        $this->kernelBrowser->disableReboot();
×
203
    }
204

205
    /**
206
     * Enables kernel reboot between requests.
207
     */
208
    public function enableReboot(): void
209
    {
UNCOV
210
        $this->kernelBrowser->enableReboot();
×
211
    }
212

213
    /**
214
     * Extracts headers depending on the symfony/http-client version being used.
215
     *
216
     * @return array<string, string[]>
217
     */
218
    private static function extractHeaders(array $options): array
219
    {
UNCOV
220
        if (!isset($options['normalized_headers'])) {
×
221
            return $options['headers'];
×
222
        }
223

UNCOV
224
        $headers = [];
×
225

226
        /** @var string $key */
UNCOV
227
        foreach ($options['normalized_headers'] as $key => $values) {
×
UNCOV
228
            foreach ($values as $value) {
×
UNCOV
229
                [, $value] = explode(': ', (string) $value, 2);
×
UNCOV
230
                $headers[$key][] = $value;
×
231
            }
232
        }
233

UNCOV
234
        return $headers;
×
235
    }
236

237
    public function loginUser(UserInterface $user, string $firewallContext = 'main'): self
238
    {
UNCOV
239
        $this->kernelBrowser->loginUser($user, $firewallContext);
×
240

UNCOV
241
        return $this;
×
242
    }
243
}
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