Skip to content

Commit 30a7596

Browse files
Adapt tests to work with v1.1 and v2.0 of PSR-7 (#68)
* 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. * qa: remove extra linebreak * docs: provides a CHANGELOG entry for #68 * fix: always assert When performing try/catch blocks, and the catch represents a valid condition, we need to assert SOMETHING, or else the test is flagged as risky. A simple assertion that we got a throwable works in these cases. * fix: ensure invalid arguments not caught by implementation are flagged Adds a `fail()` call when an invalid argument is not caught. * qa: remove data provider test cases that fail without strict mode Per a comment from @simPod, removes the following: - From `RequestIntegrationTest::getInvalidMethod()`: - The test cases for 1 and 1.01 (integer and float) - From `ResponseIntegrationTest::getInvalidStatusCodeArguments()`: - The test cases for 200.34 Additionally, this patch adds keys to each entry returned by these two providers, to better facilitate understanding which test failed. The removals were necessary as the test case does not enable strict mode, and these particular types can be converted to the specified type "safely", leading to false negative results. * qa: adapt code to follow project styles Evidently, aligning operators is not allowed in this project. (I wish the required style guide was published with the project...) * qa: do not update before require Per @dbu
1 parent 201aeb5 commit 30a7596

File tree

7 files changed

+113
-27
lines changed

7 files changed

+113
-27
lines changed

.github/workflows/integration.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
# and finally require the implementation to test with source flag (to get integration test cases that might be excluded in git-attributes)
3434
run: |
3535
composer remove --dev guzzlehttp/psr7 laminas/laminas-diactoros nyholm/psr7 ringcentral/psr7 slim/psr7 --no-update
36-
composer update --no-interaction --no-progress
3736
composer require ${{ inputs.package }} --no-interaction --no-progress --prefer-source
3837
3938
- name: Execute tests

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
These validate that usernames and passwords which contain reserved characters (defined by RFC3986) are being encoded
1414
so that the URI does not contain these reserved characters at any time.
1515

16+
- Adds support for testing against PSR-7 1.1 and 2.0. In particular, it adapts tests that were verifying invalid parameters threw `InvalidArgumentException` previously now either throw that OR (more correctly) raise a `TypeError`.
17+
1618
## [1.2.0] - 2022-12-01
1719

1820
### Added

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: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\AssertionFailedError;
57
use Psr\Http\Message\MessageInterface;
8+
use Throwable;
9+
use TypeError;
610

711
/**
812
* Test MessageInterface.
@@ -136,9 +140,24 @@ public function testWithHeaderInvalidArguments($name, $value)
136140
if (isset($this->skippedTests[__FUNCTION__])) {
137141
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
138142
}
139-
$this->expectException(\InvalidArgumentException::class);
140-
$initialMessage = $this->getMessage();
141-
$initialMessage->withHeader($name, $value);
143+
144+
try {
145+
$initialMessage = $this->getMessage();
146+
$initialMessage->withHeader($name, $value);
147+
$this->fail('withHeader() should have raised exception on invalid argument');
148+
} catch (AssertionFailedError $e) {
149+
// invalid argument not caught
150+
throw $e;
151+
} catch (TypeError|InvalidArgumentException $e) {
152+
// valid
153+
$this->assertTrue($e instanceof Throwable);
154+
} catch (Throwable $e) {
155+
// invalid
156+
$this->fail(sprintf(
157+
'Unexpected exception (%s) thrown from withHeader(); expected TypeError or InvalidArgumentException',
158+
gettype($e)
159+
));
160+
}
142161
}
143162

144163
public function getInvalidHeaderArguments()
@@ -174,9 +193,24 @@ public function testWithAddedHeaderInvalidArguments($name, $value)
174193
if (isset($this->skippedTests[__FUNCTION__])) {
175194
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
176195
}
177-
$this->expectException(\InvalidArgumentException::class);
178-
$initialMessage = $this->getMessage();
179-
$initialMessage->withAddedHeader($name, $value);
196+
197+
try {
198+
$initialMessage = $this->getMessage();
199+
$initialMessage->withAddedHeader($name, $value);
200+
$this->fail('withAddedHeader() should have raised exception on invalid argument');
201+
} catch (AssertionFailedError $e) {
202+
// invalid argument not caught
203+
throw $e;
204+
} catch (TypeError|InvalidArgumentException $e) {
205+
// valid
206+
$this->assertTrue($e instanceof Throwable);
207+
} catch (Throwable $e) {
208+
// invalid
209+
$this->fail(sprintf(
210+
'Unexpected exception (%s) thrown from withAddedHeader(); expected TypeError or InvalidArgumentException',
211+
gettype($e)
212+
));
213+
}
180214
}
181215

182216
/**

src/RequestIntegrationTest.php

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

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\AssertionFailedError;
57
use Psr\Http\Message\RequestInterface;
68
use Psr\Http\Message\UriInterface;
9+
use Throwable;
10+
use TypeError;
711

812
/**
913
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -96,19 +100,31 @@ public function testMethodWithInvalidArguments($method)
96100
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
97101
}
98102

99-
$this->expectException(\InvalidArgumentException::class);
100-
$this->request->withMethod($method);
103+
try {
104+
$this->request->withMethod($method);
105+
$this->fail('withMethod() should have raised exception on invalid argument');
106+
} catch (AssertionFailedError $e) {
107+
// invalid argument not caught
108+
throw $e;
109+
} catch (InvalidArgumentException|TypeError $e) {
110+
// valid
111+
$this->assertTrue($e instanceof Throwable);
112+
} catch (Throwable $e) {
113+
// invalid
114+
$this->fail(sprintf(
115+
'Unexpected exception (%s) thrown from withMethod(); expected TypeError or InvalidArgumentException',
116+
gettype($e)
117+
));
118+
}
101119
}
102120

103121
public function getInvalidMethods()
104122
{
105123
return [
106-
[null],
107-
[1],
108-
[1.01],
109-
[false],
110-
[['foo']],
111-
[new \stdClass()],
124+
'null' => [null],
125+
'false' => [false],
126+
'array' => [['foo']],
127+
'object' => [new \stdClass()],
112128
];
113129
}
114130

src/ResponseIntegrationTest.php

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

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\AssertionFailedError;
57
use Psr\Http\Message\ResponseInterface;
8+
use Throwable;
9+
use TypeError;
610

711
/**
812
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -58,19 +62,32 @@ public function testStatusCodeInvalidArgument($statusCode)
5862
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
5963
}
6064

61-
$this->expectException(\InvalidArgumentException::class);
62-
$this->response->withStatus($statusCode);
65+
try {
66+
$this->response->withStatus($statusCode);
67+
$this->fail('withStatus() should have raised exception on invalid argument');
68+
} catch (AssertionFailedError $e) {
69+
// invalid argument not caught
70+
throw $e;
71+
} catch (InvalidArgumentException|TypeError $e) {
72+
// valid
73+
$this->assertTrue($e instanceof Throwable);
74+
} catch (Throwable $e) {
75+
// invalid
76+
$this->fail(sprintf(
77+
'Unexpected exception (%s) thrown from withStatus(); expected TypeError or InvalidArgumentException',
78+
gettype($e)
79+
));
80+
}
6381
}
6482

6583
public function getInvalidStatusCodeArguments()
6684
{
6785
return [
68-
[true],
69-
['foobar'],
70-
[99],
71-
[600],
72-
[200.34],
73-
[new \stdClass()],
86+
'true' => [true],
87+
'string' => ['foobar'],
88+
'too-low' => [99],
89+
'too-high' => [600],
90+
'object' => [new \stdClass()],
7491
];
7592
}
7693

src/UriIntegrationTest.php

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

33
namespace Http\Psr7Test;
44

5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\AssertionFailedError;
57
use Psr\Http\Message\UriInterface;
8+
use Throwable;
9+
use TypeError;
610

711
/**
812
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -47,8 +51,22 @@ public function testWithSchemeInvalidArguments($schema)
4751
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
4852
}
4953

50-
$this->expectException(\InvalidArgumentException::class);
51-
$this->createUri('/')->withScheme($schema);
54+
try {
55+
$this->createUri('/')->withScheme($schema);
56+
$this->fail('withScheme() should have raised exception on invalid argument');
57+
} catch (AssertionFailedError $e) {
58+
// invalid argument not caught
59+
throw $e;
60+
} catch (InvalidArgumentException|TypeError $e) {
61+
// valid
62+
$this->assertTrue($e instanceof Throwable);
63+
} catch (Throwable $e) {
64+
// invalid
65+
$this->fail(sprintf(
66+
'Unexpected exception (%s) thrown from withScheme(); expected TypeError or InvalidArgumentException',
67+
gettype($e)
68+
));
69+
}
5270
}
5371

5472
public function getInvalidSchemaArguments()

0 commit comments

Comments
 (0)