Skip to content

Commit eaffc85

Browse files
authored
Merge pull request #22 from php-http/patch-invalid-arguments
Adding some tests on invalid arguments
2 parents adafb0e + 903f970 commit eaffc85

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

src/MessageTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ public function testWithHeader()
103103

104104
$message = $initialMessage->withHeader('Content-TYPE', 'text/script');
105105
$this->assertEquals('text/script', $message->getHeaderLine('content-type'));
106+
107+
$message = $initialMessage->withHeader('x-foo', ['bar', 'baz']);
108+
$this->assertRegExp('|bar, ?baz|', $message->getHeaderLine('x-foo'));
109+
}
110+
111+
/**
112+
* @dataProvider getInvalidHeaderArguments
113+
*/
114+
public function testWithHeaderInvalidArguments($name, $value)
115+
{
116+
if (isset($this->skippedTests[__FUNCTION__])) {
117+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
118+
}
119+
$this->expectException(\InvalidArgumentException::class);
120+
$initialMessage = $this->getMessage();
121+
$initialMessage->withHeader($name, $value);
122+
}
123+
124+
public function getInvalidHeaderArguments()
125+
{
126+
return [
127+
[[], 'foo'],
128+
['foo', []],
129+
['', ''],
130+
['foo', false],
131+
[false, 'foo'],
132+
['foo', new \stdClass()],
133+
[new \stdClass(), 'foo'],
134+
];
106135
}
107136

108137
public function testWithAddedHeader()
@@ -117,6 +146,19 @@ public function testWithAddedHeader()
117146
$this->assertRegExp('|text/html, ?text/plain|', $message->getHeaderLine('Content-Type'));
118147
}
119148

149+
/**
150+
* @dataProvider getInvalidHeaderArguments
151+
*/
152+
public function testWithAddedHeaderInvalidArguments($name, $value)
153+
{
154+
if (isset($this->skippedTests[__FUNCTION__])) {
155+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
156+
}
157+
$this->expectException(\InvalidArgumentException::class);
158+
$initialMessage = $this->getMessage();
159+
$initialMessage->withAddedHeader($name, $value);
160+
}
161+
120162
/**
121163
* Make sure we maintain headers when we add array values.
122164
*/

src/RequestIntegrationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ public function testMethod()
6666
$this->assertEquals('head', $request->getMethod());
6767
}
6868

69+
/**
70+
* @dataProvider getInvalidMethods
71+
*/
72+
public function testMethodWithInvalidArguments($method)
73+
{
74+
if (isset($this->skippedTests[__FUNCTION__])) {
75+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
76+
}
77+
78+
$this->expectException(\InvalidArgumentException::class);
79+
$this->request->withMethod($method);
80+
}
81+
82+
public function getInvalidMethods()
83+
{
84+
return [
85+
[false],
86+
[['foo']],
87+
[new \stdClass()],
88+
];
89+
}
90+
6991
public function testUri()
7092
{
7193
if (isset($this->skippedTests[__FUNCTION__])) {

src/ResponseIntegrationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ public function testStatusCode()
4747
$this->assertEquals(204, $response->getStatusCode());
4848
}
4949

50+
/**
51+
* @dataProvider getInvalidStatusCodeArguments
52+
*/
53+
public function testStatusCodeInvalidArgument($statusCode)
54+
{
55+
if (isset($this->skippedTests[__FUNCTION__])) {
56+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
57+
}
58+
59+
$this->expectException(\InvalidArgumentException::class);
60+
$this->response->withStatus($statusCode);
61+
}
62+
63+
public function getInvalidStatusCodeArguments()
64+
{
65+
return [
66+
[true],
67+
['foobar'],
68+
[99],
69+
[600],
70+
[200.34],
71+
[new \stdClass()],
72+
];
73+
}
74+
5075
public function testReasonPhrase()
5176
{
5277
if (isset($this->skippedTests[__FUNCTION__])) {

src/UriIntegrationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ public function testScheme()
3838
$this->assertEquals('http', $newUri->getScheme());
3939
}
4040

41+
/**
42+
* @dataProvider getInvalidSchemaArguments
43+
*/
44+
public function testWithSchemeInvalidArguments($schema)
45+
{
46+
if (isset($this->skippedTests[__FUNCTION__])) {
47+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
48+
}
49+
50+
$this->expectException(\InvalidArgumentException::class);
51+
$this->createUri('/')->withScheme($schema);
52+
}
53+
54+
public function getInvalidSchemaArguments()
55+
{
56+
return [
57+
[true],
58+
[['foobar']],
59+
[34],
60+
[new \stdClass()],
61+
];
62+
}
63+
4164
public function testAuthority()
4265
{
4366
if (isset($this->skippedTests[__FUNCTION__])) {

0 commit comments

Comments
 (0)