From 749debd4d1e03acec5d5ba46c462e36df985085e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 21:12:12 +0200 Subject: [PATCH 01/19] Tests: allow for namespacing the tests This is a preliminary commit to set up the basics to allow for namespacing the tests. It: * Adds an entry for the namespaced variants of the tests to the PHPUnit config file, so those tests will be found and run. * Adds a custom autoloader for PSR4-based namespaced test classes to the test bootstrap. The old setup is - for now - still left in place to allow for testing each individual step in the transformation. Once the transformation has been completed, the old setup will be removed. **Note**: _the testsuite cannot be named `Requests` as older PHPUnit versions, in that case, would confuse it with the `Requests` class and expect that class to extend the `PHPUnit_Framework_TestCase` class. With that in mind, the testsuite is called `RequestsTests`._ --- phpunit.xml.dist | 3 +++ tests/bootstrap.php | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 48856b5eb..0b5445a7a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -29,6 +29,9 @@ tests/SSL.php tests/Utility/FilteredIterator.php + + tests + diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 10aa38155..f6a2a17b1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -24,14 +24,42 @@ function define_from_env($name, $default = false) { require_once dirname(__DIR__) . '/library/Requests.php'; Requests::register_autoloader(); -$polyfill_autoloader = dirname(__DIR__) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; -if (file_exists($polyfill_autoloader)) { - require_once $polyfill_autoloader; +if (is_dir(dirname(__DIR__) . '/vendor') + && file_exists(dirname(__DIR__) . '/vendor/autoload.php') + && file_exists(dirname(__DIR__) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php') +) { + $vendor_dir = dirname(__DIR__) . '/vendor'; } else { - echo 'Please run `composer install` before attempting to run the tests.', PHP_EOL; + echo 'Please run `composer install` before attempting to run the unit tests. +You can still run the tests using a PHPUnit phar file, but some test dependencies need to be available. +'; die(1); } +// Load the PHPUnit Polyfills autoloader. +require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; + +// New autoloader. +spl_autoload_register( + function ($class_name) { + // Only try & load our own classes. + if (stripos($class_name, 'Requests\\Tests\\') !== 0) { + return false; + } + + // Strip namespace prefix 'Requests\Tests\'. + $relative_class = substr($class_name, 15); + $file = realpath(__DIR__ . '/' . strtr($relative_class, '\\', '/') . '.php'); + + if (file_exists($file)) { + include_once $file; + } + + return true; + } +); + +// Old autoloader. function autoload_tests($class) { if (strpos($class, 'RequestsTest_') !== 0) { return; From ae3e0b6ba72b0033b8e0e83cf211464352b6f30b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 19:43:37 +0200 Subject: [PATCH 02/19] Tests: namespace the base TestCase class ... and add use statements for it and adjust the `extends` in all the test classes which extend from it. --- tests/Auth/Basic.php | 4 +++- tests/ChunkedEncoding.php | 4 +++- tests/Cookies.php | 4 +++- tests/Encoding.php | 4 +++- tests/IDNAEncoder.php | 4 +++- tests/IRI.php | 4 +++- tests/Proxy/HTTP.php | 4 +++- tests/Requests.php | 4 +++- tests/Response/Headers.php | 4 +++- tests/SSL.php | 4 +++- tests/Session.php | 4 +++- tests/TestCase.php | 6 ++++-- tests/Transport/Base.php | 4 +++- tests/Utility/FilteredIterator.php | 4 +++- 14 files changed, 43 insertions(+), 15 deletions(-) diff --git a/tests/Auth/Basic.php b/tests/Auth/Basic.php index eff132613..0b0c738b4 100644 --- a/tests/Auth/Basic.php +++ b/tests/Auth/Basic.php @@ -1,6 +1,8 @@ expectException('Requests_Exception'); diff --git a/tests/Response/Headers.php b/tests/Response/Headers.php index 65f2b838b..6aa8bb1a9 100644 --- a/tests/Response/Headers.php +++ b/tests/Response/Headers.php @@ -1,6 +1,8 @@ transport, 'test'); $supported = call_user_func($callback); diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIterator.php index 01f9e0a2b..5bfd81bed 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIterator.php @@ -1,6 +1,8 @@ Date: Tue, 29 Jun 2021 22:00:30 +0200 Subject: [PATCH 03/19] Tests: namespace the `Transport` base TestCase class ... and add use statements for it and adjust the `extends` in all the test classes which extend from it. --- tests/Transport/{Base.php => BaseTestCase.php} | 9 ++++++++- tests/Transport/cURL.php | 4 +++- tests/Transport/fsockopen.php | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) rename tests/Transport/{Base.php => BaseTestCase.php} (99%) diff --git a/tests/Transport/Base.php b/tests/Transport/BaseTestCase.php similarity index 99% rename from tests/Transport/Base.php rename to tests/Transport/BaseTestCase.php index cca93b595..bfbfbfca2 100644 --- a/tests/Transport/Base.php +++ b/tests/Transport/BaseTestCase.php @@ -1,8 +1,15 @@ transport, 'test'); $supported = call_user_func($callback); diff --git a/tests/Transport/cURL.php b/tests/Transport/cURL.php index 7f7c41774..701883012 100644 --- a/tests/Transport/cURL.php +++ b/tests/Transport/cURL.php @@ -1,6 +1,8 @@ Date: Tue, 29 Jun 2021 20:31:21 +0200 Subject: [PATCH 04/19] Tests: namespace the `ChunkedDecoding` test class This commit: * Namespaces the class. * Renames the `RequestsTest_ChunkedDecoding` class to `ChunkedDecodingTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{ChunkedEncoding.php => ChunkedEncodingTest.php} | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) rename tests/{ChunkedEncoding.php => ChunkedEncodingTest.php} (96%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0b5445a7a..a2fdbdf29 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/ChunkedEncoding.php tests/Cookies.php tests/Encoding.php tests/IDNAEncoder.php diff --git a/tests/ChunkedEncoding.php b/tests/ChunkedEncodingTest.php similarity index 96% rename from tests/ChunkedEncoding.php rename to tests/ChunkedEncodingTest.php index fc32b5d75..4465aa112 100644 --- a/tests/ChunkedEncoding.php +++ b/tests/ChunkedEncodingTest.php @@ -1,8 +1,12 @@ Date: Tue, 29 Jun 2021 20:40:33 +0200 Subject: [PATCH 05/19] Tests: namespace the `Cookies` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Cookies` class to `CookiesTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{Cookies.php => CookiesTest.php} | 13 +++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) rename tests/{Cookies.php => CookiesTest.php} (98%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a2fdbdf29..6079b1eae 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/Cookies.php tests/Encoding.php tests/IDNAEncoder.php tests/IRI.php diff --git a/tests/Cookies.php b/tests/CookiesTest.php similarity index 98% rename from tests/Cookies.php rename to tests/CookiesTest.php index cfe9296a8..24655544a 100644 --- a/tests/Cookies.php +++ b/tests/CookiesTest.php @@ -1,8 +1,17 @@ Date: Tue, 29 Jun 2021 20:41:33 +0200 Subject: [PATCH 06/19] Tests: namespace the `Encoding` test class This commit: * Namespaces the class. * Renames the `RequestsTests_Encoding` class to `EncodingTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{Encoding.php => EncodingTest.php} | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) rename tests/{Encoding.php => EncodingTest.php} (96%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6079b1eae..692afaabd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/Encoding.php tests/IDNAEncoder.php tests/IRI.php tests/Requests.php diff --git a/tests/Encoding.php b/tests/EncodingTest.php similarity index 96% rename from tests/Encoding.php rename to tests/EncodingTest.php index 99bf3688d..74d2a65ac 100644 --- a/tests/Encoding.php +++ b/tests/EncodingTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 20:42:33 +0200 Subject: [PATCH 07/19] Tests: namespace the `IDNAEncoder` test class This commit: * Namespaces the class. * Renames the `RequestsTest_IDNAEncoder` class to `IDNAEncoderTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{IDNAEncoder.php => IDNAEncoderTest.php} | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) rename tests/{IDNAEncoder.php => IDNAEncoderTest.php} (96%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 692afaabd..5a0a418ef 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/IDNAEncoder.php tests/IRI.php tests/Requests.php tests/Response/Headers.php diff --git a/tests/IDNAEncoder.php b/tests/IDNAEncoderTest.php similarity index 96% rename from tests/IDNAEncoder.php rename to tests/IDNAEncoderTest.php index 0c8efe010..b0bd9647d 100644 --- a/tests/IDNAEncoder.php +++ b/tests/IDNAEncoderTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 20:43:49 +0200 Subject: [PATCH 08/19] Tests: namespace the `IRI` test class This commit: * Namespaces the class. * Renames the `RequestsTest_IRI` class to `IRITest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. * Adjusts the reference to the file in the PHPCS config file. --- .phpcs.xml.dist | 2 +- phpunit.xml.dist | 1 - tests/{IRI.php => IRITest.php} | 5 ++++- 3 files changed, 5 insertions(+), 3 deletions(-) rename tests/{IRI.php => IRITest.php} (99%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 23ccb1c35..bca2cd08f 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -17,7 +17,7 @@ */library/Requests/IRI\.php$ - */tests/IRI\.php$ + */tests/IRITest\.php$ */vendor/* */tests/coverage/ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5a0a418ef..6bca16300 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/IRI.php tests/Requests.php tests/Response/Headers.php tests/Session.php diff --git a/tests/IRI.php b/tests/IRITest.php similarity index 99% rename from tests/IRI.php rename to tests/IRITest.php index 896e07869..a0662929f 100644 --- a/tests/IRI.php +++ b/tests/IRITest.php @@ -40,9 +40,12 @@ * */ +namespace Requests\Tests; + +use Requests_IRI; use Requests\Tests\TestCase; -class RequestsTest_IRI extends TestCase +class IRITest extends TestCase { public static function rfc3986_tests() { From 8c13f7926126998ecb6ac1f8bbdf829b6d62ae74 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 20:45:10 +0200 Subject: [PATCH 09/19] Tests: namespace the `Requests` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Requests` class to `RequestsTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{Requests.php => RequestsTest.php} | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) rename tests/{Requests.php => RequestsTest.php} (96%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6bca16300..49e1eb3e2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/Requests.php tests/Response/Headers.php tests/Session.php tests/SSL.php diff --git a/tests/Requests.php b/tests/RequestsTest.php similarity index 96% rename from tests/Requests.php rename to tests/RequestsTest.php index bebe80490..1c3cf4027 100644 --- a/tests/Requests.php +++ b/tests/RequestsTest.php @@ -1,8 +1,14 @@ expectException('Requests_Exception'); From d4da1cfae3da53a1a409dfc5991cdf6fc161cf71 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 20:46:19 +0200 Subject: [PATCH 10/19] Tests: namespace the `Session` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Session` class to `SessionTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{Session.php => SessionTest.php} | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) rename tests/{Session.php => SessionTest.php} (98%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 49e1eb3e2..79b8b62d8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,7 +19,6 @@ tests/Response/Headers.php - tests/Session.php tests/SSL.php tests/Utility/FilteredIterator.php diff --git a/tests/Session.php b/tests/SessionTest.php similarity index 98% rename from tests/Session.php rename to tests/SessionTest.php index 0e96ff927..448c2e248 100644 --- a/tests/Session.php +++ b/tests/SessionTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 20:47:04 +0200 Subject: [PATCH 11/19] Tests: namespace the `SSL` test class This commit: * Namespaces the class. * Renames the `RequestsTest_SSL` class to `SSLTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/{SSL.php => SSLTest.php} | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) rename tests/{SSL.php => SSLTest.php} (97%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 79b8b62d8..3635de363 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,7 +19,6 @@ tests/Response/Headers.php - tests/SSL.php tests/Utility/FilteredIterator.php diff --git a/tests/SSL.php b/tests/SSLTest.php similarity index 97% rename from tests/SSL.php rename to tests/SSLTest.php index edf586301..7f3c462cd 100644 --- a/tests/SSL.php +++ b/tests/SSLTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 20:50:37 +0200 Subject: [PATCH 12/19] Tests: namespace the `Response_Headers` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Response_Headers` class to `HeadersTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file. --- phpunit.xml.dist | 1 - tests/Response/{Headers.php => HeadersTest.php} | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) rename tests/Response/{Headers.php => HeadersTest.php} (93%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3635de363..1ab03f849 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ tests/Proxy - tests/Response/Headers.php tests/Utility/FilteredIterator.php diff --git a/tests/Response/Headers.php b/tests/Response/HeadersTest.php similarity index 93% rename from tests/Response/Headers.php rename to tests/Response/HeadersTest.php index 6aa8bb1a9..34224428f 100644 --- a/tests/Response/Headers.php +++ b/tests/Response/HeadersTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 20:51:29 +0200 Subject: [PATCH 13/19] Tests: namespace the `Utility_FilteredIterator` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Utility_FilteredIterator` class to `FilteredIteratorTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the named reference to the old test class file from the PHPUnit config file and removes the now empty `testsuite` "General". --- phpunit.xml.dist | 3 --- .../{FilteredIterator.php => FilteredIteratorTest.php} | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) rename tests/Utility/{FilteredIterator.php => FilteredIteratorTest.php} (86%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1ab03f849..b39b9b042 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,9 +17,6 @@ tests/Proxy - - tests/Utility/FilteredIterator.php - tests diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIteratorTest.php similarity index 86% rename from tests/Utility/FilteredIterator.php rename to tests/Utility/FilteredIteratorTest.php index 5bfd81bed..5931a33bd 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIteratorTest.php @@ -1,8 +1,13 @@ Date: Tue, 29 Jun 2021 20:48:18 +0200 Subject: [PATCH 14/19] Tests: namespace the `Auth_Basic` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Auth_Basic` class to `BasicTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the testsuite which previously contained the old test class file from the PHPUnit config file in favour of letting the generic testsuite handle this. --- phpunit.xml.dist | 3 --- tests/Auth/{Basic.php => BasicTest.php} | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) rename tests/Auth/{Basic.php => BasicTest.php} (95%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b39b9b042..68371e17c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,9 +8,6 @@ verbose="true" > - - tests/Auth - tests/Transport diff --git a/tests/Auth/Basic.php b/tests/Auth/BasicTest.php similarity index 95% rename from tests/Auth/Basic.php rename to tests/Auth/BasicTest.php index 0b0c738b4..d7bbdcb96 100644 --- a/tests/Auth/Basic.php +++ b/tests/Auth/BasicTest.php @@ -1,8 +1,12 @@ Date: Tue, 29 Jun 2021 20:49:45 +0200 Subject: [PATCH 15/19] Tests: namespace the `Proxy_HTTP` test class This commit: * Namespaces the class. * Renames the `RequestsTest_Proxy_HTTP` class to `HTTPTest`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Removes the testsuite which previously contained the old test class file from the PHPUnit config file in favour of letting the generic testsuite handle this. --- phpunit.xml.dist | 3 --- tests/Proxy/{HTTP.php => HTTPTest.php} | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) rename tests/Proxy/{HTTP.php => HTTPTest.php} (97%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 68371e17c..773320d3f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,9 +11,6 @@ tests/Transport - - tests/Proxy - tests diff --git a/tests/Proxy/HTTP.php b/tests/Proxy/HTTPTest.php similarity index 97% rename from tests/Proxy/HTTP.php rename to tests/Proxy/HTTPTest.php index b63b91dd2..2a142057d 100644 --- a/tests/Proxy/HTTP.php +++ b/tests/Proxy/HTTPTest.php @@ -1,8 +1,12 @@ Date: Tue, 29 Jun 2021 22:11:02 +0200 Subject: [PATCH 16/19] Tests: namespace the `Transport_*` test classes This commit: * Namespaces the classes. * Renames the `RequestsTest_Transport_cURL` class to `CurlTest`. * Renames the `RequestsTest_Transport_fsockopen` class to `FsockopenTest`. * Renames the class files to match the new class names. * Add `use` statements for all used classes. * Removes the testsuite which previously contained the old test class files from the PHPUnit config file in favour of letting the generic testsuite handle this. --- phpunit.xml.dist | 3 --- tests/Transport/{cURL.php => CURLTest.php} | 5 ++++- tests/Transport/{fsockopen.php => FsockopenTest.php} | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) rename tests/Transport/{cURL.php => CURLTest.php} (97%) rename tests/Transport/{fsockopen.php => FsockopenTest.php} (92%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 773320d3f..d14f496a5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,9 +8,6 @@ verbose="true" > - - tests/Transport - tests diff --git a/tests/Transport/cURL.php b/tests/Transport/CURLTest.php similarity index 97% rename from tests/Transport/cURL.php rename to tests/Transport/CURLTest.php index 701883012..13567dd6f 100644 --- a/tests/Transport/cURL.php +++ b/tests/Transport/CURLTest.php @@ -1,8 +1,11 @@ Date: Tue, 29 Jun 2021 22:17:48 +0200 Subject: [PATCH 17/19] Tests: namespace the `Transport` mock class This commit: * Namespaces the class. * Renames the `RequestsTest_Mock_Transport` class to `TransportMock`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Adjusts all references to this Mock class in other test files. --- tests/ChunkedEncodingTest.php | 8 ++++---- tests/Mock/{Transport.php => TransportMock.php} | 6 +++++- tests/RequestsTest.php | 4 ++-- tests/Transport/BaseTestCase.php | 12 ++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) rename tests/Mock/{Transport.php => TransportMock.php} (96%) diff --git a/tests/ChunkedEncodingTest.php b/tests/ChunkedEncodingTest.php index 4465aa112..f890c8b06 100644 --- a/tests/ChunkedEncodingTest.php +++ b/tests/ChunkedEncodingTest.php @@ -3,8 +3,8 @@ namespace Requests\Tests; use Requests; +use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; -use RequestsTest_Mock_Transport; class ChunkedDecodingTest extends TestCase { public static function chunkedProvider() { @@ -40,7 +40,7 @@ public static function chunkedProvider() { * @dataProvider chunkedProvider */ public function testChunked($body, $expected) { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->body = $body; $transport->chunked = true; @@ -68,7 +68,7 @@ public static function notChunkedProvider() { * @dataProvider notChunkedProvider */ public function testNotActuallyChunked($body) { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->body = $body; $transport->chunked = true; @@ -86,7 +86,7 @@ public function testNotActuallyChunked($body) { * that they're lying to us */ public function testMixedChunkiness() { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->body = "02\r\nab\r\nNot actually chunked!"; $transport->chunked = true; diff --git a/tests/Mock/Transport.php b/tests/Mock/TransportMock.php similarity index 96% rename from tests/Mock/Transport.php rename to tests/Mock/TransportMock.php index 42580fab2..a0af0a186 100644 --- a/tests/Mock/Transport.php +++ b/tests/Mock/TransportMock.php @@ -1,6 +1,10 @@ code = 302; $options = array( diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index bfbfbfca2..81ecd2679 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -3,11 +3,11 @@ namespace Requests\Tests\Transport; use Requests; +use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Exception; use Requests_Hooks; use Requests_Response; -use RequestsTest_Mock_Transport; abstract class BaseTestCase extends TestCase { public function set_up() { @@ -392,7 +392,7 @@ public static function statusCodeSuccessProvider() { * @dataProvider statusCodeSuccessProvider */ public function testStatusCode($code, $success) { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->code = $code; $url = sprintf(httpbin('/status/%d'), $code); @@ -410,7 +410,7 @@ public function testStatusCode($code, $success) { * @dataProvider statusCodeSuccessProvider */ public function testStatusCodeThrow($code, $success) { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->code = $code; $url = sprintf(httpbin('/status/%d'), $code); @@ -440,7 +440,7 @@ public function testStatusCodeThrow($code, $success) { * @dataProvider statusCodeSuccessProvider */ public function testStatusCodeThrowAllowRedirects($code, $success) { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->code = $code; $url = sprintf(httpbin('/status/%d'), $code); @@ -464,7 +464,7 @@ public function testStatusCodeThrowAllowRedirects($code, $success) { } public function testStatusCodeUnknown() { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->code = 599; $options = array( @@ -477,7 +477,7 @@ public function testStatusCodeUnknown() { } public function testStatusCodeThrowUnknown() { - $transport = new RequestsTest_Mock_Transport(); + $transport = new TransportMock(); $transport->code = 599; $options = array( From d6a2232ea47c903f3d4ef6253358c77bad87e35f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 22:19:10 +0200 Subject: [PATCH 18/19] Tests: namespace the `RawTransport` mock class This commit: * Namespaces the class. * Renames the `RequestsTest_Mock_Transport` class to `TransportMock`. * Renames the class file to match the new class name. * Add `use` statements for all used classes. * Adjusts all references to this Mock class in other test files. --- .../{RawTransport.php => RawTransportMock.php} | 6 +++++- tests/RequestsTest.php | 16 ++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) rename tests/Mock/{RawTransport.php => RawTransportMock.php} (82%) diff --git a/tests/Mock/RawTransport.php b/tests/Mock/RawTransportMock.php similarity index 82% rename from tests/Mock/RawTransport.php rename to tests/Mock/RawTransportMock.php index 94dd91660..c807e01c9 100644 --- a/tests/Mock/RawTransport.php +++ b/tests/Mock/RawTransportMock.php @@ -1,6 +1,10 @@ data; diff --git a/tests/RequestsTest.php b/tests/RequestsTest.php index c26b6f380..0e7f36244 100644 --- a/tests/RequestsTest.php +++ b/tests/RequestsTest.php @@ -3,10 +3,10 @@ namespace Requests\Tests; use Requests; +use Requests\Tests\Mock\RawTransportMock; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Response_Headers; -use RequestsTest_Mock_RawTransport; class RequestsTest extends TestCase { @@ -25,7 +25,7 @@ public function testDefaultTransport() { * Standard response header parsing */ public function testHeaderParsing() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/1.0 200 OK\r\n" . "Host: localhost\r\n" . @@ -60,7 +60,7 @@ public function testHeaderParsing() { } public function testProtocolVersionParsing() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/1.0 200 OK\r\n" . "Host: localhost\r\n\r\n"; @@ -74,7 +74,7 @@ public function testProtocolVersionParsing() { } public function testRawAccess() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/1.0 200 OK\r\n" . "Host: localhost\r\n\r\n" . @@ -91,7 +91,7 @@ public function testRawAccess() { * Headers with only \n delimiting should be treated as if they're \r\n */ public function testHeaderOnlyLF() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/1.0 200 OK\r\nTest: value\nAnother-Test: value\r\n\r\n"; $options = array( @@ -109,7 +109,7 @@ public function testHeaderOnlyLF() { * new issue, and update your server/proxy to support a proper protocol. */ public function testInvalidProtocolVersion() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/0.9 200 OK\r\n\r\n

Test"; $options = array( @@ -125,7 +125,7 @@ public function testInvalidProtocolVersion() { * HTTP/0.9 also appears to use a single CRLF instead of two. */ public function testSingleCRLFSeparator() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/0.9 200 OK\r\n

Test"; $options = array( @@ -138,7 +138,7 @@ public function testSingleCRLFSeparator() { } public function testInvalidStatus() { - $transport = new RequestsTest_Mock_RawTransport(); + $transport = new RawTransportMock(); $transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest"; $options = array( From bb565e9145fa7dd044b601b4402c7907e1be5730 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 29 Jun 2021 22:27:33 +0200 Subject: [PATCH 19/19] Tests: finish off namespacing Now all test classes are namespaced, the old autoloader in the test bootstrap is no longer necessary. Additionally, as long as Composer knows about the namespaced test classes. the (new) custom autoloader is only really needed when PHPUnit is run via a PHAR file. To this end, this commit: * Adds a `autoload-dev` section to the `composer.json` configuration file. * Removes the old autoloader. * And places the "manual" loading of the PHPUnit polyfills autoloader and the custom autoloader in a condition, so they only get used when the tests are run via a PHAR file. --- composer.json | 5 ++++ tests/bootstrap.php | 58 +++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index 1b61196b2..00d7f0637 100644 --- a/composer.json +++ b/composer.json @@ -54,6 +54,11 @@ "Requests": "library/" } }, + "autoload-dev": { + "psr-4": { + "Requests\\Tests\\": "tests/" + } + }, "scripts": { "lint": [ "@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git" diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f6a2a17b1..ba540c175 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -36,44 +36,40 @@ function define_from_env($name, $default = false) { die(1); } -// Load the PHPUnit Polyfills autoloader. -require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; +if (defined('__PHPUNIT_PHAR__')) { + // Testing via a PHPUnit phar. -// New autoloader. -spl_autoload_register( - function ($class_name) { - // Only try & load our own classes. - if (stripos($class_name, 'Requests\\Tests\\') !== 0) { - return false; - } + // Load the PHPUnit Polyfills autoloader. + require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; - // Strip namespace prefix 'Requests\Tests\'. - $relative_class = substr($class_name, 15); - $file = realpath(__DIR__ . '/' . strtr($relative_class, '\\', '/') . '.php'); + /* + * Autoloader specifically for the test files. + * Fixes issues with PHPUnit not being able to find test classes being extended when running + * in a non-Composer context. + */ + spl_autoload_register( + function ($class_name) { + // Only try & load our own classes. + if (stripos($class_name, 'Requests\\Tests\\') !== 0) { + return false; + } - if (file_exists($file)) { - include_once $file; - } + // Strip namespace prefix 'Requests\Tests\'. + $relative_class = substr($class_name, 15); + $file = realpath(__DIR__ . '/' . strtr($relative_class, '\\', '/') . '.php'); - return true; - } -); - -// Old autoloader. -function autoload_tests($class) { - if (strpos($class, 'RequestsTest_') !== 0) { - return; - } + if (file_exists($file)) { + include_once $file; + } - $class = substr($class, 13); - $file = str_replace('_', '/', $class); - if (file_exists(__DIR__ . '/' . $file . '.php')) { - require_once __DIR__ . '/' . $file . '.php'; - } + return true; + } + ); +} else { + // Testing via a Composer setup. + require_once $vendor_dir . '/autoload.php'; } -spl_autoload_register('autoload_tests'); - function httpbin($suffix = '', $ssl = false) { $host = $ssl ? 'https://' . REQUESTS_TEST_HOST_HTTPS : 'http://' . REQUESTS_TEST_HOST_HTTP; return rtrim($host, '/') . '/' . ltrim($suffix, '/');