From cd825550b21a4bf581441ad4fa3b4b50e48fb257 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Mon, 26 Jul 2021 11:16:24 -0400 Subject: [PATCH] Fix Curl::downloadFileName not being correctly set --- src/Curl/Curl.php | 2 +- src/Curl/MultiCurl.php | 2 +- tests/PHPCurlClass/PHPCurlClassTest.php | 30 +++++++++++++++----- tests/PHPCurlClass/PHPMultiCurlClassTest.php | 13 +++++---- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 3fe29ec..21dea01 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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); diff --git a/src/Curl/MultiCurl.php b/src/Curl/MultiCurl.php index ee0e42b..06d47e8 100755 --- a/src/Curl/MultiCurl.php +++ b/src/Curl/MultiCurl.php @@ -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)) { diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 132a2ab..33d5a8f 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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,29 +684,28 @@ 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); unlink($upload_file_path); $this->assertFalse(file_exists($upload_file_path)); - $this->assertFalse(file_exists($upload_file_path)); } public function testDownloadRange() @@ -825,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 c302c92..6811334 100644 --- a/tests/PHPCurlClass/PHPMultiCurlClassTest.php +++ b/tests/PHPCurlClass/PHPMultiCurlClassTest.php @@ -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); @@ -2595,7 +2598,7 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase public function testDownloadCallback() { - // Upload a file. + // Create and upload a file. $upload_file_path = \Helper\get_png(); $uploaded_file_path = \Helper\upload_file_to_server($upload_file_path); @@ -2623,10 +2626,8 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase unlink($upload_file_path); $this->assertFalse(file_exists($upload_file_path)); - $this->assertFalse(file_exists($uploaded_file_path)); } - public function testDownloadRange() { // Create and upload a file. @@ -2749,7 +2750,7 @@ 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();