‪TYPO3CMS  10.4
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 class ‪ServerRequestFactoryTest extends UnitTestCase
31 {
36  {
37  $factory = new ‪ServerRequestFactory();
38  self::assertInstanceOf(ServerRequestFactoryInterface::class, $factory);
39  }
40 
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 
75  {
76  return [
77  'true' => [true],
78  'false' => [false],
79  'int' => [1],
80  'float' => [1.1],
81  'array' => [['http://example.com']],
82  'stdClass' => [(object)['href' => 'http://example.com']],
83  ];
84  }
85 
91  {
92  $this->expectException(\InvalidArgumentException::class);
93  $this->expectExceptionCode(1436717272);
94  $factory = new ‪ServerRequestFactory();
95  $factory->createServerRequest('GET', $uri);
96  }
97 
102  {
103  $this->expectException(\InvalidArgumentException::class);
104  $this->expectExceptionCode(1436717275);
105  $factory = new ‪ServerRequestFactory();
106  $factory->createServerRequest('BOGUS-BODY', '/');
107  }
108 
113  {
114  $_SERVER['HTTP_HOST'] = 'localhost';
115  $_SERVER['REQUEST_URI'] = '/index.php';
116  $_SERVER['REMOTE_ADDR'] = '';
117  $_SERVER['SSL_SESSION_ID'] = '';
118  $_FILES = [
119  'tx_uploadexample_piexample' => [
120  'name' => [
121  'newExample' => [
122  'image' => 'o51pb.jpg',
123  'imageCollection' => [
124  0 => 'composer.json',
125  ],
126  ],
127  ],
128  'type' => [
129  'newExample' => [
130  'image' => 'image/jpeg',
131  'imageCollection' => [
132  0 => 'application/json'
133  ]
134  ]
135  ],
136  'tmp_name' => [
137  'newExample' => [
138  'image' => '/Applications/MAMP/tmp/php/phphXdbcd',
139  'imageCollection' => [
140  0 => '/Applications/MAMP/tmp/php/phpgrZ4bb'
141  ]
142  ]
143  ],
144  'error' => [
145  'newExample' => [
146  'image' => 0,
147  'imageCollection' => [
148  0 => 0
149  ]
150  ]
151  ],
152  'size' => [
153  'newExample' => [
154  'image' => 59065,
155  'imageCollection' => [
156  0 => 683
157  ]
158  ]
159  ]
160  ]
161  ];
162 
164 
165  self::assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['image']);
166  self::assertTrue($uploadedFiles['tx_uploadexample_piexample']['newExample']['image'] instanceof ‪UploadedFile);
167  self::assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0]);
168  self::assertTrue($uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0] instanceof ‪UploadedFile);
169  }
170 
175  {
176  $_SERVER['HTTP_HOST'] = 'localhost';
177  $_SERVER['REQUEST_URI'] = '/index.php';
178  $_SERVER['REMOTE_ADDR'] = '';
179  $_SERVER['SSL_SESSION_ID'] = '';
180  $_FILES = [];
181 
183 
184  self::assertEmpty($uploadedFiles);
185  }
186 
191  {
192  $_SERVER['HTTP_HOST'] = 'localhost';
193  $_SERVER['REQUEST_URI'] = '/index.php';
194  $_SERVER['REMOTE_ADDR'] = '';
195  $_SERVER['SSL_SESSION_ID'] = '';
196  $_FILES = [
197  'tx_uploadexample_piexample' => [
198  'name' => '',
199  'tmp_name' => '',
200  'error' => 4,
201  'size' => 0,
202  ],
203  ];
204 
206 
207  self::assertEmpty($uploadedFiles);
208  }
209 
213  public function ‪handlesNumericKeys()
214  {
215  $_SERVER['HTTP_HOST'] = 'localhost';
216  $_SERVER['REQUEST_URI'] = '/index.php';
217  $_SERVER[1] = '1';
218 
220 
221  self::assertInstanceOf(ServerRequest::class, $request, '$_SERVER with numeric key prevented creation.');
222  self::assertEquals([], $request->getHeader('1'), 'Numeric keys are not processed, default empty array should be returned.');
223  }
224 }
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\handlesNumericKeys
‪handlesNumericKeys()
Definition: ServerRequestFactoryTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\testServerRequestHasMethodSet
‪testServerRequestHasMethodSet()
Definition: ServerRequestFactoryTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\testServerRequestFactoryHasAWritableEmptyBody
‪testServerRequestFactoryHasAWritableEmptyBody()
Definition: ServerRequestFactoryTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNormalizedFromFilesSuperGlobal
‪uploadedFilesAreNormalizedFromFilesSuperGlobal()
Definition: ServerRequestFactoryTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\invalidRequestUriDataProvider
‪array invalidRequestUriDataProvider()
Definition: ServerRequestFactoryTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNotCreatedIfTmpNameIsEmpty
‪uploadedFilesAreNotCreatedIfTmpNameIsEmpty()
Definition: ServerRequestFactoryTest.php:190
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest
Definition: ServerRequestFactoryTest.php:31
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\raisesExceptionForInvalidMethod
‪raisesExceptionForInvalidMethod()
Definition: ServerRequestFactoryTest.php:101
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Http\ServerRequest\getUploadedFiles
‪array getUploadedFiles()
Definition: ServerRequest.php:196
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\constructorRaisesExceptionForInvalidUri
‪constructorRaisesExceptionForInvalidUri($uri)
Definition: ServerRequestFactoryTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\uploadedFilesAreNotCreatedForEmptyFilesArray
‪uploadedFilesAreNotCreatedForEmptyFilesArray()
Definition: ServerRequestFactoryTest.php:174
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:31
‪TYPO3\CMS\Core\Tests\Unit\Http\ServerRequestFactoryTest\implementsPsr17FactoryInterface
‪implementsPsr17FactoryInterface()
Definition: ServerRequestFactoryTest.php:35
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59