Skip to content

Commit 9407c93

Browse files
committed
Reformatted tests to new code style
1 parent 580de0a commit 9407c93

File tree

3 files changed

+67
-64
lines changed

3 files changed

+67
-64
lines changed

tests/RequestTest.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Polymorphine\Message\Request;
1616
use Polymorphine\Message\Uri;
1717
use Psr\Http\Message\RequestInterface;
18+
use Psr\Http\Message\UriInterface;
1819
use InvalidArgumentException;
1920

2021

@@ -26,27 +27,26 @@ public function testRequestInstantiation()
2627
}
2728

2829
/**
29-
* @dataProvider mutatorMethods
30+
* @param callable $mutate fn(Request) => Request
3031
*
31-
* @param $method
32-
* @param $param
32+
* @dataProvider mutatorMethods
3333
*/
34-
public function testMutatorMethod_ReturnsNewInstance($method, $param)
34+
public function testMutatorMethod_ReturnsNewInstance(callable $mutate)
3535
{
3636
$original = $this->request();
37-
$clone1 = $original->{$method}($param);
38-
$clone2 = $original->{$method}($param);
39-
$this->assertNotSame($clone1, $clone2);
40-
$this->assertEquals($clone1, $clone2);
41-
$this->assertNotEquals($original, $clone1);
37+
$cloneA = $mutate($original);
38+
$cloneB = $mutate($original);
39+
$this->assertNotSame($cloneA, $cloneB);
40+
$this->assertEquals($cloneA, $cloneB);
41+
$this->assertNotEquals($original, $cloneA);
4242
}
4343

4444
public function mutatorMethods(): array
4545
{
4646
return [
47-
'withRequestTarget' => ['withRequestTarget', '*'],
48-
'withUri' => ['withUri', Uri::fromString('/some/path')],
49-
'withMethod' => ['withMethod', 'POST']
47+
'withRequestTarget' => [fn (Request $original) => $original->withRequestTarget('*')],
48+
'withUri' => [fn (Request $original) => $original->withUri(Uri::fromString('/some/path'))],
49+
'withMethod' => [fn (Request $original) => $original->withMethod('POST')]
5050
];
5151
}
5252

@@ -135,15 +135,14 @@ public function testWithUriResolvesHostHeader()
135135
$this->assertSame('uri-example.com', $request->withUri($uri, false)->getHeaderLine('host'), $fail);
136136
}
137137

138-
private function request($method = 'GET', array $headers = [], $uri = null, $target = null): Request
139-
{
140-
if (!isset($uri)) {
141-
$uri = Uri::fromString();
142-
}
143-
if (!$target) {
144-
return new Request($method, $uri, null, $headers, []);
145-
}
146-
147-
return new Request($method, $uri, null, $headers, ['target' => $target]);
138+
private function request(
139+
string $method = 'GET',
140+
array $headers = [],
141+
?UriInterface $uri = null,
142+
?string $target = null
143+
): Request {
144+
return $target
145+
? new Request($method, $uri ?? Uri::fromString(), null, $headers, ['target' => $target])
146+
: new Request($method, $uri ?? Uri::fromString(), null, $headers, []);
148147
}
149148
}

tests/ServerRequestTest.php

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,22 @@ public function testGetServerParams_ReturnsInstanceServerParamsArray()
3232
}
3333

3434
/**
35-
* @dataProvider instanceProperties
35+
* @param callable $getValue fn(ServerRequest) => array
3636
*
37-
* @param $method
38-
* @param $key
39-
* @param $params
37+
* @dataProvider instanceProperties
4038
*/
41-
public function testGetters_ReturnConstructorProperties($method, $params, $key)
39+
public function testGetters_ReturnConstructorProperties(string $name, array $value, callable $getValue)
4240
{
43-
$this->assertSame($params, $this->request([$key => $params])->{$method}());
41+
$this->assertSame($value, $getValue($this->request([$name => $value])));
4442
}
4543

46-
public function instanceProperties(): array
44+
public static function instanceProperties(): array
4745
{
4846
return [
49-
'cookie' => ['getCookieParams', ['key' => 'value'], 'cookie'],
50-
'query' => ['getQueryParams', ['key' => 'value'], 'query'],
51-
'pBody' => ['getParsedBody', ['key' => 'value'], 'parsedBody'],
52-
'files' => ['getUploadedFiles', ['key' => new Doubles\FakeUploadedFile()], 'files']
47+
'cookie' => ['cookie', ['key' => 'value'], fn (ServerRequest $request) => $request->getCookieParams()],
48+
'query' => ['query', ['key' => 'value'], fn (ServerRequest $request) => $request->getQueryParams()],
49+
'pBody' => ['parsedBody', ['key' => 'value'], fn (ServerRequest $request) => $request->getParsedBody()],
50+
'files' => ['files', ['key' => new Doubles\FakeUploadedFile()], fn (ServerRequest $request) => $request->getUploadedFiles()]
5351
];
5452
}
5553

@@ -69,44 +67,43 @@ public function testGetAttribute_ReturnsDefaultValueIfAttributeNotPresent()
6967
}
7068

7169
/**
72-
* @dataProvider mutatorMethods
70+
* @param callable $mutate fn(ServerRequest) => ServerRequest
7371
*
74-
* @param $method
75-
* @param $params
72+
* @dataProvider mutatorMethods
7673
*/
77-
public function testMutatorMethods_ReturnNewInstance($method, $params)
74+
public function testMutatorMethods_ReturnNewInstance(callable $mutate)
7875
{
7976
$original = $this->request();
80-
$derived1 = $original->{$method}($params);
81-
$derived2 = $original->{$method}($params);
82-
$this->assertEquals($derived1, $derived2);
83-
$this->assertNotSame($derived1, $derived2);
77+
$derivedA = $mutate($original);
78+
$derivedB = $mutate($original);
79+
$this->assertEquals($derivedA, $derivedB);
80+
$this->assertNotSame($derivedA, $derivedB);
8481
}
8582

86-
public function mutatorMethods(): array
83+
public static function mutatorMethods(): array
8784
{
8885
return [
89-
'cookie' => ['withCookieParams', ['key' => 'value']],
90-
'query' => ['withQueryParams', ['key' => 'value']],
91-
'pBody' => ['withParsedBody', ['key' => 'value']],
92-
'files' => ['withUploadedFiles', ['key' => new Doubles\FakeUploadedFile()]]
86+
'cookie' => [fn (ServerRequest $original) => $original->withCookieParams(['key' => 'value'])],
87+
'query' => [fn (ServerRequest $original) => $original->withQueryParams(['key' => 'value'])],
88+
'pBody' => [fn (ServerRequest $original) => $original->withParsedBody(['key' => 'value'])],
89+
'files' => [fn (ServerRequest $original) => $original->withUploadedFiles(['key' => new Doubles\FakeUploadedFile()])]
9390
];
9491
}
9592

9693
public function testAttributeMutation_ReturnsNewInstance()
9794
{
9895
$original = $this->request();
9996
[$name, $value] = ['name', 'value'];
100-
$derived1 = $original->withAttribute($name, $value);
101-
$derived2 = $original->withAttribute($name, $value);
102-
$this->assertEquals($derived1, $derived2);
103-
$this->assertNotSame($derived1, $derived2);
104-
105-
$original = $derived1;
106-
$derived1 = $original->withoutAttribute($name);
107-
$derived2 = $original->withoutAttribute($name);
108-
$this->assertEquals($derived1, $derived2);
109-
$this->assertNotSame($derived1, $derived2);
97+
$derivedA = $original->withAttribute($name, $value);
98+
$derivedB = $original->withAttribute($name, $value);
99+
$this->assertEquals($derivedA, $derivedB);
100+
$this->assertNotSame($derivedA, $derivedB);
101+
102+
$original = $derivedA;
103+
$derivedA = $original->withoutAttribute($name);
104+
$derivedB = $original->withoutAttribute($name);
105+
$this->assertEquals($derivedA, $derivedB);
106+
$this->assertNotSame($derivedA, $derivedB);
110107
}
111108

112109
public function testGetParsedBodyForRequestWithoutBody_returnsNull()

tests/UriTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ public function testGetPathShouldNotFilterInitialSlashes()
189189
}
190190

191191
/**
192-
* @param $port
192+
* @param mixed $port
193+
*
193194
* @dataProvider invalidPorts
194195
*/
195196
public function testWithPortInvalidArgument_ThrowsException($port)
@@ -213,8 +214,9 @@ public function invalidPorts(): array
213214
}
214215

215216
/**
216-
* @param $user
217-
* @param $pass
217+
* @param mixed $user
218+
* @param mixed $pass
219+
*
218220
* @dataProvider invalidUserInfoArgs
219221
*/
220222
public function testWithUserInfoInvalidArgument_ThrowsException($user, $pass)
@@ -238,7 +240,8 @@ public function invalidUserInfoArgs(): array
238240
}
239241

240242
/**
241-
* @param $scheme
243+
* @param mixed $scheme
244+
*
242245
* @dataProvider invalidNonStringArgs
243246
*/
244247
public function testWithSchemeNonStringArgument_ThrowsException($scheme)
@@ -248,7 +251,8 @@ public function testWithSchemeNonStringArgument_ThrowsException($scheme)
248251
}
249252

250253
/**
251-
* @param $host
254+
* @param mixed $host
255+
*
252256
* @dataProvider invalidNonStringArgs
253257
*/
254258
public function testWithHostNonStringArgument_ThrowsException($host)
@@ -258,7 +262,8 @@ public function testWithHostNonStringArgument_ThrowsException($host)
258262
}
259263

260264
/**
261-
* @param $path
265+
* @param mixed $path
266+
*
262267
* @dataProvider invalidNonStringArgs
263268
*/
264269
public function testWithPathNonStringArgument_ThrowsException($path)
@@ -268,7 +273,8 @@ public function testWithPathNonStringArgument_ThrowsException($path)
268273
}
269274

270275
/**
271-
* @param $query
276+
* @param mixed $query
277+
*
272278
* @dataProvider invalidNonStringArgs
273279
*/
274280
public function testWithQueryNonStringArgument_ThrowsException($query)
@@ -278,7 +284,8 @@ public function testWithQueryNonStringArgument_ThrowsException($query)
278284
}
279285

280286
/**
281-
* @param $fragment
287+
* @param mixed $fragment
288+
*
282289
* @dataProvider invalidNonStringArgs
283290
*/
284291
public function testWithFragmentNonStringArgument_ThrowsException($fragment)

0 commit comments

Comments
 (0)