Skip to content

Commit 0a73f59

Browse files
committed
Changed data provider methods to static
1 parent 3c7f742 commit 0a73f59

File tree

7 files changed

+163
-180
lines changed

7 files changed

+163
-180
lines changed

tests/MessageMethodsTest.php

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@
2020

2121
class MessageMethodsTest extends TestCase
2222
{
23+
public static function invalidHeaderNames(): array
24+
{
25+
return [
26+
'empty name' => [''],
27+
'spaced name' => ['header name'],
28+
'invalid name char "@"' => ['email@example']
29+
];
30+
}
31+
32+
public function invalidHeaderValues(): array
33+
{
34+
return [
35+
'null value' => [null],
36+
'bool value' => [true],
37+
'toString object' => [new FakeStream()],
38+
'int within array' => [['valid header', 9001]],
39+
'illegal char' => ["some value\xFF"],
40+
'invalid linebreak \n' => ["some\n value"],
41+
'invalid linebreak \r' => ["some\r value"],
42+
'invalid linebreak \n\r' => ["some\n\r value"],
43+
'no whitespace after linebreak' => ["some\r\nvalue"]
44+
];
45+
}
46+
2347
public function test_Instantiation()
2448
{
2549
$this->assertInstanceOf(MessageInterface::class, $this->message());
@@ -222,72 +246,36 @@ public function test_GetHeaders_ReturnsHeaderValuesIgnoringPassedArrayKeys()
222246
$this->assertSame(['test' => ['first', 'second']], $message->withAddedHeader('test', ['two' => 'second'])->getHeaders());
223247
}
224248

225-
/**
226-
* @dataProvider invalidHeaderNames
227-
*
228-
* @param $name
229-
*/
230-
public function test_Instantiation_WithInvalidHeaderName_ThrowsException($name)
249+
/** @dataProvider invalidHeaderNames */
250+
public function test_Instantiation_WithInvalidHeaderName_ThrowsException(string $name)
231251
{
232252
$this->expectException(InvalidArgumentException::class);
233253
$this->message([$name => 'valid value']);
234254
}
235255

236256
/**
237-
* @dataProvider invalidHeaderNames
257+
* @param mixed $header
238258
*
239-
* @param $name
259+
* @dataProvider invalidHeaderValues
240260
*/
241-
public function test_WithHeader_SettingInvalidHeaderName_ThrowsException($name)
261+
public function test_Instantiation_WithInvalidHeaderValue_ThrowsException($header)
242262
{
243263
$this->expectException(InvalidArgumentException::class);
244-
$this->message()->withHeader($name, 'valid value');
264+
$this->message(['test' => $header]);
245265
}
246266

247-
/**
248-
* @dataProvider invalidHeaderNames
249-
*
250-
* @param $name
251-
*/
252-
public function test_WithAddedHeader_SettingInvalidHeaderName_ThrowsException($name)
267+
/** @dataProvider invalidHeaderNames */
268+
public function test_WithHeader_SettingInvalidHeaderName_ThrowsException(string $name)
253269
{
254270
$this->expectException(InvalidArgumentException::class);
255-
$this->message()->withAddedHeader($name, 'valid value');
256-
}
257-
258-
public function invalidHeaderNames(): array
259-
{
260-
return [
261-
'empty name' => [''],
262-
'spaced name' => ['header name'],
263-
'invalid name char "@"' => ['email@example']
264-
];
271+
$this->message()->withHeader($name, 'valid value');
265272
}
266273

267-
/**
268-
* @dataProvider invalidHeaderValues
269-
*
270-
* @param $header
271-
*/
272-
public function test_Instantiation_WithInvalidHeaderValues_ThrowsException($header)
274+
/** @dataProvider invalidHeaderNames */
275+
public function test_WithAddedHeader_SettingInvalidHeaderName_ThrowsException(string $name)
273276
{
274277
$this->expectException(InvalidArgumentException::class);
275-
$this->message(['test' => $header]);
276-
}
277-
278-
public function invalidHeaderValues(): array
279-
{
280-
return [
281-
'null value' => [null],
282-
'bool value' => [true],
283-
'toString object' => [new FakeStream()],
284-
'int within array' => [['valid header', 9001]],
285-
'illegal char' => ["some value\xFF"],
286-
'invalid linebreak \n' => ["some\n value"],
287-
'invalid linebreak \r' => ["some\r value"],
288-
'invalid linebreak \n\r' => ["some\n\r value"],
289-
'no whitespace after linebreak' => ["some\r\nvalue"]
290-
];
278+
$this->message()->withAddedHeader($name, 'valid value');
291279
}
292280

293281
private function message(array $headers = [], $version = null): MessageMethodsClass

tests/RequestTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
class RequestTest extends TestCase
2323
{
24+
public static function mutatorMethods(): array
25+
{
26+
return [
27+
'withRequestTarget' => [fn (Request $original) => $original->withRequestTarget('*')],
28+
'withUri' => [fn (Request $original) => $original->withUri(Uri::fromString('/some/path'))],
29+
'withMethod' => [fn (Request $original) => $original->withMethod('POST')]
30+
];
31+
}
32+
2433
public function test_Instantiation()
2534
{
2635
$this->assertInstanceOf(RequestInterface::class, $this->request());
@@ -41,15 +50,6 @@ public function test_MutatorMethods_ReturnNewInstance(callable $mutate)
4150
$this->assertNotEquals($original, $cloneA);
4251
}
4352

44-
public function mutatorMethods(): array
45-
{
46-
return [
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')]
50-
];
51-
}
52-
5353
public function test_GetMethod_ReturnsInstanceMethod()
5454
{
5555
$this->assertSame('POST', $this->request('POST')->getMethod());

tests/ResponseTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@
2121

2222
class ResponseTest extends TestCase
2323
{
24+
public static function invalidStatusCodes(): array
25+
{
26+
return [
27+
'null' => [null],
28+
'false' => [false],
29+
'string' => ['200'],
30+
'below min range' => [99],
31+
'above max range' => [600]
32+
];
33+
}
34+
35+
public static function invalidReasonPhrases(): array
36+
{
37+
return [
38+
'array' => [['Reason in array']],
39+
'false' => [false],
40+
'int' => [20]
41+
];
42+
}
43+
2444
public function test_Instantiation()
2545
{
2646
$this->assertInstanceOf(ResponseInterface::class, $this->response());
@@ -76,31 +96,20 @@ public function test_ConstructorWithInvalidStatusCode_ThrowsException()
7696
}
7797

7898
/**
79-
* @dataProvider invalidStatusCodes
80-
*
8199
* @param $code
100+
*
101+
* @dataProvider invalidStatusCodes
82102
*/
83103
public function test_WithStatusWithInvalidStatusCode_ThrowsException($code)
84104
{
85105
$this->expectException(InvalidArgumentException::class);
86106
$this->response()->withStatus($code);
87107
}
88108

89-
public function invalidStatusCodes(): array
90-
{
91-
return [
92-
'null' => [null],
93-
'false' => [false],
94-
'string' => ['200'],
95-
'below min range' => [99],
96-
'above max range' => [600]
97-
];
98-
}
99-
100109
/**
101-
* @dataProvider invalidReasonPhrases
102-
*
103110
* @param $reason
111+
*
112+
* @dataProvider invalidReasonPhrases
104113
*/
105114
public function test_ConstructorWithInvalidReasonPhrase_ThrowsException($reason)
106115
{
@@ -109,25 +118,16 @@ public function test_ConstructorWithInvalidReasonPhrase_ThrowsException($reason)
109118
}
110119

111120
/**
112-
* @dataProvider invalidReasonPhrases
113-
*
114121
* @param $reason
122+
*
123+
* @dataProvider invalidReasonPhrases
115124
*/
116125
public function test_WithStatusWithInvalidReasonPhrase_ThrowsException($reason)
117126
{
118127
$this->expectException(InvalidArgumentException::class);
119128
$this->response()->withStatus(200, $reason);
120129
}
121130

122-
public function invalidReasonPhrases(): array
123-
{
124-
return [
125-
'array' => [['Reason in array']],
126-
'false' => [false],
127-
'int' => [20]
128-
];
129-
}
130-
131131
public function test_NamedConstructors()
132132
{
133133
$this->equivalentConstructs(

tests/ServerDataTest.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ class ServerDataTest extends TestCase
2727
{
2828
public static ?array $nativeCallResult = null;
2929

30-
public function tearDown(): void
30+
protected function tearDown(): void
3131
{
3232
self::$nativeCallResult = null;
3333
}
3434

35+
public static function normalizedHeaderNames(): array
36+
{
37+
return [
38+
['HTTP_ACCEPT', 'Accept'],
39+
['HTTP_ACCEPT_ENCODING', 'Accept-Encoding'],
40+
['HTTP_CONTENT_MD5', 'Content-MD5'],
41+
['CONTENT_TYPE', 'Content-Type']
42+
];
43+
}
44+
3545
public function test_Instantiation()
3646
{
3747
$this->assertInstanceOf(ServerData::class, $this->serverData());
@@ -66,28 +76,13 @@ public function test_OverridingSuperglobals()
6676
$this->assertSame([], $request->getAttributes());
6777
}
6878

69-
/**
70-
* @dataProvider normalizeHeaderNames
71-
*
72-
* @param $serverKey
73-
* @param $headerName
74-
*/
75-
public function test_NormalizedHeaderNamesFromServerArray($serverKey, $headerName)
79+
/** @dataProvider normalizedHeaderNames */
80+
public function test_NormalizedHeaderNamesFromServerArray(string $serverKey, string $headerName)
7681
{
7782
$data = $this->serverData(['server' => [$serverKey => 'value']]);
7883
$this->assertTrue(ServerRequest::fromServerData($data)->hasHeader($headerName));
7984
}
8085

81-
public function normalizeHeaderNames(): array
82-
{
83-
return [
84-
['HTTP_ACCEPT', 'Accept'],
85-
['HTTP_ACCEPT_ENCODING', 'Accept-Encoding'],
86-
['HTTP_CONTENT_MD5', 'Content-MD5'],
87-
['CONTENT_TYPE', 'Content-Type']
88-
];
89-
}
90-
9186
public function test_ResolvingAuthorizationHeader()
9287
{
9388
$this->assertFalse(ServerRequest::fromServerData($this->serverData())->hasHeader('Authorization'));

tests/ServerRequestTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020

2121
class ServerRequestTest extends TestCase
2222
{
23+
public static function instanceProperties(): array
24+
{
25+
return [
26+
'cookie' => ['cookie', ['key' => 'value'], fn (ServerRequest $request) => $request->getCookieParams()],
27+
'query' => ['query', ['key' => 'value'], fn (ServerRequest $request) => $request->getQueryParams()],
28+
'pBody' => ['parsedBody', ['key' => 'value'], fn (ServerRequest $request) => $request->getParsedBody()],
29+
'files' => ['files', ['key' => new Doubles\FakeUploadedFile()], fn (ServerRequest $request) => $request->getUploadedFiles()]
30+
];
31+
}
32+
33+
public static function mutatorMethods(): array
34+
{
35+
return [
36+
'cookie' => [fn (ServerRequest $original) => $original->withCookieParams(['key' => 'value'])],
37+
'query' => [fn (ServerRequest $original) => $original->withQueryParams(['key' => 'value'])],
38+
'pBody' => [fn (ServerRequest $original) => $original->withParsedBody(['key' => 'value'])],
39+
'files' => [fn (ServerRequest $original) => $original->withUploadedFiles(['key' => new Doubles\FakeUploadedFile()])]
40+
];
41+
}
42+
2343
public function test_Instantiation()
2444
{
2545
$this->assertInstanceOf(ServerRequestInterface::class, $this->request());
@@ -41,16 +61,6 @@ public function test_Getters_ReturnConstructorProperties(string $name, array $va
4161
$this->assertSame($value, $getValue($this->request([$name => $value])));
4262
}
4363

44-
public static function instanceProperties(): array
45-
{
46-
return [
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()]
51-
];
52-
}
53-
5464
public function test_GetAttribute_WhenAttributeExists_ReturnsAttributeValue()
5565
{
5666
$request = $this->request()->withAttribute('name', 'value');
@@ -80,16 +90,6 @@ public function test_MutatorMethods_ReturnNewInstance(callable $mutate)
8090
$this->assertNotSame($derivedA, $derivedB);
8191
}
8292

83-
public static function mutatorMethods(): array
84-
{
85-
return [
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()])]
90-
];
91-
}
92-
9393
public function test_AttributeMutation_ReturnsNewInstance()
9494
{
9595
$original = $this->request();

0 commit comments

Comments
 (0)