‪TYPO3CMS  11.5
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 Psr\Http\Message\StreamFactoryInterface;
21 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
22 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
23 
24 class ‪InternalRequestDataMappingTest extends FunctionalTestCase
25 {
27  'typo3/sysext/frontend/Tests/Functional/Fixtures/Extensions/test_request_mirror',
28  ];
29 
30  public function ‪ensureRequestMappingWorksDataProvider(): \Generator
31  {
32  yield 'POST parsedBody(_POST) as parsedBody' => [
33  'uri' => 'https://acme.com/request-mirror',
34  'method' => 'POST',
35  'parsedBody' => ['param1' => 'value1'],
36  'headers' => [
37  'Content-type' => 'application/x-www-form-urlencoded',
38  ],
39  'body' => null,
40  'expectedJsonKeyValues' => [
41  'method' => 'POST',
42  'parsedBody' => ['param1' => 'value1'],
43  'queryParams' => [],
44  'body' => '',
45  'headers' => [
46  'Content-type' => [
47  'application/x-www-form-urlencoded',
48  ],
49  'host' => [
50  'acme.com',
51  ],
52  ],
53  ],
54  ];
55  yield 'PATCH body as parsedBody' => [
56  'uri' => 'https://acme.com/request-mirror',
57  'method' => 'PATCH',
58  'parsedBody' => null,
59  'headers' => [
60  'Content-type' => 'application/x-www-form-urlencoded',
61  ],
62  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
63  'expectedJsonKeyValues' => [
64  'method' => 'PATCH',
65  'parsedBody' => ['param1' => 'value1'],
66  'queryParams' => [],
67  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
68  'headers' => [
69  'Content-type' => [
70  'application/x-www-form-urlencoded',
71  ],
72  'host' => [
73  'acme.com',
74  ],
75  ],
76  ],
77  ];
78  yield 'PUT body as parsedBody' => [
79  'uri' => 'https://acme.com/request-mirror',
80  'method' => 'PUT',
81  'parsedBody' => null,
82  'headers' => [
83  'Content-type' => 'application/x-www-form-urlencoded',
84  ],
85  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
86  'expectedJsonKeyValues' => [
87  'method' => 'PUT',
88  'parsedBody' => ['param1' => 'value1'],
89  'queryParams' => [],
90  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
91  'headers' => [
92  'Content-type' => [
93  'application/x-www-form-urlencoded',
94  ],
95  'host' => [
96  'acme.com',
97  ],
98  ],
99  ],
100  ];
101  yield 'DELETE body as parsedBody' => [
102  'uri' => 'https://acme.com/request-mirror',
103  'method' => 'DELETE',
104  'parsedBody' => null,
105  'headers' => [
106  'Content-type' => 'application/x-www-form-urlencoded',
107  ],
108  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
109  'expectedJsonKeyValues' => [
110  'method' => 'DELETE',
111  'parsedBody' => ['param1' => 'value1'],
112  'queryParams' => [],
113  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
114  'headers' => [
115  'Content-type' => [
116  'application/x-www-form-urlencoded',
117  ],
118  'host' => [
119  'acme.com',
120  ],
121  ],
122  ],
123  ];
124  yield 'POST parsedBody(_POST) as parsedBody and queryParams' => [
125  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
126  'method' => 'POST',
127  'parsedBody' => ['param1' => 'value1'],
128  'headers' => [
129  'Content-type' => 'application/x-www-form-urlencoded',
130  ],
131  'body' => null,
132  'expectedJsonKeyValues' => [
133  'method' => 'POST',
134  'parsedBody' => ['param1' => 'value1'],
135  'queryParams' => ['queryParam1' => 'queryValue1'],
136  'body' => '',
137  'headers' => [
138  'Content-type' => [
139  'application/x-www-form-urlencoded',
140  ],
141  'host' => [
142  'acme.com',
143  ],
144  ],
145  ],
146  ];
147  yield 'PATCH body as parsedBody and queryParams' => [
148  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
149  'method' => 'PATCH',
150  'parsedBody' => null,
151  'headers' => [
152  'Content-type' => 'application/x-www-form-urlencoded',
153  ],
154  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
155  'expectedJsonKeyValues' => [
156  'method' => 'PATCH',
157  'parsedBody' => ['param1' => 'value1'],
158  'queryParams' => ['queryParam1' => 'queryValue1'],
159  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
160  'headers' => [
161  'Content-type' => [
162  'application/x-www-form-urlencoded',
163  ],
164  'host' => [
165  'acme.com',
166  ],
167  ],
168  ],
169  ];
170  yield 'PUT body as parsedBody and queryParams' => [
171  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
172  'method' => 'PUT',
173  'parsedBody' => null,
174  'headers' => [
175  'Content-type' => 'application/x-www-form-urlencoded',
176  ],
177  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
178  'expectedJsonKeyValues' => [
179  'method' => 'PUT',
180  'parsedBody' => ['param1' => 'value1'],
181  'queryParams' => ['queryParam1' => 'queryValue1'],
182  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
183  'headers' => [
184  'Content-type' => [
185  'application/x-www-form-urlencoded',
186  ],
187  'host' => [
188  'acme.com',
189  ],
190  ],
191  ],
192  ];
193  yield 'DELETE body as parsedBody and queryParams' => [
194  'uri' => 'https://acme.com/request-mirror?queryParam1=queryValue1',
195  'method' => 'DELETE',
196  'parsedBody' => null,
197  'headers' => [
198  'Content-type' => 'application/x-www-form-urlencoded',
199  ],
200  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
201  'expectedJsonKeyValues' => [
202  'method' => 'DELETE',
203  'parsedBody' => ['param1' => 'value1'],
204  'queryParams' => ['queryParam1' => 'queryValue1'],
205  'body' => \GuzzleHttp\Psr7\Query::build(['param1' => 'value1']),
206  'headers' => [
207  'Content-type' => [
208  'application/x-www-form-urlencoded',
209  ],
210  'host' => [
211  'acme.com',
212  ],
213  ],
214  ],
215  ];
216  }
217 
225  public function ‪ensureRequestMappingWorks(string $uri, string $method, ?array $parsedBody, array $headers, ?string $body, array $expectedJsonKeyValues): void
226  {
227  $request = (new InternalRequest($uri))
228  ->withMethod($method)
229  ->withParsedBody($parsedBody);
230  foreach ($headers as $headerName => $headerValue) {
231  $request = $request->withAddedHeader($headerName, $headerValue);
232  }
233  if ($body) {
234  $streamFactory = $this->get(StreamFactoryInterface::class);
235  $request = $request->withBody($streamFactory->createStream($body));
236  }
237 
238  $response = $this->executeFrontendSubRequest($request);
239  self::assertSame(200, $response->getStatusCode());
240  $json = json_decode((string)$response->getBody(), true);
241  foreach ($expectedJsonKeyValues as $expectedKey => $expectedValue) {
242  self::assertSame($expectedValue, $json[$expectedKey] ?? null);
243  }
244  }
245 }
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest
Definition: InternalRequestDataMappingTest.php:25
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\$testExtensionsToLoad
‪$testExtensionsToLoad
Definition: InternalRequestDataMappingTest.php:26
‪TYPO3\CMS\Frontend\Tests\Functional\Request
Definition: InternalRequestDataMappingTest.php:18
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\ensureRequestMappingWorks
‪ensureRequestMappingWorks(string $uri, string $method, ?array $parsedBody, array $headers, ?string $body, array $expectedJsonKeyValues)
Definition: InternalRequestDataMappingTest.php:225
‪TYPO3\CMS\Frontend\Tests\Functional\Request\InternalRequestDataMappingTest\ensureRequestMappingWorksDataProvider
‪ensureRequestMappingWorksDataProvider()
Definition: InternalRequestDataMappingTest.php:30