Skip to content

Commit

Permalink
Merge pull request #817 from WordPress/feature/upgrade-phpunit-polyfi…
Browse files Browse the repository at this point in the history
…lls-phpunit-10
  • Loading branch information
schlessera committed Jul 24, 2023
2 parents efb240e + 148c25e commit cb9da04
Show file tree
Hide file tree
Showing 61 changed files with 332 additions and 171 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -9,3 +9,4 @@ tests/ export-ignore
.phpcs.xml.dist export-ignore
phpdoc.dist.xml export-ignore
phpunit.xml.dist export-ignore
phpunit10.xml.dist export-ignore
12 changes: 9 additions & 3 deletions .github/workflows/quicktest.yml
Expand Up @@ -90,12 +90,18 @@ jobs:
- name: Access localhost on port 9002
run: curl -i http://localhost:9002

- name: Show PHPUnit version
run: vendor/bin/phpunit --version
- name: Grab PHPUnit version
id: phpunit_version
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT

- name: Run the unit tests
- name: Run the unit tests (PHPUnit < 10)
if: ${{ ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer test

- name: Run the unit tests (PHPUnit 10+)
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer test10

- name: Stop proxy server
continue-on-error: true
run: |
Expand Down
23 changes: 16 additions & 7 deletions .github/workflows/test.yml
Expand Up @@ -114,16 +114,25 @@ jobs:
- name: Access localhost on port 9002
run: curl -i http://localhost:9002

- name: Show PHPUnit version
run: vendor/bin/phpunit --version
- name: Grab PHPUnit version
id: phpunit_version
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> $GITHUB_OUTPUT

- name: Run the unit tests, no code coverage
if: ${{ matrix.coverage == false }}
- name: Run the unit tests, no code coverage (PHPUnit < 10)
if: ${{ matrix.coverage == false && ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer test

- name: Run the unit tests with code coverage
if: ${{ matrix.coverage == true }}
run: vendor/bin/phpunit --coverage-clover clover.xml
- name: Run the unit tests, no code coverage (PHPUnit 10+)
if: ${{ matrix.coverage == false && startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer test10

- name: Run the unit tests with code coverage (PHPUnit < 10)
if: ${{ matrix.coverage == true && ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer coverage -- --coverage-clover clover.xml

- name: Run the unit tests with code coverage (PHPUnit 10+)
if: ${{ matrix.coverage == true && startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
run: composer coverage10 -- --coverage-clover clover.xml

- name: Stop proxy server
continue-on-error: true
Expand Down
5 changes: 5 additions & 0 deletions .phpcs.xml.dist
Expand Up @@ -247,4 +247,9 @@
<exclude-pattern>/tests/*\.php$</exclude-pattern>
</rule>

<!-- Using set_error_handler() in test code is fine. -->
<rule ref="WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler">
<exclude-pattern>/tests/*\.php$</exclude-pattern>
</rule>

</ruleset>
17 changes: 16 additions & 1 deletion composer.json
Expand Up @@ -48,7 +48,7 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"php-parallel-lint/php-console-highlighter": "^1.0.0",
"yoast/phpunit-polyfills": "^1.0.0",
"yoast/phpunit-polyfills": "^2.0.0",
"roave/security-advisories": "dev-latest"
},
"suggest": {
Expand Down Expand Up @@ -84,8 +84,23 @@
"test": [
"@php ./vendor/phpunit/phpunit/phpunit --no-coverage"
],
"test10": [
"@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist --no-coverage"
],
"coverage": [
"@php ./vendor/phpunit/phpunit/phpunit"
],
"coverage10": [
"@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist"
]
},
"scripts-descriptions": {
"lint": "Lint PHP files to find parse errors.",
"checkcs": "Check the entire codebase for code-style issues.",
"fixcs": "Fix all auto-fixable code-style issues in the entire codebase.",
"test": "Run the unit tests on PHPUnit 5.x - 9.x without code coverage.",
"test10": "Run the unit tests on PHPUnit 10.x without code coverage.",
"coverage": "Run the unit tests on PHPUnit 5.x - 9.x with code coverage.",
"coverage10": "Run the unit tests on PHPUnit 10.x with code coverage."
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -4,6 +4,7 @@
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
backupGlobals="true"
bootstrap="tests/bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
convertDeprecationsToExceptions="true"
colors="true"
verbose="true"
Expand Down
36 changes: 36 additions & 0 deletions phpunit10.xml.dist
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/10.1/phpunit.xsd"
backupGlobals="true"
bootstrap="tests/bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
colors="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
failOnWarning="true"
failOnNotice="true"
failOnDeprecation="true"
>
<testsuites>
<testsuite name="RequestsTests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>

<coverage includeUncoveredFiles="true" ignoreDeprecatedCodeUnits="true">
<report>
<html outputDirectory="tests/coverage" lowUpperBound="35" highLowerBound="90"/>
</report>
</coverage>
</phpunit>
5 changes: 5 additions & 0 deletions src/Transport/Curl.php
Expand Up @@ -177,6 +177,11 @@ public function request($url, $headers = [], $data = [], $options = []) {
$this->stream_handle = @fopen($options['filename'], 'wb');
if ($this->stream_handle === false) {
$error = error_get_last();
if (!is_array($error)) {
// Shouldn't be possible, but can happen in test situations.
$error = ['message' => 'Failed to open stream'];
}

throw new Exception($error['message'], 'fopen');
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Transport/Fsockopen.php
Expand Up @@ -282,6 +282,11 @@ public function request($url, $headers = [], $data = [], $options = []) {
$download = @fopen($options['filename'], 'wb');
if ($download === false) {
$error = error_get_last();
if (!is_array($error)) {
// Shouldn't be possible, but can happen in test situations.
$error = ['message' => 'Failed to open stream'];
}

throw new Exception($error['message'], 'fopen');
}
}
Expand Down
18 changes: 9 additions & 9 deletions tests/Auth/Basic/BasicTest.php
Expand Up @@ -44,14 +44,14 @@ public function testUsingArray($transport) {
$result = json_decode($request->body);
$this->assertIsObject($result, 'Decoded response body is not an object');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'authenticated',
$result,
'Property "authenticated" not available in decoded response'
);
$this->assertTrue($result->authenticated, 'Authentication failed');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'user',
$result,
'Property "user" not available in decoded response'
Expand Down Expand Up @@ -91,14 +91,14 @@ public function testUsingInstantiation($transport) {
$result = json_decode($request->body);
$this->assertIsObject($result, 'Decoded response body is not an object');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'authenticated',
$result,
'Property "authenticated" not available in decoded response'
);
$this->assertTrue($result->authenticated, 'Authentication failed');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'user',
$result,
'Property "user" not available in decoded response'
Expand Down Expand Up @@ -141,14 +141,14 @@ public function testUsingInstantiationWithDelayedSettingOfCredentials($transport
$result = json_decode($request->body);
$this->assertIsObject($result, 'Decoded response body is not an object');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'authenticated',
$result,
'Property "authenticated" not available in decoded response'
);
$this->assertTrue($result->authenticated, 'Authentication failed');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'user',
$result,
'Property "user" not available in decoded response'
Expand Down Expand Up @@ -189,12 +189,12 @@ public function testPOSTUsingInstantiation($transport) {
$result = json_decode($request->body);

$this->assertIsObject($result, 'Decoded response body is not an object');
$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'headers',
$result,
'Property "headers" not available in decoded response'
);
$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'Authorization',
$result->headers,
'Property "headers->Authorization" not available in decoded response'
Expand All @@ -205,7 +205,7 @@ public function testPOSTUsingInstantiation($transport) {
$this->assertArrayHasKey(1, $auth, 'Authorization header failed to be split into two parts');
$this->assertSame(base64_encode('user:passwd'), $auth[1], 'Unexpected authorization string in headers');

$this->assertObjectHasAttribute(
$this->assertObjectHasProperty(
'data',
$result,
'Property "data" not available in decoded response'
Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/Basic/ConstructorTest.php
Expand Up @@ -34,7 +34,7 @@ public function testInvalidInputType($input) {
*
* @return array
*/
public function dataInvalidInputType() {
public static function dataInvalidInputType() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_NULL, TypeProviderHelper::GROUP_ARRAY);
}

Expand All @@ -59,7 +59,7 @@ public function testInvalidArgumentCount($input) {
*
* @return array
*/
public function dataInvalidArgumentCount() {
public static function dataInvalidArgumentCount() {
return [
'empty array' => [[]],
'array with only one element' => [['user']],
Expand Down
29 changes: 24 additions & 5 deletions tests/Autoload/AutoloadTest.php
Expand Up @@ -2,6 +2,7 @@

namespace WpOrg\Requests\Tests\Autoload;

use Exception;
use Requests;
use Requests_Exception_Transport_cURL;
use Requests_Utility_FilteredIterator;
Expand All @@ -21,8 +22,17 @@ final class AutoloadTest extends TestCase {
* Verify that a deprecation notice is thrown when the "old" Requests class is loaded via a require/include.
*/
public function testDeprecationNoticeThrownForOldRequestsClass() {
$this->expectDeprecation();
$this->expectDeprecationMessage(self::MSG);
// PHPUnit 10 compatible way to test the deprecation notice.
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

$this->expectException(Exception::class);
$this->expectExceptionMessage(self::MSG);

require_once dirname(dirname(__DIR__)) . '/library/Requests.php';
}
Expand All @@ -31,8 +41,17 @@ public function testDeprecationNoticeThrownForOldRequestsClass() {
* Verify that a deprecation notice is thrown when one of the other "old" Requests classes is autoloaded.
*/
public function testDeprecationNoticeThrownForOtherOldRequestsClass() {
$this->expectDeprecation();
$this->expectDeprecationMessage(self::MSG);
// PHPUnit 10 compatible way to test the deprecation notice.
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

$this->expectException(Exception::class);
$this->expectExceptionMessage(self::MSG);

$this->assertNotEmpty(Requests_Exception_Transport_cURL::EASY);
}
Expand Down Expand Up @@ -91,7 +110,7 @@ public function testLoad($class_name, $expected) {
*
* @return array
*/
public function dataLoad() {
public static function dataLoad() {
return [
'Request for class not in this package should be rejected' => [
'class_name' => 'Unrelated\Package\ClassName',
Expand Down
16 changes: 8 additions & 8 deletions tests/Cookie/ConstructorTest.php
Expand Up @@ -50,7 +50,7 @@ public function testInvalidValue($input) {
*
* @return array
*/
public function dataInvalidStringInput() {
public static function dataInvalidStringInput() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}

Expand All @@ -75,7 +75,7 @@ public function testInvalidAttributes($input) {
*
* @return array
*/
public function dataInvalidAttributes() {
public static function dataInvalidAttributes() {
$except = array_intersect(TypeProviderHelper::GROUP_ITERABLE, TypeProviderHelper::GROUP_ARRAY_ACCESSIBLE);
return TypeProviderHelper::getAllExcept($except);
}
Expand All @@ -101,7 +101,7 @@ public function testInvalidFlags($input) {
*
* @return array
*/
public function dataInvalidFlags() {
public static function dataInvalidFlags() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_ARRAY);
}

Expand All @@ -126,7 +126,7 @@ public function testInvalidReferenceTime($input) {
*
* @return array
*/
public function dataInvalidReferenceTime() {
public static function dataInvalidReferenceTime() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_NULL, TypeProviderHelper::GROUP_INT);
}

Expand All @@ -152,7 +152,7 @@ public function testMinimalArguments($name, $value) {
*
* @return array
*/
public function dataMinimalArguments() {
public static function dataMinimalArguments() {
return [
'empty name and value' => [
'name' => '',
Expand Down Expand Up @@ -199,7 +199,7 @@ public function testFlagMerging($flags, $expected) {
*
* @return array
*/
public function dataFlagMerging() {
public static function dataFlagMerging() {
return [
'empty array' => [
'flags' => [],
Expand Down Expand Up @@ -269,7 +269,7 @@ public function testSetReferenceTime($time, $expected = null) {
*
* @return array
*/
public function dataSetReferenceTime() {
public static function dataSetReferenceTime() {
return [
'null' => [
'time' => null,
Expand Down Expand Up @@ -302,7 +302,7 @@ public function testAttributesAreNormalized($attributes, $expected) {
*
* @return array
*/
public function dataAttributesAreNormalized() {
public static function dataAttributesAreNormalized() {
return [
'empty array' => [
'attributes' => [],
Expand Down

0 comments on commit cb9da04

Please sign in to comment.