[BUGFIX] Unit tests call parent::tearDown()
[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\TestingFramework\Core\Unit\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 * @test
121 */
122 public function withAttributeMutatorReturnsCloneWithChanges()
123 {
124 $request = $this->request->withAttribute('foo', 'bar');
125 $this->assertNotSame($this->request, $request);
126 $this->assertEquals('bar', $request->getAttribute('foo'));
127
128 return $request;
129 }
130
131 /**
132 * @test
133 */
134 public function withoutAttributeReturnsCloneWithoutAttribute()
135 {
136 $request = $this->request;
137 $new = $request->withoutAttribute('foo');
138 $this->assertNotSame($request, $new);
139 $this->assertNull($new->getAttribute('foo', null));
140 }
141
142 /**
143 * @test
144 */
145 public function constructorUsesProvidedArguments()
146 {
147 $server = [
148 'foo' => 'bar',
149 'baz' => 'bat',
150 ];
151
152 $server['server'] = true;
153
154 $files = [
155 'files' => new UploadedFile('php://temp', 0, 0),
156 ];
157
158 $uri = new Uri('http://example.com');
159 $method = 'POST';
160 $headers = [
161 'host' => ['example.com'],
162 ];
163
164 $request = new ServerRequest(
165 $uri,
166 $method,
167 'php://memory',
168 $headers,
169 $server,
170 $files
171 );
172
173 $this->assertEquals($server, $request->getServerParams());
174 $this->assertEquals($files, $request->getUploadedFiles());
175
176 $this->assertSame($uri, $request->getUri());
177 $this->assertEquals($method, $request->getMethod());
178 $this->assertEquals($headers, $request->getHeaders());
179
180 $body = $request->getBody();
181 $r = new \ReflectionProperty($body, 'stream');
182 $r->setAccessible(true);
183 $stream = $r->getValue($body);
184 $this->assertEquals('php://memory', $stream);
185 }
186 }