From 7865cc4e52f4efc68bd35cb71223cc3014960bb4 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sat, 30 Oct 2021 11:29:18 +0100 Subject: [PATCH] Abstract guzzle client for testing --- CRM/Extension/Browser.php | 21 ++++++++++++++++++++- CRM/Extension/Downloader.php | 22 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CRM/Extension/Browser.php b/CRM/Extension/Browser.php index b808ce4b2c..42fe8fa574 100644 --- a/CRM/Extension/Browser.php +++ b/CRM/Extension/Browser.php @@ -40,6 +40,25 @@ class CRM_Extension_Browser { // timeout for when the connection or the server is slow const CHECK_TIMEOUT = 5; + /** + * @var GuzzleHttp\Client + */ + protected $guzzleClient; + + /** + * @return \GuzzleHttp\Client + */ + public function getGuzzleClient(): \GuzzleHttp\Client { + return $this->guzzleClient ?? new \GuzzleHttp\Client(); + } + + /** + * @param \GuzzleHttp\Client $guzzleClient + */ + public function setGuzzleClient(\GuzzleHttp\Client $guzzleClient) { + $this->guzzleClient = $guzzleClient; + } + /** * @param string $repoUrl * URL of the remote repository. @@ -230,7 +249,7 @@ class CRM_Extension_Browser { $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE . '.' . md5($this->getRepositoryUrl()); $url = $this->getRepositoryUrl() . $this->indexPath; - $client = new GuzzleHttp\Client(); + $client = $this->getGuzzleClient(); $response = $client->request('GET', $url, ['sink' => $filename, 'timeout' => \Civi::settings()->get('http_timeout')]); restore_error_handler(); diff --git a/CRM/Extension/Downloader.php b/CRM/Extension/Downloader.php index c1c31c604d..4b472267af 100644 --- a/CRM/Extension/Downloader.php +++ b/CRM/Extension/Downloader.php @@ -16,6 +16,26 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ class CRM_Extension_Downloader { + + /** + * @var GuzzleHttp\Client + */ + protected $guzzleClient; + + /** + * @return \GuzzleHttp\Client + */ + public function getGuzzleClient(): \GuzzleHttp\Client { + return $this->guzzleClient ?? new \GuzzleHttp\Client(); + } + + /** + * @param \GuzzleHttp\Client $guzzleClient + */ + public function setGuzzleClient(\GuzzleHttp\Client $guzzleClient) { + $this->guzzleClient = $guzzleClient; + } + /** * @var CRM_Extension_Container_Basic * The place where downloaded extensions are ultimately stored @@ -136,7 +156,7 @@ class CRM_Extension_Downloader { * Whether the download was successful. */ public function fetch($remoteFile, $localFile) { - $client = new GuzzleHttp\Client(); + $client = $this->getGuzzleClient(); $response = $client->request('GET', $remoteFile, ['sink' => $localFile, 'timeout' => \Civi::settings()->get('http_timeout')]); if ($response->getStatusCode() === 200) { return TRUE; -- 2.25.1