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