Skip to content

Commit 0835764

Browse files
committed
feat: update to v2 usage of PSR-7
Refactors tests that were using the assumption of throwing an `InvalidArgumentException` to test for either `InvalidArgumentException` OR `TypeError`. This change will allow testing against PSR-7 1.1+ releases safely.
1 parent 3867c50 commit 0835764

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
"require": {
1717
"php": "^7.2 || ^8.0",
1818
"phpunit/phpunit": "^8.0 || ^9.3",
19-
"psr/http-message": "^1.0"
19+
"psr/http-message": "^1.0 || ^2.0"
2020
},
2121
"require-dev": {
2222
"guzzlehttp/psr7": "^1.7 || ^2.0",
2323
"laminas/laminas-diactoros": "^2.1",
2424
"nyholm/psr7": "^1.0",
2525
"ringcentral/psr7": "^1.2",
26-
"slim/psr7": "dev-master"
26+
"slim/psr7": "^1.4"
2727
},
2828
"extra": {
2929
"branch-alias": {

src/MessageTrait.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
56
use Psr\Http\Message\MessageInterface;
7+
use Throwable;
8+
use TypeError;
69

710
/**
811
* Test MessageInterface.
@@ -136,9 +139,19 @@ public function testWithHeaderInvalidArguments($name, $value)
136139
if (isset($this->skippedTests[__FUNCTION__])) {
137140
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
138141
}
139-
$this->expectException(\InvalidArgumentException::class);
140-
$initialMessage = $this->getMessage();
141-
$initialMessage->withHeader($name, $value);
142+
143+
try {
144+
$initialMessage = $this->getMessage();
145+
$initialMessage->withHeader($name, $value);
146+
} catch (TypeError|InvalidArgumentException $e) {
147+
// valid
148+
} catch (Throwable $e) {
149+
// invalid
150+
$this->fail(sprintf(
151+
'Unexpected exception (%s) thrown from withHeader(); expected TypeError or InvalidArgumentException',
152+
gettype($e)
153+
));
154+
}
142155
}
143156

144157
public function getInvalidHeaderArguments()
@@ -154,6 +167,7 @@ public function getInvalidHeaderArguments()
154167
];
155168
}
156169

170+
157171
public function testWithAddedHeader()
158172
{
159173
if (isset($this->skippedTests[__FUNCTION__])) {
@@ -174,9 +188,19 @@ public function testWithAddedHeaderInvalidArguments($name, $value)
174188
if (isset($this->skippedTests[__FUNCTION__])) {
175189
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
176190
}
177-
$this->expectException(\InvalidArgumentException::class);
178-
$initialMessage = $this->getMessage();
179-
$initialMessage->withAddedHeader($name, $value);
191+
192+
try {
193+
$initialMessage = $this->getMessage();
194+
$initialMessage->withAddedHeader($name, $value);
195+
} catch (TypeError|InvalidArgumentException $e) {
196+
// valid
197+
} catch (Throwable $e) {
198+
// invalid
199+
$this->fail(sprintf(
200+
'Unexpected exception (%s) thrown from withAddedHeader(); expected TypeError or InvalidArgumentException',
201+
gettype($e)
202+
));
203+
}
180204
}
181205

182206
/**

src/RequestIntegrationTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
56
use Psr\Http\Message\RequestInterface;
67
use Psr\Http\Message\UriInterface;
8+
use Throwable;
9+
use TypeError;
710

811
/**
912
* @author Tobias Nyholm <[email protected]>
@@ -96,8 +99,17 @@ public function testMethodWithInvalidArguments($method)
9699
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
97100
}
98101

99-
$this->expectException(\InvalidArgumentException::class);
100-
$this->request->withMethod($method);
102+
try {
103+
$this->request->withMethod($method);
104+
} catch (InvalidArgumentException|TypeError $e) {
105+
// valid
106+
} catch (Throwable $e) {
107+
// invalid
108+
$this->fail(sprintf(
109+
'Unexpected exception (%s) thrown from withMethod(); expected TypeError or InvalidArgumentException',
110+
gettype($e)
111+
));
112+
}
101113
}
102114

103115
public function getInvalidMethods()

src/ResponseIntegrationTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
56
use Psr\Http\Message\ResponseInterface;
7+
use Throwable;
8+
use TypeError;
69

710
/**
811
* @author Tobias Nyholm <[email protected]>
@@ -58,8 +61,17 @@ public function testStatusCodeInvalidArgument($statusCode)
5861
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
5962
}
6063

61-
$this->expectException(\InvalidArgumentException::class);
62-
$this->response->withStatus($statusCode);
64+
try {
65+
$this->response->withStatus($statusCode);
66+
} catch (InvalidArgumentException|TypeError $e) {
67+
// valid
68+
} catch (Throwable $e) {
69+
// invalid
70+
$this->fail(sprintf(
71+
'Unexpected exception (%s) thrown from withStatus(); expected TypeError or InvalidArgumentException',
72+
gettype($e)
73+
));
74+
}
6375
}
6476

6577
public function getInvalidStatusCodeArguments()

src/UriIntegrationTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
56
use Psr\Http\Message\UriInterface;
7+
use Throwable;
8+
use TypeError;
69

710
/**
811
* @author Tobias Nyholm <[email protected]>
@@ -47,8 +50,17 @@ public function testWithSchemeInvalidArguments($schema)
4750
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
4851
}
4952

50-
$this->expectException(\InvalidArgumentException::class);
51-
$this->createUri('/')->withScheme($schema);
53+
try {
54+
$this->createUri('/')->withScheme($schema);
55+
} catch (InvalidArgumentException|TypeError $e) {
56+
// valid
57+
} catch (Throwable $e) {
58+
// invalid
59+
$this->fail(sprintf(
60+
'Unexpected exception (%s) thrown from withScheme(); expected TypeError or InvalidArgumentException',
61+
gettype($e)
62+
));
63+
}
5264
}
5365

5466
public function getInvalidSchemaArguments()

0 commit comments

Comments
 (0)