Skip to content

Commit a371321

Browse files
committed
Refactored test setup methods
1 parent 0a73f59 commit a371321

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

tests/MessageMethodsTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,8 @@ public function test_WithAddedHeader_SettingInvalidHeaderName_ThrowsException(st
280280

281281
private function message(array $headers = [], $version = null): MessageMethodsClass
282282
{
283-
if (!$version) {
284-
return new MessageMethodsClass(new FakeStream(), $headers);
285-
}
286-
287-
return new MessageMethodsClass(new FakeStream(), $headers, $version);
283+
return $version
284+
? new MessageMethodsClass(new FakeStream(), $headers, $version)
285+
: new MessageMethodsClass(new FakeStream(), $headers);
288286
}
289287
}

tests/RequestTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function test_WithUri_ResolvesHostHeader()
132132
$fail = 'WithUri($uri, true) should not overwrite host header';
133133
$this->assertSame('header-example.com', $request->withUri($uri, true)->getHeaderLine('host'), $fail);
134134
$fail = 'WithUri($uri, [false]) should overwrite host header';
135-
$this->assertSame('uri-example.com', $request->withUri($uri, false)->getHeaderLine('host'), $fail);
135+
$this->assertSame('uri-example.com', $request->withUri($uri)->getHeaderLine('host'), $fail);
136136
}
137137

138138
private function request(
@@ -141,8 +141,6 @@ private function request(
141141
?UriInterface $uri = null,
142142
?string $target = null
143143
): Request {
144-
return $target
145-
? new Request($method, $uri ?? Uri::fromString(), null, $headers, ['target' => $target])
146-
: new Request($method, $uri ?? Uri::fromString(), null, $headers, []);
144+
return new Request($method, $uri ?? Uri::fromString(), null, $headers, $target ? ['target' => $target] : []);
147145
}
148146
}

tests/ResponseTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,38 @@ public function test_WithStatusWithInvalidReasonPhrase_ThrowsException($reason)
130130

131131
public function test_NamedConstructors()
132132
{
133-
$this->equivalentConstructs(
133+
$this->assertEqualResponces(
134134
new Response(303, null, ['Location' => '/foo/bar/234']),
135135
Response::redirect(Uri::fromString('/foo/bar/234'))
136136
);
137-
$this->equivalentConstructs(
137+
$this->assertEqualResponces(
138138
new Response(301, null, ['Location' => '/foo/bar/baz']),
139139
Response::redirect('/foo/bar/baz', 301)
140140
);
141-
$this->equivalentConstructs(new Response(400), Response::badRequest());
142-
$this->equivalentConstructs(new Response(401), Response::unauthorized());
143-
$this->equivalentConstructs(new Response(404), Response::notFound());
144-
$this->equivalentConstructs(
141+
$this->assertEqualResponces(new Response(400), Response::badRequest());
142+
$this->assertEqualResponces(new Response(401), Response::unauthorized());
143+
$this->assertEqualResponces(new Response(404), Response::notFound());
144+
$this->assertEqualResponces(
145145
new Response(404, new FakeStream('Not Found. Sorry.')),
146146
Response::notFound(new FakeStream('Not Found. Sorry.'))
147147
);
148-
$this->equivalentConstructs(
148+
$this->assertEqualResponces(
149149
new Response(200, new FakeStream('text'), ['Content-Type' => 'text/plain']),
150150
Response::text('text')
151151
);
152-
$this->equivalentConstructs(
152+
$this->assertEqualResponces(
153153
new Response(200, new FakeStream('html'), ['Content-Type' => 'text/html']),
154154
Response::html('html')
155155
);
156-
$this->equivalentConstructs(
156+
$this->assertEqualResponces(
157157
new Response(200, new FakeStream('xml'), ['Content-Type' => 'application/xml']),
158158
Response::xml('xml')
159159
);
160160

161161
$data = ['Foo' => "single \"slash 'quote'", 'Bar' => '<tag>&ampersand</tag>"double quote"'];
162162
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT |
163163
JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT;
164-
$this->equivalentConstructs(
164+
$this->assertEqualResponces(
165165
new Response(200, new FakeStream(json_encode($data, $options)), ['Content-Type' => 'application/json']),
166166
Response::json($data)
167167
);
@@ -173,7 +173,7 @@ public function test_RedirectWithInvalidStatusCode_ThrowsException()
173173
Response::redirect('/foo/bar', 200);
174174
}
175175

176-
private function equivalentConstructs(ResponseInterface $responseA, ResponseInterface $responseB)
176+
private function assertEqualResponces(ResponseInterface $responseA, ResponseInterface $responseB): void
177177
{
178178
$bodyA = $responseA->getBody();
179179
$this->assertSame($bodyA->getContents(), $responseB->getBody()->getContents());

tests/ServerDataTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,14 @@ public function test_UploadedFilesSuperGlobalParameterStructure()
106106

107107
public function test_MultipleUploadedFilesSuperGlobalParameterStructure()
108108
{
109-
$files['single'] = $this->fileData('avatar.png');
110-
$nested = [];
111-
foreach ($files['single'] as $name => $value) {
112-
$nested[$name] = [0 => $value, 'nested' => $value, 'multi-nested' => [0 => $value, 'sub-nested' => $value]];
113-
}
114-
$files['multi'] = $nested;
115-
116-
$uploadedFiles = $this->serverData(['files' => $files])->uploadedFiles();
117-
109+
$fileArray = $this->fileData('avatar.png');
110+
$structure = fn ($value) => [0 => $value, 'nested' => $value, 'multi-nested' => [0 => $value, 'sub-nested' => $value]];
111+
$serverData = $this->serverData(['files' => [
112+
'single' => $fileArray,
113+
'multi' => array_map($structure, $fileArray)
114+
]]);
115+
116+
$uploadedFiles = $serverData->uploadedFiles();
118117
$this->assertInstanceOf(UploadedFile::class, $uploadedFiles['single']);
119118
$this->assertInstanceOf(UploadedFile::class, $uploadedFiles['multi'][0]);
120119
$this->assertInstanceOf(UploadedFile::class, $uploadedFiles['multi']['nested']);

tests/UploadedFileTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class UploadedFileTest extends TestCase
2828
private ?string $tempFile = null;
2929
private ?string $movedFile = null;
3030

31-
public function tearDown(): void
31+
protected function tearDown(): void
3232
{
3333
if (is_file($this->tempFile ?? '')) { unlink($this->tempFile); }
3434
if (is_file($this->movedFile ?? '')) { unlink($this->movedFile); }
@@ -152,8 +152,8 @@ private function file(array $data = [], bool $realFile = false): UploadedFile
152152
]);
153153
}
154154

155-
private function targetPath($name = 'test.txt'): string
155+
private function targetPath(): string
156156
{
157-
return $this->movedFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name;
157+
return $this->movedFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test.txt';
158158
}
159159
}

0 commit comments

Comments
 (0)