Skip to content

Commit fd902b4

Browse files
authored
Fix CI (PHPUnit) (php-curl-class#918)
* Add shims for PHPUnit * Fix PHPUnit error: undefined method returnValue() 1) CurlTest\PHPCurlClassTest::testMock Error: Call to undefined method CurlTest\PHPCurlClassTest::returnValue() * Fix PHPUnit risky test by restoring error handler before exception thrown There was 1 risky test: 1) CurlTest\PHPCurlClassTest::testRequiredOptionCurlOptReturnTransferEmitsWarningPHPUnit10Plus Test code or tested code did not remove its own error handlers * Fix psalm errors * Add attributes for version constraint * Temporarily pin psalm to fix ci
1 parent 2386b2a commit fd902b4

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"phpstan/phpstan": "*",
3131
"phpunit/phpunit": "*",
3232
"squizlabs/php_codesniffer": "*",
33-
"vimeo/psalm": ">=5.26.1"
33+
"vimeo/psalm": ">=5.26.1,<6.7"
3434
},
3535
"suggest": {
3636
"ext-mbstring": "*"

src/Curl/Curl.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public function download($url, $mixed_filename)
328328
// Attempt to resume download only when a temporary download file exists and is not empty.
329329
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
330330
$first_byte_position = $filesize;
331-
$range = $first_byte_position . '-';
331+
$range = (string)$first_byte_position . '-';
332332
$this->setRange($range);
333333
$this->fileHandle = fopen($download_filename, 'ab');
334334
} else {
@@ -385,7 +385,7 @@ public function fastDownload($url, $filename, $connections = 4)
385385
}
386386

387387
// Divide chunk_size across the number of connections.
388-
$chunk_size = ceil($content_length / $connections);
388+
$chunk_size = (int)ceil($content_length / $connections);
389389

390390
// Keep track of file name parts.
391391
$part_file_names = [];
@@ -399,9 +399,9 @@ public function fastDownload($url, $filename, $connections = 4)
399399
if ($part_number === $connections) {
400400
$range_end = '';
401401
}
402-
$range = $range_start . '-' . $range_end;
402+
$range = (string)$range_start . '-' . (string)$range_end;
403403

404-
$part_file_name = $filename . '.part' . $part_number;
404+
$part_file_name = $filename . '.part' . (string)$part_number;
405405

406406
// Save the file name of this part.
407407
$part_file_names[] = $part_file_name;
@@ -1294,27 +1294,27 @@ public function diagnose($return = false)
12941294
$response_headers_count = count($this->responseHeaders);
12951295

12961296
echo
1297-
'Request contained ' . $request_options_count . ' ' . (
1297+
'Request contained ' . (string)$request_options_count . ' ' . (
12981298
$request_options_count === 1 ? 'option:' : 'options:'
12991299
) . "\n";
13001300
if ($request_options_count) {
13011301
$i = 1;
13021302
foreach ($this->options as $option => $value) {
1303-
echo ' ' . $i . ' ';
1303+
echo ' ' . (string)$i . ' ';
13041304
$this->displayCurlOptionValue($option, $value);
13051305
$i += 1;
13061306
}
13071307
}
13081308

13091309
echo
13101310
'Sent an HTTP ' . $request_method . ' request to "' . $request_url . '".' . "\n" .
1311-
'Request contained ' . $request_headers_count . ' ' . (
1311+
'Request contained ' . (string)$request_headers_count . ' ' . (
13121312
$request_headers_count === 1 ? 'header:' : 'headers:'
13131313
) . "\n";
13141314
if ($request_headers_count) {
13151315
$i = 1;
13161316
foreach ($this->requestHeaders as $key => $value) {
1317-
echo ' ' . $i . ' ' . $key . ': ' . $value . "\n";
1317+
echo ' ' . (string)$i . ' ' . $key . ': ' . $value . "\n";
13181318
$i += 1;
13191319
}
13201320
}
@@ -1346,13 +1346,13 @@ public function diagnose($return = false)
13461346
}
13471347

13481348
echo
1349-
'Response contains ' . $response_headers_count . ' ' . (
1349+
'Response contains ' . (string)$response_headers_count . ' ' . (
13501350
$response_headers_count === 1 ? 'header:' : 'headers:'
13511351
) . "\n";
13521352
if ($this->responseHeaders !== null) {
13531353
$i = 1;
13541354
foreach ($this->responseHeaders as $key => $value) {
1355-
echo ' ' . $i . ' ' . $key . ': ' . $value . "\n";
1355+
echo ' ' . (string)$i . ' ' . $key . ': ' . $value . "\n";
13561356
$i += 1;
13571357
}
13581358
}
@@ -1409,12 +1409,13 @@ public function diagnose($return = false)
14091409
$messages_count = count($messages);
14101410
if ($messages_count) {
14111411
echo
1412-
'Found ' . $messages_count . ' ' . ($messages_count === 1 ? 'message' : 'messages') .
1412+
'Found ' . (string)$messages_count . ' ' .
1413+
($messages_count === 1 ? 'message' : 'messages') .
14131414
' in response:' . "\n";
14141415

14151416
$i = 1;
14161417
foreach ($messages as $message) {
1417-
echo ' ' . $i . ' ' . $message . "\n";
1418+
echo ' ' . (string)$i . ' ' . $message . "\n";
14181419
$i += 1;
14191420
}
14201421
}
@@ -1713,7 +1714,7 @@ public function displayCurlOptionValue($option, $value = null)
17131714
if (is_string($value)) {
17141715
echo ' "' . $value . '"' . "\n";
17151716
} elseif (is_int($value)) {
1716-
echo ' ' . $value;
1717+
echo ' ' . (string)$value;
17171718

17181719
$bit_flag_lookups = [
17191720
'CURLOPT_HTTPAUTH' => 'CURLAUTH_',

src/Curl/MultiCurl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function addDownload($url, $mixed_filename)
111111
// Attempt to resume download only when a temporary download file exists and is not empty.
112112
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
113113
$first_byte_position = $filesize;
114-
$range = $first_byte_position . '-';
114+
$range = (string)$first_byte_position . '-';
115115
$curl->setRange($range);
116116
$curl->fileHandle = fopen($download_filename, 'ab');
117117

@@ -587,7 +587,7 @@ public function setRateLimit($rate_limit)
587587
$interval_seconds = $interval * 3600;
588588
}
589589

590-
$this->rateLimit = $max_requests . '/' . $interval . $unit;
590+
$this->rateLimit = (string)$max_requests . '/' . (string)$interval . $unit;
591591
$this->rateLimitEnabled = true;
592592
$this->maxRequests = $max_requests;
593593
$this->interval = $interval;

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,33 +3186,21 @@ public function testCookieJarAfterClose()
31863186
$this->assertNotEmpty($cookies);
31873187
}
31883188

3189-
/**
3190-
* @requires PHPUnit < 10
3191-
* @expectedException \PHPUnit\Framework\Error\Warning
3192-
*/
3193-
public function testRequiredOptionCurlOptReturnTransferEmitsWarning()
3194-
{
3195-
$this->expectWarning(\PHPUnit\Framework\Error\Warning::class);
3196-
3197-
$curl = new Curl();
3198-
$curl->setOpt(CURLOPT_RETURNTRANSFER, false);
3199-
}
3200-
32013189
/**
32023190
* @requires PHPUnit >= 10
32033191
*/
3192+
#[RequiresPhpunit('>= 10')]
32043193
public function testRequiredOptionCurlOptReturnTransferEmitsWarningPHPUnit10Plus()
32053194
{
32063195
set_error_handler(static function (int $errno, string $errstr): never {
3196+
restore_error_handler();
32073197
throw new \Exception($errstr, $errno);
32083198
}, E_USER_WARNING);
32093199

32103200
$this->expectExceptionMessage('CURLOPT_RETURNTRANSFER is a required option');
32113201

32123202
$curl = new Curl();
32133203
$curl->setOpt(CURLOPT_RETURNTRANSFER, false);
3214-
3215-
restore_error_handler();
32163204
}
32173205

32183206
public function testRequestMethodSuccessiveGetRequests()
@@ -3966,7 +3954,7 @@ public function testMock()
39663954

39673955
$curl->expects($this->once())
39683956
->method('getRawResponse')
3969-
->will($this->returnValue('[]'));
3957+
->willReturn('[]');
39703958

39713959
$this->assertEquals('[]', $curl->getRawResponse());
39723960
}

tests/psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<!-- TODO: Use errorLevel="1" -->
33
<psalm
4+
ensureOverrideAttribute="false"
45
errorLevel="4"
56
resolveFromConfigFile="true"
67
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

tests/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ source "run_static_analysis_check_phpstan.sh"
3333

3434
source "run_static_analysis_check_psalm.sh"
3535

36+
set +x
37+
3638
source "display_errors.inc.sh"
3739

3840
if [[ "${CI_PHP_FUTURE_RELEASE}" != "true" ]]; then

tests/run_phpunit.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ phpunit_v10_shim() {
5757
remove_expectWarning
5858
}
5959

60+
phpunit_v11_shim() {
61+
:;
62+
}
63+
64+
phpunit_v12_shim() {
65+
remove_expectWarning
66+
}
67+
6068
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6169
cd "${SCRIPT_DIR}"
6270

@@ -101,6 +109,12 @@ elif [[ "${phpunit_version}" == "9."* ]]; then
101109
elif [[ "${phpunit_version}" == "10."* ]]; then
102110
phpunit_v10_shim
103111
phpunit_args=" --display-incomplete --display-skipped --display-deprecations --display-errors --display-notices --display-warnings --fail-on-risky ${extra_args}"
112+
elif [[ "${phpunit_version}" == "11."* ]]; then
113+
phpunit_v11_shim
114+
phpunit_args=" --display-incomplete --display-skipped --display-deprecations --display-errors --display-notices --display-warnings --fail-on-risky ${extra_args}"
115+
elif [[ "${phpunit_version}" == "12."* ]]; then
116+
phpunit_v12_shim
117+
phpunit_args=" --display-incomplete --display-skipped --display-deprecations --display-errors --display-notices --display-warnings --fail-on-risky ${extra_args}"
104118
fi
105119

106120
# Run tests.

0 commit comments

Comments
 (0)