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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

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

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

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

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

55
    private ?Response $response = null;
56

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

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

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

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

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

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

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

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

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

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

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

151
    /**
152
     * Returns the container.
153
     *
154
     * @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet
155
     */
156
    public function getContainer(): ?ContainerInterface
157
    {
158
        return $this->kernelBrowser->getContainer();
×
159
    }
160

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

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

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

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

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

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

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

UNCOV
227
        $headers = [];
×
228

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

UNCOV
237
        return $headers;
×
238
    }
239

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

UNCOV
244
        return $this;
×
245
    }
246
}
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

© 2026 Coveralls, Inc