Merge pull request #678 from zachborboa/master

Use strict types
This commit is contained in:
Zach Borboa
2021-07-26 21:28:15 -04:00
committed by GitHub
17 changed files with 93 additions and 80 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
+3 -3
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
@@ -1845,7 +1845,7 @@ class Curl
*/
private function downloadComplete($fh)
{
if ($this->error && is_file($this->downloadFileName)) {
if ($this->error && is_file((string) $this->downloadFileName)) {
@unlink($this->downloadFileName);
} elseif (!$this->error && $this->downloadCompleteCallback) {
rewind($fh);
@@ -1884,7 +1884,7 @@ class Curl
*/
private function parseHeaders($raw_headers)
{
$raw_headers = preg_split('/\r\n/', $raw_headers, null, PREG_SPLIT_NO_EMPTY);
$raw_headers = preg_split('/\r\n/', (string) $raw_headers, -1, PREG_SPLIT_NO_EMPTY);
$http_headers = new CaseInsensitiveArray();
$raw_headers_count = count($raw_headers);
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
+3 -3
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
@@ -117,7 +117,7 @@ class MultiCurl
// path. The download request will include header "Range: bytes=$filesize-" which is syntactically valid,
// but unsatisfiable.
$download_filename = $filename . '.pccdownload';
$this->downloadFileName = $download_filename;
$curl->downloadFileName = $download_filename;
// Attempt to resume download only when a temporary download file exists and is not empty.
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
@@ -1270,7 +1270,7 @@ class MultiCurl
$sleep_seconds = $sleep_until - microtime(true);
// Avoid using time_sleep_until() as it appears to be less precise and not sleep long enough.
usleep($sleep_seconds * 1000000);
usleep((int) $sleep_seconds * 1000000);
// Ensure that enough time has passed as usleep() may not have waited long enough.
$this->currentStartTime = microtime(true);
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
+2 -2
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Curl;
@@ -194,7 +194,7 @@ class Url
// $7 = <undefined> (query)
// $8 = #Related (ignore)
// $9 = Related (fragment)
preg_match('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/', $url, $output_array);
preg_match('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/', (string) $url, $output_array);
$parts = [];
if (isset($output_array['1']) && $output_array['1'] !== '') {
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace CurlTest;
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace ContentRangeServer;
+2 -2
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Helper;
@@ -103,7 +103,7 @@ function get_tmp_file_path()
// Return temporary file path without creating file.
$tmp_file_path =
rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) .
DIRECTORY_SEPARATOR . 'php-curl-class.' . uniqid(rand(), true);
DIRECTORY_SEPARATOR . 'php-curl-class.' . uniqid((string) rand(), true);
return $tmp_file_path;
}
+25 -8
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace CurlTest;
@@ -643,7 +643,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('OPTIONS', $test->curl->responseHeaders['X-REQUEST-METHOD']);
}
public function testDownload()
public function testDownloadToFile()
{
// Create and upload a file.
$upload_file_path = \Helper\get_png();
@@ -661,6 +661,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(filesize($upload_file_path), filesize($downloaded_file_path));
$this->assertEquals(md5_file($upload_file_path), md5_file($downloaded_file_path));
$this->assertEquals(md5_file($upload_file_path), $download_test->curl->responseHeaders['ETag']);
$this->assertEquals($download_test->curl->downloadFileName, $downloaded_file_path . '.pccdownload');
// Ensure successive requests set the appropriate values.
$this->assertEquals('GET', $download_test->server('request_method', 'GET'));
@@ -683,22 +684,22 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$uploaded_file_path = \Helper\upload_file_to_server($upload_file_path);
// Download the file.
$callback_called = false;
$download_callback_called = false;
$curl = new Curl();
$curl->setHeader('X-DEBUG-TEST', 'download_response');
$curl->download(Test::TEST_URL . '?' . http_build_query([
'file_path' => $uploaded_file_path,
]), function ($instance, $fh) use (&$callback_called) {
\PHPUnit\Framework\Assert::assertFalse($callback_called);
]), function ($instance, $fh) use (&$download_callback_called) {
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
$callback_called = true;
$download_callback_called = true;
});
$this->assertTrue($callback_called);
$this->assertTrue($download_callback_called);
// Remove server file.
\Helper\remove_file_from_server($uploaded_file_path);
@@ -824,10 +825,26 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$test->curl->setHeader('X-DEBUG-TEST', '404');
$test->curl->download(Test::TEST_URL, $destination);
$this->assertFalse(file_exists($test->curl->getDownloadFileName()));
$this->assertFalse(file_exists($test->curl->downloadFileName));
$this->assertFalse(file_exists($destination));
}
public function testDownloadCallbackError()
{
$download_before_send_called = false;
$download_callback_called = false;
$curl = new Curl();
$curl->beforeSend(function ($instance) use (&$download_before_send_called) {
\PHPUnit\Framework\Assert::assertFalse($download_before_send_called);
$download_before_send_called = true;
});
$curl->download(Test::ERROR_URL, function ($instance, $fh) use (&$download_callback_called) {
$download_callback_called = true;
});
$this->assertTrue($download_before_send_called);
$this->assertFalse($download_callback_called);
}
public function testMaxFilesize()
{
$tests = [
+45 -49
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace CurlTest;
@@ -2561,7 +2561,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$multi_curl->start();
}
public function testDownload()
public function testDownloadToFile()
{
// Create and upload a file.
$upload_file_path = \Helper\get_png();
@@ -2574,9 +2574,12 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
'file_path' => $uploaded_file_path,
]), $downloaded_file_path);
$multi_curl->complete(function ($instance) use ($upload_file_path) {
\PHPUnit\Framework\Assert::assertFalse($instance->error);
$multi_curl->complete(function ($instance) use ($upload_file_path, $downloaded_file_path) {
\PHPUnit\Framework\Assert::assertEquals(md5_file($upload_file_path), $instance->responseHeaders['ETag']);
\PHPUnit\Framework\Assert::assertEquals(
$instance->downloadFileName,
$downloaded_file_path . '.pccdownload'
);
});
$multi_curl->start();
$this->assertNotEquals($uploaded_file_path, $downloaded_file_path);
@@ -2593,6 +2596,38 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertFalse(file_exists($downloaded_file_path));
}
public function testDownloadCallback()
{
// Create and upload a file.
$upload_file_path = \Helper\get_png();
$uploaded_file_path = \Helper\upload_file_to_server($upload_file_path);
// Download the file.
$download_callback_called = false;
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'download_response');
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
'file_path' => $uploaded_file_path,
]), function ($instance, $fh) use (&$download_callback_called) {
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
$download_callback_called = true;
});
$multi_curl->start();
$this->assertTrue($download_callback_called);
// Remove server file.
\Helper\remove_file_from_server($uploaded_file_path);
unlink($upload_file_path);
$this->assertFalse(file_exists($upload_file_path));
}
public function testDownloadRange()
{
// Create and upload a file.
@@ -2715,51 +2750,12 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$multi_curl->setHeader('X-DEBUG-TEST', '404');
$multi_curl->addDownload(Test::TEST_URL, $destination);
$multi_curl->complete(function ($instance) use ($destination) {
\PHPUnit\Framework\Assert::assertFalse(file_exists($instance->getDownloadFileName()));
\PHPUnit\Framework\Assert::assertFalse(file_exists($instance->downloadFileName));
\PHPUnit\Framework\Assert::assertFalse(file_exists($destination));
});
$multi_curl->start();
}
public function testDownloadCallback()
{
// Upload a file.
$upload_file_path = \Helper\get_png();
$upload_test = new Test();
$upload_test->server('upload_response', 'POST', [
'image' => '@' . $upload_file_path,
]);
$uploaded_file_path = $upload_test->curl->response->file_path;
// Download the file.
$download_callback_called = false;
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'download_response');
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
'file_path' => $uploaded_file_path,
]), function ($instance, $fh) use (&$download_callback_called) {
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
$download_callback_called = true;
});
$multi_curl->start();
$this->assertTrue($download_callback_called);
// Remove server file.
$this->assertEquals('true', $upload_test->server('upload_cleanup', 'POST', [
'file_path' => $uploaded_file_path,
]));
unlink($upload_file_path);
$this->assertFalse(file_exists($upload_file_path));
$this->assertFalse(file_exists($uploaded_file_path));
}
public function testDownloadCallbackError()
{
$download_before_send_called = false;
@@ -2946,7 +2942,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$urls = [];
$copy_of_urls = [];
for ($i = 0; $i < 10; $i++) {
$url = Test::TEST_URL . '?' . md5(mt_rand());
$url = Test::TEST_URL . '?' . md5((string) mt_rand());
$urls[] = $url;
$copy_of_urls[] = $url;
}
@@ -3778,7 +3774,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
// Assert R4 ends around 11.
$this->assertGreaterThanOrEqual(10.8, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(11.5, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(11.5 + 1, $request_stats['4']['relative_stop']);
}
public function testSetRateLimitPerSecond2()
@@ -3847,7 +3843,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
// Assert R4 ends around 11.
$this->assertGreaterThanOrEqual(10.8, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(11.5, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(11.5 + 1, $request_stats['4']['relative_stop']);
}
public function testSetRateLimitPerSecond3()
@@ -4049,7 +4045,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
// Assert R4 ends around 12.
$this->assertGreaterThanOrEqual(11.8, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(12.5, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(12.5 + 1, $request_stats['4']['relative_stop']);
}
public function testSetRateLimitPerSecond6()
@@ -4117,7 +4113,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
// Assert R4 ends around 12.
$this->assertGreaterThanOrEqual(11.8, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(12.5, $request_stats['4']['relative_stop']);
$this->assertLessThanOrEqual(12.5 + 1, $request_stats['4']['relative_stop']);
}
public function testSetRateLimitPerSecond7()
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace RangeHeader;
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace CurlTest;
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Helper;
+3 -3
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
$server_start = microtime(true);
// Prevent direct access unless testing.
@@ -264,7 +264,7 @@ if ($test === 'http_basic_auth') {
exit;
} elseif ($test === 'download_file_size') {
$bytes = isset($_GET['bytes']) ? $_GET['bytes'] : 1234;
$str = str_repeat('.', $bytes);
$str = str_repeat('.', (int) $bytes);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($str));
header('ETag: ' . md5($str));
@@ -292,7 +292,7 @@ if ($test === 'http_basic_auth') {
$dots_to_print = floor($elapsed) - $dots_printed;
if ($dots_to_print) {
echo str_repeat('.', $dots_to_print);
echo str_repeat('.', (int) $dots_to_print);
$dots_printed += $dots_to_print;
}