From 3b6f287b21073408531168d1c398b26fddd8df93 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Mar 2013 16:32:22 -0400 Subject: [PATCH] CRM_Utils_HttpClient - Change static method (in preparation for using mock clients in testing) --- CRM/Extension/Downloader.php | 2 +- CRM/Utils/HttpClient.php | 14 +++++++++++++- tests/phpunit/CRM/Utils/HttpClientTest.php | 16 +++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CRM/Extension/Downloader.php b/CRM/Extension/Downloader.php index 598e062bf3..d883045f36 100644 --- a/CRM/Extension/Downloader.php +++ b/CRM/Extension/Downloader.php @@ -129,7 +129,7 @@ class CRM_Extension_Downloader { * @return boolean Whether the download was successful. */ public function fetch($remoteFile, $localFile) { - $result = CRM_Utils_HttpClient::fetch($remoteFile, $localFile); + $result = CRM_Utils_HttpClient::singleton()->fetch($remoteFile, $localFile); switch ($result) { case CRM_Utils_HttpClient::STATUS_OK: return TRUE; diff --git a/CRM/Utils/HttpClient.php b/CRM/Utils/HttpClient.php index da84a25f2b..e76d563913 100644 --- a/CRM/Utils/HttpClient.php +++ b/CRM/Utils/HttpClient.php @@ -39,6 +39,18 @@ class CRM_Utils_HttpClient { const STATUS_WRITE_ERROR = 'write-error'; const STATUS_DL_ERROR = 'dl-error'; + /** + * @var CRM_Utils_HttpClient + */ + protected static $singleton; + + public static function singleton() { + if (!self::$singleton) { + self::$singleton = new CRM_Utils_HttpClient(); + } + return self::$singleton; + } + /** * Download the remote zipfile. * @@ -46,7 +58,7 @@ class CRM_Utils_HttpClient { * @param string $localFile path at which to store the .zip file * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR */ - public static function fetch($remoteFile, $localFile) { + public function fetch($remoteFile, $localFile) { require_once 'CA/Config/Curl.php'; $caConfig = CA_Config_Curl::probe(array( 'verify_peer' => (bool) CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, TRUE) diff --git a/tests/phpunit/CRM/Utils/HttpClientTest.php b/tests/phpunit/CRM/Utils/HttpClientTest.php index 323022f051..c698fbb3ca 100644 --- a/tests/phpunit/CRM/Utils/HttpClientTest.php +++ b/tests/phpunit/CRM/Utils/HttpClientTest.php @@ -14,6 +14,11 @@ class CRM_Utils_HttpClientTest extends CiviUnitTestCase { */ protected $tmpFile; + /** + * @var CRM_Utils_HttpClient + */ + protected $client; + public function setUp() { parent::setUp(); @@ -24,6 +29,7 @@ class CRM_Utils_HttpClientTest extends CiviUnitTestCase { 'verifySSL' => TRUE, )); $this->assertAPISuccess($result); + $this->client = new CRM_Utils_HttpClient(); } public function tearDown() { @@ -33,19 +39,19 @@ class CRM_Utils_HttpClientTest extends CiviUnitTestCase { } function testFetchHttp() { - $result = CRM_Utils_HttpClient::fetch(self::VALID_HTTP_URL, $this->tmpFile); + $result = $this->client->fetch(self::VALID_HTTP_URL, $this->tmpFile); $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); $this->assertRegExp(self::VALID_HTTP_REGEX, file_get_contents($this->tmpFile)); } function testFetchHttps_valid() { - $result = CRM_Utils_HttpClient::fetch(self::VALID_HTTPS_URL, $this->tmpFile); + $result = $this->client->fetch(self::VALID_HTTPS_URL, $this->tmpFile); $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); $this->assertRegExp(self::VALID_HTTPS_REGEX, file_get_contents($this->tmpFile)); } function testFetchHttps_invalid_verify() { - $result = CRM_Utils_HttpClient::fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); + $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $result); $this->assertEquals('', file_get_contents($this->tmpFile)); } @@ -57,13 +63,13 @@ class CRM_Utils_HttpClientTest extends CiviUnitTestCase { )); $this->assertAPISuccess($result); - $result = CRM_Utils_HttpClient::fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); + $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile); $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result); $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, file_get_contents($this->tmpFile)); } function testFetchHttp_badOutFile() { - $result = CRM_Utils_HttpClient::fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput'); + $result = $this->client->fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput'); $this->assertEquals(CRM_Utils_HttpClient::STATUS_WRITE_ERROR, $result); } -- 2.25.1