[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / ServerRequestTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Http\ServerRequest;
18 use TYPO3\CMS\Core\Http\UploadedFile;
19 use TYPO3\CMS\Core\Http\Uri;
20
21 /**
22 * Testcase for \TYPO3\CMS\Core\Http\ServerRequest
23 *
24 * Adapted from https://github.com/phly/http/
25 */
26 class ServerRequestTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * @var ServerRequest
30 */
31 protected $request;
32
33 protected function setUp()
34 {
35 $this->request = new ServerRequest();
36 }
37
38 /**
39 * @test
40 */
41 public function getServerParamsAreEmptyByDefault()
42 {
43 $this->assertEmpty($this->request->getServerParams());
44 }
45
46 /**
47 * @test
48 */
49 public function getQueryParamsAreEmptyByDefault()
50 {
51 $this->assertEmpty($this->request->getQueryParams());
52 }
53
54 /**
55 * @test
56 */
57 public function withQueryParamsMutatorReturnsCloneWithChanges()
58 {
59 $value = ['foo' => 'bar'];
60 $request = $this->request->withQueryParams($value);
61 $this->assertNotSame($this->request, $request);
62 $this->assertEquals($value, $request->getQueryParams());
63 }
64
65 /**
66 * @test
67 */
68 public function getCookieParamsAreEmptyByDefault()
69 {
70 $this->assertEmpty($this->request->getCookieParams());
71 }
72
73 /**
74 * @test
75 */
76 public function withCookieParamsMutatorReturnsCloneWithChanges()
77 {
78 $value = ['foo' => 'bar'];
79 $request = $this->request->withCookieParams($value);
80 $this->assertNotSame($this->request, $request);
81 $this->assertEquals($value, $request->getCookieParams());
82 }
83
84 /**
85 * @test
86 */
87 public function getUploadedFilesAreEmptyByDefault()
88 {
89 $this->assertEmpty($this->request->getUploadedFiles());
90 }
91
92 /**
93 * @test
94 */
95 public function getParsedBodyIsEmptyByDefault()
96 {
97 $this->assertEmpty($this->request->getParsedBody());
98 }
99
100 /**
101 * @test
102 */
103 public function withParsedBodyMutatorReturnsCloneWithChanges()
104 {
105 $value = ['foo' => 'bar'];
106 $request = $this->request->withParsedBody($value);
107 $this->assertNotSame($this->request, $request);
108 $this->assertEquals($value, $request->getParsedBody());
109 }
110
111 /**
112 * @test
113 */
114 public function getAttributesAreEmptyByDefault()
115 {
116 $this->assertEmpty($this->request->getAttributes());
117 }
118
119 /**
120 * @depends testAttributesAreEmptyByDefault
121 * @test
122 */
123 public function withAttributeMutatorReturnsCloneWithChanges()
124 {
125 $request = $this->request->withAttribute('foo', 'bar');
126 $this->assertNotSame($this->request, $request);
127 $this->assertEquals('bar', $request->getAttribute('foo'));
128
129 return $request;
130 }
131
132 /**
133 * @depends testAttributeMutatorReturnsCloneWithChanges
134 * @test
135 */
136 public function withoutAttributeReturnsCloneWithoutAttribute($request)
137 {
138 $new = $request->withoutAttribute('foo');
139 $this->assertNotSame($request, $new);
140 $this->assertNull($new->getAttribute('foo', null));
141 }
142
143 /**
144 * @test
145 */
146 public function constructorUsesProvidedArguments()
147 {
148 $server = [
149 'foo' => 'bar',
150 'baz' => 'bat',
151 ];
152
153 $server['server'] = true;
154
155 $files = [
156 'files' => new UploadedFile('php://temp', 0, 0),
157 ];
158
159 $uri = new Uri('http://example.com');
160 $method = 'POST';
161 $headers = [
162 'host' => ['example.com'],
163 ];
164
165 $request = new ServerRequest(
166 $uri,
167 $method,
168 'php://memory',
169 $headers,
170 $server,
171 $files
172 );
173
174 $this->assertEquals($server, $request->getServerParams());
175 $this->assertEquals($files, $request->getUploadedFiles());
176
177 $this->assertSame($uri, $request->getUri());
178 $this->assertEquals($method, $request->getMethod());
179 $this->assertEquals($headers, $request->getHeaders());
180
181 $body = $request->getBody();
182 $r = new \ReflectionProperty($body, 'stream');
183 $r->setAccessible(true);
184 $stream = $r->getValue($body);
185 $this->assertEquals('php://memory', $stream);
186 }
187 }