Skip to content

Commit

Permalink
tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Dec 13, 2023
1 parent 83ae3ff commit 9ec86da
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
16 changes: 16 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace PHPSTORM_META {

use AcquiaCloudApi\Response\ApplicationResponse;
use AcquiaCloudApi\Response\ApplicationsResponse;
use AcquiaCloudApi\Response\DatabasesResponse;
use AcquiaCloudApi\Response\EnvironmentsResponse;

override(\Acquia\Cli\Tests\TestBase::mockRequest(), map([
'getApplications' => ApplicationsResponse::class,
'getApplicationByUuid' => ApplicationResponse::class,
'getApplicationEnvironments' => EnvironmentsResponse::class,
'getEnvironmentsDatabases' => DatabasesResponse::class
]));

}
2 changes: 1 addition & 1 deletion src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function getLocalFilesDir(string $site): string {
return $this->dir . '/docroot/sites/' . $site . '/files';
}

public static function getBackupPath(object $environment, object $database, object $backupResponse): string {
public static function getBackupPath(object $environment, DatabaseResponse $database, object $backupResponse): string {
// Databases have a machine name not exposed via the API; we can only
// approximately reconstruct it and match the filename you'd get downloading
// a backup from Cloud UI.
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/src/Commands/Ide/IdeListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ protected function createCommand(): CommandBase {
*/
public function testIdeListCommand(): void {
$applications = $this->mockRequest('getApplications');
/** @var \AcquiaCloudApi\Response\ApplicationResponse $application */
$application = $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid);
$this->mockRequest('getApplicationIdes', $application->uuid);
$inputs = [
Expand Down
26 changes: 19 additions & 7 deletions tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function providerTestPullDatabaseWithInvalidSslCertificate(): array {
}

public function mockGetBackup(mixed $environment): void {
$databases = $this->mockGetEnvironmentsDatabases($environment->id);
$databases = $this->mockRequest('getEnvironmentsDatabases', $environment->id);
$tamper = function ($backups): void {
$backups[0]->completedAt = $backups[0]->completed_at;
};
Expand Down Expand Up @@ -418,25 +418,37 @@ public function testDownloadProgressDisplay(): void {
$this->assertStringContainsString('100/100 [============================] 100%', $output->fetch());
}

protected function mockDownloadBackup(object $selectedDatabase, object $selectedEnvironment, object $selectedBackup, int $curlCode = 0): object {
protected function mockDownloadBackup(object $database, object $environment, object $backup, int $curlCode = 0): object {
if ($curlCode) {
$this->prophet->prophesize(StreamInterface::class);
/** @var RequestException|ObjectProphecy $requestException */
$requestException = $this->prophet->prophesize(RequestException::class);
$requestException->getHandlerContext()->willReturn(['errno' => $curlCode]);
$this->clientProphecy->stream('get', "/environments/{$selectedEnvironment->id}/databases/{$selectedDatabase->name}/backups/1/actions/download", [])
$this->clientProphecy->stream('get', "/environments/{$environment->id}/databases/{$database->name}/backups/1/actions/download", [])
->willThrow($requestException->reveal())
->shouldBeCalled();
$response = $this->prophet->prophesize(ResponseInterface::class);
$this->httpClientProphecy->request('GET', 'https://other.example.com/download-backup', Argument::type('array'))->willReturn($response->reveal())->shouldBeCalled();
$domainsResponse = $this->getMockResponseFromSpec('/environments/{environmentId}/domains', 'get', 200);
$this->clientProphecy->request('get', "/environments/{$selectedEnvironment->id}/domains")->willReturn($domainsResponse->_embedded->items);
$this->clientProphecy->request('get', "/environments/{$environment->id}/domains")->willReturn($domainsResponse->_embedded->items);
$this->command->setBackupDownloadUrl(new Uri( 'https://www.example.com/download-backup'));
}
else {
$this->mockDownloadBackupResponse($selectedEnvironment, $selectedDatabase->name, 1);
$this->mockDownloadBackupResponse($environment, $database->name, 1);
}
$localFilepath = PullCommandBase::getBackupPath($selectedEnvironment, $selectedDatabase, $selectedBackup);
if ($database->flags->default) {
$dbMachineName = $database->name . $environment->name;
}
else {
$dbMachineName = 'db' . $database->id;
}
$filename = implode('-', [
$environment->name,
$database->name,
$dbMachineName,
$backup->completedAt,
]) . '.sql.gz';
$localFilepath = Path::join(sys_get_temp_dir(), $filename);
$this->clientProphecy->addOption('sink', $localFilepath)->shouldBeCalled();
$this->clientProphecy->addOption('curl.options', [
'CURLOPT_FILE' => $localFilepath,
Expand All @@ -446,7 +458,7 @@ protected function mockDownloadBackup(object $selectedDatabase, object $selected
$this->clientProphecy->addOption('on_stats', Argument::type('Closure'))->shouldBeCalled();
$this->clientProphecy->getOptions()->willReturn([]);

return $selectedDatabase;
return $database;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testNoAuthenticationRequired(): void {
public function testPushArtifact(): void {
$applications = $this->mockRequest('getApplications');
$this->mockRequest('getApplicationByUuid', $applications[0]->uuid);
$environments = $this->mockGetApplicationEnvironments($applications[0]->uuid);
$environments = $this->mockRequest('getApplicationEnvironments', $applications[0]->uuid);
$localMachineHelper = $this->mockLocalMachineHelper();
$this->setUpPushArtifact($localMachineHelper, $environments[0]->vcs->path, [$environments[0]->vcs->url]);
$inputs = [
Expand Down
17 changes: 5 additions & 12 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use Acquia\Cli\Helpers\TelemetryHelper;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Exception\ApiErrorException;
use AcquiaCloudApi\Response\DatabasesResponse;
use AcquiaCloudApi\Response\EnvironmentsResponse;
use AcquiaCloudApi\Response\IdeResponse;
use AcquiaLogstream\LogstreamManager;
use Closure;
Expand Down Expand Up @@ -439,16 +437,11 @@ protected function createMockAcliConfigFile(string $cloudAppUuid): void {
$this->datastoreAcli->set('cloud_app_uuid', $cloudAppUuid);
}

protected function mockGetEnvironmentsDatabases(string $id): DatabasesResponse {
$databases = $this->mockRequest('getEnvironmentsDatabases', $id);
return new DatabasesResponse($databases);
}

protected function mockGetApplicationEnvironments(string $id): EnvironmentsResponse {
$environments = $this->mockRequest('getApplicationEnvironments', $id);
return new EnvironmentsResponse($environments);
}

/**
* This is the preferred generic way of mocking requests and responses. We still maintain a lot of boilerplate mocking methods for legacy reasons.
*
* Auto-completion and return type inferencing is provided by .phpstorm.meta.php.
*/
protected function mockRequest(string $operationId, string|array|null $params = NULL, ?array $body = NULL, ?string $exampleResponse = NULL, Closure $tamper = NULL): object|array {
if (is_string($params)) {
$params = [$params];
Expand Down

0 comments on commit 9ec86da

Please sign in to comment.