diff --git a/.gitignore b/.gitignore index f252a70c5..535811fa5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ tests/coverage/* # Ignore local overrides of the PHPCS config file. .phpcs.xml phpcs.xml + +# Ignore PHPUnit results cache file. +.phpunit.result.cache diff --git a/composer.json b/composer.json index 2e051c9e8..1b61196b2 100644 --- a/composer.json +++ b/composer.json @@ -40,13 +40,13 @@ }, "require-dev": { "requests/test-server": "dev-master", - "phpunit/phpunit": "^5.7 || ^6.5 || ^7.5", "squizlabs/php_codesniffer": "^3.5", "phpcompatibility/php-compatibility": "^9.0", "wp-coding-standards/wpcs": "^2.0", "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "php-parallel-lint/php-parallel-lint": "^1.3", - "php-parallel-lint/php-console-highlighter": "^0.5.0" + "php-parallel-lint/php-console-highlighter": "^0.5.0", + "yoast/phpunit-polyfills": "^1.0.0" }, "type": "library", "autoload": { diff --git a/tests/Auth/Basic.php b/tests/Auth/Basic.php index eec54cfc1..eff132613 100644 --- a/tests/Auth/Basic.php +++ b/tests/Auth/Basic.php @@ -1,6 +1,6 @@ assertSame('test', $result->data); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid number of arguments - */ public function testMissingPassword() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid number of arguments'); new Requests_Auth_Basic(array('user')); } diff --git a/tests/ChunkedEncoding.php b/tests/ChunkedEncoding.php index f3ca0a053..e1c03aad7 100644 --- a/tests/ChunkedEncoding.php +++ b/tests/ChunkedEncoding.php @@ -1,6 +1,6 @@ assertFalse(isset($jar['requests-testcookie'])); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Object is a dictionary, not a list - */ public function testCookieJarAsList() { + $this->expectException(Requests_Exception::class); + $this->expectExceptionMessage('Object is a dictionary, not a list'); $cookies = new Requests_Cookie_Jar(); $cookies[] = 'requests-testcookie1=testvalue1'; } @@ -111,7 +109,7 @@ protected function setCookieRequest($cookies) { $response = Requests::get(httpbin('/cookies/set'), array(), $options); $data = json_decode($response->body, true); - $this->assertInternalType('array', $data); + $this->assertIsArray($data); $this->assertArrayHasKey('cookies', $data); return $data['cookies']; } diff --git a/tests/Encoding.php b/tests/Encoding.php index 54dc5ec8a..800fddcf9 100644 --- a/tests/Encoding.php +++ b/tests/Encoding.php @@ -1,6 +1,6 @@ assertSame($original, $decoded); } - - protected function bin2hex($field) { - $field = bin2hex($field); - $field = chunk_split($field, 2, "\\x"); - $field = "\\x" . substr($field, 0, -2); - return $field; - } } diff --git a/tests/IDNAEncoder.php b/tests/IDNAEncoder.php index dc3f37e53..277fc5e9d 100644 --- a/tests/IDNAEncoder.php +++ b/tests/IDNAEncoder.php @@ -1,6 +1,6 @@ assertSame($expected, $result); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Provided string is too long - */ public function testASCIITooLong() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Provided string is too long'); $data = str_repeat('abcd', 20); Requests_IDNAEncoder::encode($data); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Encoded string is too long - */ public function testEncodedTooLong() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Encoded string is too long'); $data = str_repeat("\xe4\xbb\x96", 60); Requests_IDNAEncoder::encode($data); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Provided string begins with ACE prefix - */ public function testAlreadyPrefixed() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Provided string begins with ACE prefix'); Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96"); } @@ -68,43 +62,33 @@ public function testFourByteCharacter() { $this->assertSame('xn--ww6j', $result); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid Unicode codepoint - */ public function testFiveByteCharacter() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6"); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid Unicode codepoint - */ public function testSixByteCharacter() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6"); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid Unicode codepoint - */ public function testInvalidASCIICharacterWithMultibyte() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\0\xc2\xb6"); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid Unicode codepoint - */ public function testUnfinishedMultibyte() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xc2"); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Invalid Unicode codepoint - */ public function testPartialMultibyte() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xc2\xc2\xb6"); } } diff --git a/tests/IRI.php b/tests/IRI.php index 488083fc1..cef75078f 100644 --- a/tests/IRI.php +++ b/tests/IRI.php @@ -40,7 +40,7 @@ * */ -class RequestsTest_IRI extends PHPUnit_Framework_TestCase +class RequestsTest_IRI extends RequestsTest_TestCase { public static function rfc3986_tests() { @@ -384,11 +384,9 @@ public function testWriteAliased() $this->assertSame('test', $iri->fragment); } - /** - * @expectedException PHPUnit_Framework_Error_Notice - */ public function testNonexistantProperty() { + $this->expectNotice('Undefined property: Requests_IRI::nonexistant_prop'); $iri = new Requests_IRI(); $this->assertFalse(isset($iri->nonexistant_prop)); $should_fail = $iri->nonexistant_prop; diff --git a/tests/Proxy/HTTP.php b/tests/Proxy/HTTP.php index 91a8350ff..a6de95deb 100644 --- a/tests/Proxy/HTTP.php +++ b/tests/Proxy/HTTP.php @@ -1,6 +1,6 @@ checkProxyAvailable(); @@ -71,6 +68,8 @@ public function testConnectInvalidParameters($transport) { 'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'), 'transport' => $transport, ); + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Invalid number of arguments'); Requests::get(httpbin('/get'), array(), $options); } diff --git a/tests/Requests.php b/tests/Requests.php index 49eee86f0..8e04e1465 100644 --- a/tests/Requests.php +++ b/tests/Requests.php @@ -1,11 +1,10 @@ expectException('Requests_Exception'); + $this->expectExceptionMessage('Only HTTP(S) requests are handled'); Requests::request('ftp://128.0.0.1/'); } @@ -100,9 +99,6 @@ public function testHeaderOnlyLF() { * * We do not support HTTP/0.9. If this is really an issue for you, file a * new issue, and update your server/proxy to support a proper protocol. - * - * @expectedException Requests_Exception - * @expectedExceptionMessage Response could not be parsed */ public function testInvalidProtocolVersion() { $transport = new RequestsTest_Mock_RawTransport(); @@ -111,14 +107,14 @@ public function testInvalidProtocolVersion() { $options = array( 'transport' => $transport, ); + + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Response could not be parsed'); Requests::get('http://example.com/', array(), $options); } /** - * HTTP/0.9 also appears to use a single CRLF instead of two - * - * @expectedException Requests_Exception - * @expectedExceptionMessage Missing header/body separator + * HTTP/0.9 also appears to use a single CRLF instead of two. */ public function testSingleCRLFSeparator() { $transport = new RequestsTest_Mock_RawTransport(); @@ -127,13 +123,12 @@ public function testSingleCRLFSeparator() { $options = array( 'transport' => $transport, ); + + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Missing header/body separator'); Requests::get('http://example.com/', array(), $options); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Response could not be parsed - */ public function testInvalidStatus() { $transport = new RequestsTest_Mock_RawTransport(); $transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest"; @@ -141,6 +136,9 @@ public function testInvalidStatus() { $options = array( 'transport' => $transport, ); + + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Response could not be parsed'); Requests::get('http://example.com/', array(), $options); } @@ -156,12 +154,10 @@ public function test30xWithoutLocation() { $this->assertSame(0, $response->redirects); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage timed out - */ public function testTimeoutException() { $options = array('timeout' => 0.5); + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('timed out'); Requests::get(httpbin('/delay/3'), array(), $options); } } diff --git a/tests/Response/Headers.php b/tests/Response/Headers.php index b5fbe2a76..65f2b838b 100644 --- a/tests/Response/Headers.php +++ b/tests/Response/Headers.php @@ -1,6 +1,6 @@ expectException('Requests_Exception'); + $this->expectExceptionMessage('Object is a dictionary, not a list'); $headers = new Requests_Response_Headers(); $headers[] = 'text/plain'; } diff --git a/tests/SSL.php b/tests/SSL.php index 8c28c1d55..a23ccd696 100644 --- a/tests/SSL.php +++ b/tests/SSL.php @@ -1,6 +1,6 @@ transport, 'test'); $supported = call_user_func($callback); @@ -17,29 +17,6 @@ public function setUp() { } protected $skip_https = false; - /** - * PHPUnit 6+ compatibility shim. - * - * @param mixed $exception - * @param string $message - * @param int|string $code - */ - public function setExpectedException($exception, $message = '', $code = null) { - if (method_exists('PHPUnit_Framework_TestCase', 'setExpectedException')) { - parent::setExpectedException($exception, $message, $code); - } - else { - $this->expectException($exception); - if ($message !== null) { - $this->expectExceptionMessage($message); - } - if ($code !== null) { - $this->expectExceptionCode($code); - } - } - } - - protected function getOptions($other = array()) { $options = array( 'transport' => $this->transport, @@ -344,14 +321,12 @@ public function testRelativeRedirects() { $this->assertSame(6, $request->redirects); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage Too many redirects - */ public function testTooManyRedirects() { $options = array( 'redirects' => 10, // default, but force just in case ); + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('Too many redirects'); Requests::get(httpbin('/redirect/11'), array(), $this->getOptions($options)); } @@ -437,10 +412,11 @@ public function testStatusCodeThrow($code, $success) { if (!$success) { if ($code >= 400) { - $this->setExpectedException('Requests_Exception_HTTP_' . $code, null, $code); + $this->expectException('Requests_Exception_HTTP_' . $code); + $this->expectExceptionCode($code); } elseif ($code >= 300 && $code < 400) { - $this->setExpectedException('Requests_Exception', null); + $this->expectException('Requests_Exception'); } } @@ -466,7 +442,8 @@ public function testStatusCodeThrowAllowRedirects($code, $success) { if (!$success) { if ($code >= 400 || $code === 304 || $code === 305 || $code === 306) { - $this->setExpectedException('Requests_Exception_HTTP_' . $code, null, $code); + $this->expectException('Requests_Exception_HTTP_' . $code); + $this->expectExceptionCode($code); } } @@ -490,10 +467,6 @@ public function testStatusCodeUnknown() { $this->assertFalse($request->success); } - /** - * @expectedException Requests_Exception_HTTP_Unknown - * @expectedExceptionMessage 599 Unknown - */ public function testStatusCodeThrowUnknown() { $transport = new RequestsTest_Mock_Transport(); $transport->code = 599; @@ -503,6 +476,8 @@ public function testStatusCodeThrowUnknown() { ); $request = Requests::get(httpbin('/status/599'), array(), $options); + $this->expectException('Requests_Exception_HTTP_Unknown'); + $this->expectExceptionMessage('599 Unknown'); $request->throw_for_status(true); } @@ -562,10 +537,8 @@ public function testNonblocking() { $this->assertEquals($empty, $request); } - /** - * @expectedException Requests_Exception - */ public function testBadIP() { + $this->expectException('Requests_Exception'); Requests::get('http://256.256.256.0/', array(), $this->getOptions()); } @@ -582,34 +555,28 @@ public function testHTTPS() { $this->assertEmpty($result['args']); } - /** - * @expectedException Requests_Exception - */ public function testExpiredHTTPS() { if ($this->skip_https) { $this->markTestSkipped('SSL support is not available.'); return; } + $this->expectException('Requests_Exception'); Requests::get('https://testssl-expire.disig.sk/index.en.html', array(), $this->getOptions()); } - /** - * @expectedException Requests_Exception - */ public function testRevokedHTTPS() { if ($this->skip_https) { $this->markTestSkipped('SSL support is not available.'); return; } + $this->expectException('Requests_Exception'); Requests::get('https://testssl-revoked.disig.sk/index.en.html', array(), $this->getOptions()); } /** * Test that SSL fails with a bad certificate - * - * @expectedException Requests_Exception */ public function testBadDomain() { if ($this->skip_https) { @@ -617,6 +584,7 @@ public function testBadDomain() { return; } + $this->expectException('Requests_Exception'); Requests::head('https://wrong.host.badssl.com/', array(), $this->getOptions()); } @@ -663,14 +631,12 @@ public function testSNISupport() { $this->assertSame(200, $request->status_code); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage timed out - */ public function testTimeout() { $options = array( 'timeout' => 1, ); + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('timed out'); Requests::get(httpbin('/delay/10'), array(), $this->getOptions($options)); } @@ -850,13 +816,6 @@ public function testAlternatePort() { $this->assertSame('8080', $matches[1]); } - /** - * This test will be skipped on PHP 8 as it would fail due to the use of an incompatible - * PHPUnit version. Once the test suite is compatible with PHPUnit 9, this "requires" can - * be removed. - * - * @requires PHP < 8 - */ public function testProgressCallback() { $mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock(); $mock->expects($this->atLeastOnce())->method('progress'); @@ -870,13 +829,6 @@ public function testProgressCallback() { Requests::get(httpbin('/get'), array(), $options); } - /** - * This test will be skipped on PHP 8 as it would fail due to the use of an incompatible - * PHPUnit version. Once the test suite is compatible with PHPUnit 9, this "requires" can - * be removed. - * - * @requires PHP < 8 - */ public function testAfterRequestCallback() { $mock = $this->getMockBuilder('stdClass') ->setMethods(array('after_request')) diff --git a/tests/Transport/cURL.php b/tests/Transport/cURL.php index 0690d7b4b..7f7c41774 100644 --- a/tests/Transport/cURL.php +++ b/tests/Transport/cURL.php @@ -3,37 +3,30 @@ class RequestsTest_Transport_cURL extends RequestsTest_Transport_Base { protected $transport = 'Requests_Transport_cURL'; - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage t resolve host - */ public function testBadIP() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('t resolve host'); parent::testBadIP(); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage certificate subject name - */ public function testExpiredHTTPS() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('certificate subject name'); parent::testExpiredHTTPS(); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage certificate subject name - */ public function testRevokedHTTPS() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('certificate subject name'); parent::testRevokedHTTPS(); } /** * Test that SSL fails with a bad certificate - * - * @expectedException Requests_Exception - * @expectedExceptionMessage certificate subject name */ public function testBadDomain() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('certificate subject name'); parent::testBadDomain(); } diff --git a/tests/Transport/fsockopen.php b/tests/Transport/fsockopen.php index 21113e6bf..f77c154a1 100644 --- a/tests/Transport/fsockopen.php +++ b/tests/Transport/fsockopen.php @@ -3,36 +3,29 @@ class RequestsTest_Transport_fsockopen extends RequestsTest_Transport_Base { protected $transport = 'Requests_Transport_fsockopen'; - /** - * @expectedException Requests_Exception - */ public function testBadIP() { + $this->expectException('Requests_Exception'); parent::testBadIP(); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage SSL certificate did not match the requested domain name - */ public function testExpiredHTTPS() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testExpiredHTTPS(); } - /** - * @expectedException Requests_Exception - * @expectedExceptionMessage SSL certificate did not match the requested domain name - */ public function testRevokedHTTPS() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testRevokedHTTPS(); } /** * Test that SSL fails with a bad certificate - * - * @expectedException Requests_Exception - * @expectedExceptionMessage SSL certificate did not match the requested domain name */ public function testBadDomain() { + $this->expectException('Requests_Exception'); + $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testBadDomain(); } @@ -50,6 +43,6 @@ public function testContentLengthHeader() { * Issue #248. */ public function checkContentLengthHeader($headers) { - $this->assertContains('Content-Length: 0', $headers); + $this->assertStringContainsString('Content-Length: 0', $headers); } } diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIterator.php index 812ed087f..01f9e0a2b 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIterator.php @@ -1,6 +1,6 @@ =')) { - class_alias('PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'); - class_alias('PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception'); - class_alias('PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException'); - class_alias('PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice'); - class_alias('PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning'); - class_alias('PHPUnit\Framework\Test', 'PHPUnit_Framework_Test'); - class_alias('PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning'); - class_alias('PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError'); - class_alias('PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite'); - class_alias('PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener'); - class_alias('PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState'); - class_alias('PHPUnit\Util\Getopt', 'PHPUnit_Util_Getopt'); -}