Skip to content

Commit

Permalink
Adding mock responses for update command tests. (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash authored Jun 10, 2020
1 parent 03f2194 commit 65e5efa
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 47 deletions.
29 changes: 27 additions & 2 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Acquia\Cli\SelfUpdate\Strategy\GithubStrategy;
use Exception;
use GuzzleHttp\Client;
use Humbug\SelfUpdate\Updater;
use Phar;
use RuntimeException;
Expand All @@ -17,10 +18,15 @@
*/
class UpdateCommand extends CommandBase {

protected static $defaultName = 'self-update';

/** @var string */
protected $pharPath;

protected static $defaultName = 'self-update';
/**
* @var \GuzzleHttp\Client
*/
protected $client;

/**
* {inheritdoc}.
Expand All @@ -46,7 +52,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

$updater = new Updater($this->getPharPath(), FALSE);
$updater->setStrategyObject(new GithubStrategy());
$strategy = new GithubStrategy();
$updater->setStrategyObject($strategy);
$updater->getStrategy()->setClient($this->getClient());
$stability = $input->getOption('allow-unstable') !== FALSE ? GithubStrategy::UNSTABLE : GithubStrategy::STABLE;
$updater->getStrategy()->setStability($stability);
$updater->getStrategy()->setPackageName('acquia/cli');
Expand All @@ -68,6 +76,23 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}
}

/**
* @param \GuzzleHttp\Client $client
*/
public function setClient($client): void {
$this->client = $client;
}

/**
* @return \GuzzleHttp\Client
*/
public function getClient(): Client {
if (!isset($this->client)) {
$this->setClient(new Client());
}
return $this->client;
}

/**
* @return string
*/
Expand Down
74 changes: 37 additions & 37 deletions src/SelfUpdate/Strategy/GithubStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Acquia\Cli\SelfUpdate\Strategy;

use GuzzleHttp\Client;
use Humbug\SelfUpdate\Exception\HttpRequestException;
use Humbug\SelfUpdate\Exception\JsonParsingException;
use Humbug\SelfUpdate\Updater;
Expand All @@ -20,21 +21,27 @@ class GithubStrategy extends \Humbug\SelfUpdate\Strategy\GithubStrategy {
*/
private $remoteUrl;

/**
* @var Client
*/
private $client;

/**
* Retrieve the current version available remotely.
*
* @param Updater $updater
*
* @return string|bool
* @throws \Acquia\Cli\Exception\AcquiaCliException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getCurrentRemoteVersion(Updater $updater) {
/** Switch remote request errors to HttpRequestExceptions */
set_error_handler([$updater, 'throwHttpRequestException']);
$packageUrl = $this->getApiUrl();
$context = $this->getCurlContext();
$releases = json_decode(humbug_get_contents($packageUrl, FALSE, $context), TRUE);
restore_error_handler();
$client = $this->getClient();
$response = $client->request('GET', $packageUrl, [
'headers' => ['User-Agent' => $this->getPackageName()]
]);
$contents = $response->getBody()->getContents();
$releases = json_decode($contents, TRUE);

if (NULL === $releases || json_last_error() !== JSON_ERROR_NONE) {
throw new JsonParsingException(
Expand All @@ -46,9 +53,11 @@ public function getCurrentRemoteVersion(Updater $updater) {
// Remove any version that does not have an attached phar file.
foreach ($releases as $key => $release) {
if (!$this->getReleasePharAsset($release)) {
unset($release[$key]);
unset($releases[$key]);
}
}
// Re-key the array.
$releases = array_values($releases);

$versions = array_column($releases, 'tag_name');
$versionParser = new VersionParser($versions);
Expand All @@ -69,6 +78,20 @@ public function getCurrentRemoteVersion(Updater $updater) {
return $this->remoteVersion;
}

/**
* @param Client $client
*/
public function setClient($client): void {
$this->client = $client;
}

/**
* @return Client
*/
public function getClient(): Client {
return $this->client;
}

/**
* @return string
*/
Expand Down Expand Up @@ -98,39 +121,16 @@ protected function getReleasePharAsset(array $release) {
* Download the remote Phar file.
*
* @param Updater $updater
*
* @return void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function download(Updater $updater): void {
/** Switch remote request errors to HttpRequestExceptions */
set_error_handler([$updater, 'throwHttpRequestException']);
$context = $this->getCurlContext();
$result = humbug_get_contents($this->remoteUrl, FALSE, $context);
restore_error_handler();
if (FALSE === $result) {
throw new HttpRequestException(sprintf(
'Request to URL failed: %s', $this->remoteUrl
));
}

file_put_contents($updater->getTempPharFile(), $result);
}

/**
* GitHub requires a user agent to be set for all requests to their API.
*
* @return resource
*/
protected function getCurlContext() {
$opts = [
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: ' . $this->getPackageName()
]
]
];

return stream_context_create($opts);
$response = $this->getClient()->request('GET', $this->remoteUrl, [
'headers' => ['User-Agent' => $this->getPackageName()]
]);
$response_contents = $response->getBody()->getContents();
file_put_contents($updater->getTempPharFile(), $response_contents);
}

}
Binary file added tests/fixtures/test.phar
Binary file not shown.
Loading

0 comments on commit 65e5efa

Please sign in to comment.