CRM_Utils_HttpClient - Change static method (in preparation for using mock clients...
authorTim Otten <totten@civicrm.org>
Fri, 29 Mar 2013 20:32:22 +0000 (16:32 -0400)
committerTim Otten <totten@civicrm.org>
Fri, 29 Mar 2013 23:34:13 +0000 (19:34 -0400)
CRM/Extension/Downloader.php
CRM/Utils/HttpClient.php
tests/phpunit/CRM/Utils/HttpClientTest.php

index 598e062bf3bd63e902e709be606c942649f262b5..d883045f36e261844c8b4214b7b855c15b0133a8 100644 (file)
@@ -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;
index da84a25f2b38eb6a104030ca61eb0d607c43371e..e76d563913a1b2fc2bc652cea62820d81331473c 100644 (file)
@@ -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)
index 323022f0512eb9d7d77d281f5f7beceb9a02b8e1..c698fbb3ca5f011272a04f33ba41888ad30f252c 100644 (file)
@@ -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);
   }