diff --git a/src/Curl/ArrayUtil.php b/src/Curl/ArrayUtil.php index 61834de..00277b2 100644 --- a/src/Curl/ArrayUtil.php +++ b/src/Curl/ArrayUtil.php @@ -1,4 +1,4 @@ -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); diff --git a/src/Curl/Decoder.php b/src/Curl/Decoder.php index 3170811..603f4c3 100644 --- a/src/Curl/Decoder.php +++ b/src/Curl/Decoder.php @@ -1,4 +1,4 @@ -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); diff --git a/src/Curl/StringUtil.php b/src/Curl/StringUtil.php index e5ca0e9..86341f9 100644 --- a/src/Curl/StringUtil.php +++ b/src/Curl/StringUtil.php @@ -1,4 +1,4 @@ - (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'] !== '') { diff --git a/tests/PHPCurlClass/ArrayUtilTest.php b/tests/PHPCurlClass/ArrayUtilTest.php index 0f15395..95dc6a8 100644 --- a/tests/PHPCurlClass/ArrayUtilTest.php +++ b/tests/PHPCurlClass/ArrayUtilTest.php @@ -1,4 +1,4 @@ -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 = [ diff --git a/tests/PHPCurlClass/PHPMultiCurlClassTest.php b/tests/PHPCurlClass/PHPMultiCurlClassTest.php index 8e60d2d..82c9fc3 100644 --- a/tests/PHPCurlClass/PHPMultiCurlClassTest.php +++ b/tests/PHPCurlClass/PHPMultiCurlClassTest.php @@ -1,4 +1,4 @@ -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() diff --git a/tests/PHPCurlClass/RangeHeader.php b/tests/PHPCurlClass/RangeHeader.php index 380d2c5..31d153f 100644 --- a/tests/PHPCurlClass/RangeHeader.php +++ b/tests/PHPCurlClass/RangeHeader.php @@ -1,4 +1,4 @@ -