‪TYPO3CMS  ‪main
ServerRequestFactoryTest.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\ServerRequestFactoryInterface;
21 use Psr\Http\Message\ServerRequestInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 final class ‪ServerRequestFactoryTest extends UnitTestCase
31 {
35  public function ‪implementsPsr17FactoryInterface(): void
36  {
37  $factory = new ‪ServerRequestFactory();
38  self::assertInstanceOf(ServerRequestFactoryInterface::class, $factory);
39  }
40 
44  public function ‪serverRequestHasMethodSet(): void
45  {
46  $factory = new ‪ServerRequestFactory();
47  $request = $factory->createServerRequest('POST', '/');
48  self::assertSame('POST', $request->getMethod());
49  }
50 
55  {
56  $factory = new ‪ServerRequestFactory();
57  $request = $factory->createServerRequest('GET', '/');
58  $body = $request->getBody();
59 
60  self::assertInstanceOf(ServerRequestInterface::class, $request);
61 
62  self::assertSame('', $body->__toString());
63  self::assertSame(0, $body->getSize());
64  self::assertTrue($body->isSeekable());
65 
66  $body->write('Foo');
67  self::assertSame(3, $body->getSize());
68  self::assertSame('Foo', $body->__toString());
69  }
70 
74  public function ‪raisesExceptionForInvalidMethod(): void
75  {
76  $this->expectException(\InvalidArgumentException::class);
77  $this->expectExceptionCode(1436717275);
78  $factory = new ‪ServerRequestFactory();
79  $factory->createServerRequest('BOGUS-BODY', '/');
80  }
81 
86  {
87  $_SERVER['HTTP_HOST'] = 'localhost';
88  $_SERVER['REQUEST_URI'] = '/index.php';
89  $_SERVER['REMOTE_ADDR'] = '';
90  $_SERVER['SSL_SESSION_ID'] = '';
91  $_FILES = [
92  'tx_uploadexample_piexample' => [
93  'name' => [
94  'newExample' => [
95  'image' => 'o51pb.jpg',
96  'imageCollection' => [
97  0 => 'composer.json',
98  ],
99  ],
100  ],
101  'type' => [
102  'newExample' => [
103  'image' => 'image/jpeg',
104  'imageCollection' => [
105  0 => 'application/json',
106  ],
107  ],
108  ],
109  'tmp_name' => [
110  'newExample' => [
111  'image' => '/Applications/MAMP/tmp/php/phphXdbcd',
112  'imageCollection' => [
113  0 => '/Applications/MAMP/tmp/php/phpgrZ4bb',
114  ],
115  ],
116  ],
117  'error' => [
118  'newExample' => [
119  'image' => 0,
120  'imageCollection' => [
121  0 => 0,
122  ],
123  ],
124  ],
125  'size' => [
126  'newExample' => [
127  'image' => 59065,
128  'imageCollection' => [
129  0 => 683,
130  ],
131  ],
132  ],
133  ],
134  ];
135 
137 
138  self::assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['image']);
139  self::assertInstanceOf(UploadedFile::class, $uploadedFiles['tx_uploadexample_piexample']['newExample']['image']);
140  self::assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0]);
141  self::assertInstanceOf(
142  UploadedFile::class,
143  $uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0]
144  );
145  }
146 
151  {
152  $_SERVER['HTTP_HOST'] = 'localhost';
153  $_SERVER['REQUEST_URI'] = '/index.php';
154  $_SERVER['REMOTE_ADDR'] = '';
155  $_SERVER['SSL_SESSION_ID'] = '';
156  $_FILES = [];
157 
159 
160  self::assertEmpty($uploadedFiles);
161  }
162 
167  {
168  $_SERVER['HTTP_HOST'] = 'localhost';
169  $_SERVER['REQUEST_URI'] = '/index.php';
170  $_SERVER['REMOTE_ADDR'] = '';
171  $_SERVER['SSL_SESSION_ID'] = '';
172  $_FILES = [
173  'tx_uploadexample_piexample' => [
174  'name' => '',
175  'tmp_name' => '',
176  'error' => 4,
177  'size' => 0,
178  ],
179  ];
180 
182 
183  self::assertEmpty($uploadedFiles);
184  }
185 
189  public function ‪handlesNumericKeys(): void
190  {
191  $_SERVER['HTTP_HOST'] = 'localhost';
192  $_SERVER['REQUEST_URI'] = '/index.php';
193  $_SERVER[1] = '1';
194 
196 
197  self::assertInstanceOf(ServerRequest::class, $request, '$_SERVER with numeric key prevented creation.');
198  self::assertEquals([], $request->getHeader('1'), 'Numeric keys are not processed, default empty array should be returned.');
199  }
200 }
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\handlesNumericKeys
‪handlesNumericKeys()
Definition: ServerRequestFactoryTest.php:189
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNormalizedFromFilesSuperGlobal
‪uploadedFilesAreNormalizedFromFilesSuperGlobal()
Definition: ServerRequestFactoryTest.php:85
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNotCreatedIfTmpNameIsEmpty
‪uploadedFilesAreNotCreatedIfTmpNameIsEmpty()
Definition: ServerRequestFactoryTest.php:166
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest
Definition: ServerRequestFactoryTest.php:31
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:35
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\raisesExceptionForInvalidMethod
‪raisesExceptionForInvalidMethod()
Definition: ServerRequestFactoryTest.php:74
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Http\ServerRequest\getUploadedFiles
‪array getUploadedFiles()
Definition: ServerRequest.php:178
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNotCreatedForEmptyFilesArray
‪uploadedFilesAreNotCreatedForEmptyFilesArray()
Definition: ServerRequestFactoryTest.php:150
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:34
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\implementsPsr17FactoryInterface
‪implementsPsr17FactoryInterface()
Definition: ServerRequestFactoryTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\serverRequestHasMethodSet
‪serverRequestHasMethodSet()
Definition: ServerRequestFactoryTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\serverRequestFactoryHasAWritableEmptyBody
‪serverRequestFactoryHasAWritableEmptyBody()
Definition: ServerRequestFactoryTest.php:54
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59