‪TYPO3CMS  ‪main
InternalRequestDataMappingTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use GuzzleHttp\Psr7\Query;
21 use PHPUnit\Framework\Attributes\DataProvider;
22 use PHPUnit\Framework\Attributes\Test;
23 use Psr\Http\Message\StreamFactoryInterface;
24 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 final class ‪InternalRequestDataMappingTest extends FunctionalTestCase
28 {
29  protected array ‪$testExtensionsToLoad = [
30  'typo3/sysext/frontend/Tests/Functional/Fixtures/Extensions/test_request_mirror',
31  ];
32 
33  public static function ‪ensureRequestMappingWorksDataProvider(): \Generator
34  {
35  yield 'POST parsedBody(_POST) as parsedBody' => [
36  'uri' => 'https://acme.com/request-mirror',
37  'method' => 'POST',
38  'queryParams' => [],
39  'parsedBody' => ['param1' => 'value1'],
40  'headers' => [
41  'Content-type' => 'application/x-www-form-urlencoded',
42  ],
43  'body' => null,
44  'expectedJsonKeyValues' => [
45  'method' => 'POST',
46  'parsedBody' => ['param1' => 'value1'],
47  'queryParams' => [],
48  'body' => Query::build(['param1' => 'value1']),
49  'headers' => [
50  'Content-type' => [
51  'application/x-www-form-urlencoded',
52  ],
53  'Host' => [
54  'acme.com',
55  ],
56  ],
57  ],
58  ];
59  yield 'PATCH body as parsedBody' => [
60  'uri' => 'https://acme.com/request-mirror',
61  'method' => 'PATCH',
62  'queryParams' => [],
63  'parsedBody' => null,
64  'headers' => [
65  'Content-type' => 'application/x-www-form-urlencoded',
66  ],
67  'body' => Query::build(['param1' => 'value1']),
68  'expectedJsonKeyValues' => [
69  'method' => 'PATCH',
70  'parsedBody' => ['param1' => 'value1'],
71  'queryParams' => [],
72  'body' => Query::build(['param1' => 'value1']),
73  'headers' => [
74  'Content-type' => [
75  'application/x-www-form-urlencoded',
76  ],
77  'Host' => [
78  'acme.com',
79  ],
80  ],
81  ],
82  ];
83  yield 'PUT body as parsedBody' => [
84  'uri' => 'https://acme.com/request-mirror',
85  'method' => 'PUT',
86  'queryParams' => [],
87  'parsedBody' => null,
88  'headers' => [
89  'Content-type' => 'application/x-www-form-urlencoded',
90  ],
91  'body' => Query::build(['param1' => 'value1']),
92  'expectedJsonKeyValues' => [
93  'method' => 'PUT',
94  'parsedBody' => ['param1' => 'value1'],
95  'queryParams' => [],
96  'body' => Query::build(['param1' => 'value1']),
97  'headers' => [
98  'Content-type' => [
99  'application/x-www-form-urlencoded',
100  ],
101  'Host' => [
102  'acme.com',
103  ],
104  ],
105  ],
106  ];
107  yield 'DELETE body as parsedBody' => [
108  'uri' => 'https://acme.com/request-mirror',
109  'method' => 'DELETE',
110  'queryParams' => [],
111  'parsedBody' => null,
112  'headers' => [
113  'Content-type' => 'application/x-www-form-urlencoded',
114  ],
115  'body' => Query::build(['param1' => 'value1']),
116  'expectedJsonKeyValues' => [
117  'method' => 'DELETE',
118  'parsedBody' => ['param1' => 'value1'],
119  'queryParams' => [],
120  'body' => Query::build(['param1' => 'value1']),
121  'headers' => [
122  'Content-type' => [
123  'application/x-www-form-urlencoded',
124  ],
125  'Host' => [
126  'acme.com',
127  ],
128  ],
129  ],
130  ];
131  yield 'POST parsedBody(_POST) as parsedBody and queryParams' => [
132  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
133  'method' => 'POST',
134  'queryParams' => [],
135  'parsedBody' => ['param1' => 'value1'],
136  'headers' => [
137  'Content-type' => 'application/x-www-form-urlencoded',
138  ],
139  'body' => null,
140  'expectedJsonKeyValues' => [
141  'method' => 'POST',
142  'parsedBody' => ['param1' => 'value1'],
143  'queryParams' => ['queryParam1' => 'queryValue1'],
144  'body' => Query::build(['param1' => 'value1']),
145  'headers' => [
146  'Content-type' => [
147  'application/x-www-form-urlencoded',
148  ],
149  'Host' => [
150  'acme.com',
151  ],
152  ],
153  ],
154  ];
155  yield 'PATCH body as parsedBody and queryParams' => [
156  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
157  'method' => 'PATCH',
158  'queryParams' => [],
159  'parsedBody' => null,
160  'headers' => [
161  'Content-type' => 'application/x-www-form-urlencoded',
162  ],
163  'body' => Query::build(['param1' => 'value1']),
164  'expectedJsonKeyValues' => [
165  'method' => 'PATCH',
166  'parsedBody' => ['param1' => 'value1'],
167  'queryParams' => ['queryParam1' => 'queryValue1'],
168  'body' => Query::build(['param1' => 'value1']),
169  'headers' => [
170  'Content-type' => [
171  'application/x-www-form-urlencoded',
172  ],
173  'Host' => [
174  'acme.com',
175  ],
176  ],
177  ],
178  ];
179  yield 'PUT body as parsedBody and queryParams' => [
180  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
181  'method' => 'PUT',
182  'queryParams' => [],
183  'parsedBody' => null,
184  'headers' => [
185  'Content-type' => 'application/x-www-form-urlencoded',
186  ],
187  'body' => Query::build(['param1' => 'value1']),
188  'expectedJsonKeyValues' => [
189  'method' => 'PUT',
190  'parsedBody' => ['param1' => 'value1'],
191  'queryParams' => ['queryParam1' => 'queryValue1'],
192  'body' => Query::build(['param1' => 'value1']),
193  'headers' => [
194  'Content-type' => [
195  'application/x-www-form-urlencoded',
196  ],
197  'Host' => [
198  'acme.com',
199  ],
200  ],
201  ],
202  ];
203  yield 'DELETE body as parsedBody and queryParams' => [
204  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
205  'method' => 'DELETE',
206  'queryParams' => [],
207  'parsedBody' => null,
208  'headers' => [
209  'Content-type' => 'application/x-www-form-urlencoded',
210  ],
211  'body' => Query::build(['param1' => 'value1']),
212  'expectedJsonKeyValues' => [
213  'method' => 'DELETE',
214  'parsedBody' => ['param1' => 'value1'],
215  'queryParams' => ['queryParam1' => 'queryValue1'],
216  'body' => Query::build(['param1' => 'value1']),
217  'headers' => [
218  'Content-type' => [
219  'application/x-www-form-urlencoded',
220  ],
221  'Host' => [
222  'acme.com',
223  ],
224  ],
225  ],
226  ];
227  yield 'GET missing parsedParams filled from request query' => [
228  'uri' => 'https://acme.com/request-mirror?queryParam1=value1',
229  'method' => 'GET',
230  'queryParams' => [],
231  'parsedBody' => null,
232  'headers' => [],
233  'body' => null,
234  'expectedJsonKeyValues' => [
235  'method' => 'GET',
236  'uri' => 'https://acme.com/request-mirror?queryParam1=value1',
237  'queryParams' => ['queryParam1' => 'value1'],
238  'body' => '',
239  'headers' => [
240  'Host' => [
241  'acme.com',
242  ],
243  ],
244  ],
245  ];
246  yield 'GET added missing request uri query arguments from queryParams' => [
247  'uri' => 'https://acme.com/request-mirror',
248  'method' => 'GET',
249  'queryParams' => ['queryParam1' => 'value1'],
250  'parsedBody' => null,
251  'headers' => [],
252  'body' => null,
253  'expectedJsonKeyValues' => [
254  'method' => 'GET',
255  'uri' => 'https://acme.com/request-mirror?queryParam1=value1',
256  'queryParams' => ['queryParam1' => 'value1'],
257  'body' => '',
258  'headers' => [
259  'Host' => [
260  'acme.com',
261  ],
262  ],
263  ],
264  ];
265  yield 'GET request uri queryParams and queryParams are merged' => [
266  'uri' => 'https://acme.com/request-mirror?queryParam2=value2',
267  'method' => 'GET',
268  'queryParams' => ['queryParam1' => 'value1'],
269  'parsedBody' => null,
270  'headers' => [],
271  'body' => null,
272  'expectedJsonKeyValues' => [
273  'method' => 'GET',
274  'uri' => 'https://acme.com/request-mirror?queryParam1=value1&queryParam2=value2',
275  'queryParams' => [
276  'queryParam1' => 'value1',
277  'queryParam2' => 'value2',
278  ],
279  'body' => '',
280  'headers' => [
281  'Host' => [
282  'acme.com',
283  ],
284  ],
285  ],
286  ];
287  }
288 
293  #[DataProvider('ensureRequestMappingWorksDataProvider')]
294  #[Test]
296  string $uri,
297  string $method,
298  array $queryParams,
299  ?array $parsedBody,
300  array $headers,
301  ?string $body,
302  array $expectedJsonKeyValues
303  ): void {
304  $request = (new InternalRequest($uri))
305  ->withMethod($method)
306  ->withQueryParams($queryParams)
307  ->withParsedBody($parsedBody);
308  foreach ($headers as $headerName => $headerValue) {
309  $request = $request->withAddedHeader($headerName, $headerValue);
310  }
311  if ($body) {
312  $streamFactory = $this->get(StreamFactoryInterface::class);
313  $request = $request->withBody($streamFactory->createStream($body));
314  }
315 
316  $response = $this->executeFrontendSubRequest($request);
317  self::assertSame(200, $response->getStatusCode());
318  $json = json_decode((string)$response->getBody(), null, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
319  foreach ($expectedJsonKeyValues as $expectedKey => $expectedValue) {
320  self::assertSame($expectedValue, $json[$expectedKey] ?? null, 'Field "' . $expectedKey . '" must match value');
321  }
322  }
323 }
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest
Definition: InternalRequestDataMappingTest.php:28
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\ensureRequestMappingWorksDataProvider
‪static ensureRequestMappingWorksDataProvider()
Definition: InternalRequestDataMappingTest.php:33
‪TYPO3\CMS\Frontend\Tests\Functional\Request
Definition: InternalRequestDataMappingTest.php:18
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\ensureRequestMappingWorks
‪ensureRequestMappingWorks(string $uri, string $method, array $queryParams, ?array $parsedBody, array $headers, ?string $body, array $expectedJsonKeyValues)
Definition: InternalRequestDataMappingTest.php:295
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: InternalRequestDataMappingTest.php:29