Skip to content

Commit

Permalink
CLI-1389: Catch datastore validation errors in tests (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Sep 11, 2024
1 parent 81703a7 commit 456c3ba
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 40 deletions.
2 changes: 0 additions & 2 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Gitlab\Api\Projects;
use Gitlab\Api\Users;
use Gitlab\Exception\RuntimeException;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Constraint\StringContains;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down
6 changes: 0 additions & 6 deletions tests/phpunit/src/Commands/App/AppOpenCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@ public function testAppOpenCommand(): void
$this->executeCommand(['applicationUuid' => $applicationUuid]);
}

/**
* @group brokenProphecy
*/
public function testAppOpenNoBrowser(): void
{
$applicationUuid = 'a47ac10b-58cc-4372-a567-0e02b2c3d470';
$localMachineHelper = $this->mockLocalMachineHelper();
$localMachineHelper->isBrowserAvailable()->willReturn(false);

$this->mockApplicationRequest();
$this->createMockAcliConfigFile($applicationUuid);
$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('No browser is available on this machine');
$this->executeCommand();
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/src/Commands/App/LinkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function testLinkCommand(): void
public function testLinkCommandAlreadyLinked(): void
{
$this->createMockAcliConfigFile('a47ac10b-58cc-4372-a567-0e02b2c3d470');
$this->createDataStores();
$this->command = $this->injectCommand(LinkCommand::class);
$this->mockApplicationRequest();
$this->executeCommand();
$output = $this->getDisplay();
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/src/Commands/App/UnlinkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function testUnlinkCommand(): void
$cloudApplication = $applicationsResponse->{'_embedded'}->items[0];
$cloudApplicationUuid = $cloudApplication->uuid;
$this->createMockAcliConfigFile($cloudApplicationUuid);
$this->createDataStores();
$this->command = $this->injectCommand(UnlinkCommand::class);
$this->mockApplicationRequest();

// Assert we set it correctly.
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/src/Commands/CommandBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function testUnauthenticatedFailure(): void

public function testCloudAppFromLocalConfig(): void
{
$this->command = $this->injectCommand(IdeListCommand::class);
$this->mockRequest('getApplicationIdes', 'a47ac10b-58cc-4372-a567-0e02b2c3d470');
$this->createMockAcliConfigFile('a47ac10b-58cc-4372-a567-0e02b2c3d470');
$this->createDataStores();
$this->command = $this->injectCommand(IdeListCommand::class);
$this->executeCommand();
}

Expand Down
9 changes: 6 additions & 3 deletions tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ public function testPushTagArtifact(): void

public function testPushArtifactWithAcquiaCliFile(): void
{
$this->datastoreAcli->set('push.artifact.destination_git_urls', [
$destinationGitUrls = [
'https://github.com/example1/cli.git',
'https://github.com/example2/cli.git',
]);
];
$this->createMockAcliConfigFile(['push' => ['artifact' => ['destination_git_urls' => $destinationGitUrls],],]);
$this->createDataStores();
$this->command = $this->injectCommand(PushArtifactCommand::class);
$localMachineHelper = $this->mockLocalMachineHelper();
$this->setUpPushArtifact($localMachineHelper, 'master', $this->datastoreAcli->get('push.artifact.destination_git_urls'));
$this->setUpPushArtifact($localMachineHelper, 'master', $destinationGitUrls);
$this->executeCommand([
'--destination-git-branch' => 'master',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function testRemoteAliasesDownloadCommand(array $inputs, array $args, str
$cloudApplication = $applicationsResponse->{'_embedded'}->items[0];
$cloudApplicationUuid = $cloudApplication->uuid;
$this->createMockAcliConfigFile($cloudApplicationUuid);
$this->createDataStores();
$this->command = $this->injectCommand(AliasesDownloadCommand::class);
$this->mockApplicationRequest();
}

Expand Down
8 changes: 2 additions & 6 deletions tests/phpunit/src/Commands/TelemetryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public function providerTestTelemetryPrompt(): array
*/
public function testTelemetryPrompt(array $inputs, mixed $message): void
{
$this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => null];
$this->createMockConfigFiles();
$this->createMockCloudConfigFile([DataStoreContract::SEND_TELEMETRY => null]);
$this->createMockAcliConfigFile('a47ac10b-58cc-4372-a567-0e02b2c3d470');
$this->createDataStores();
$this->mockApplicationRequest();
Expand All @@ -86,17 +85,14 @@ public function testTelemetryPrompt(array $inputs, mixed $message): void
*/
public function testAmplitudeDisabled(): void
{
$this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => false];
$this->createMockConfigFiles();
$this->executeCommand();

$this->assertEquals(0, $this->getStatusCode());
}

public function testMigrateLegacyTelemetryPreference(): void
{
$this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => null];
$this->createMockConfigFiles();
$this->createMockCloudConfigFile([DataStoreContract::SEND_TELEMETRY => null]);
$this->fs->remove($this->legacyAcliConfigFilepath);
$legacyAcliConfig = ['send_telemetry' => false];
$contents = json_encode($legacyAcliConfig);
Expand Down
34 changes: 12 additions & 22 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ abstract class TestBase extends TestCase

protected Client|ObjectProphecy $clientProphecy;

/**
* @var array<mixed>
*/
protected array $acliConfig = [];

/**
* @var array<mixed>
*/
protected array $cloudConfig = [];

protected string $key = '17feaf34-5d04-402b-9a67-15d5161d24e1';

protected string $secret = 'X1u\/PIQXtYaoeui.4RJSJpGZjwmWYmfl5AUQkAebYE=';
Expand Down Expand Up @@ -425,18 +415,13 @@ protected function createLocalSshKey(mixed $contents): string
protected function createMockConfigFiles(): void
{
$this->createMockCloudConfigFile();

$defaultValues = [];
$acliConfig = array_merge($defaultValues, $this->acliConfig);
$contents = json_encode($acliConfig, JSON_THROW_ON_ERROR);
$filepath = $this->acliConfigFilepath;
$this->fs->dumpFile($filepath, $contents);
$this->createMockAcliConfigFile();
}

protected function createMockCloudConfigFile(mixed $defaultValues = []): void
protected function createMockCloudConfigFile(mixed $cloudConfig = []): void
{
if (!$defaultValues) {
$defaultValues = [
if (!$cloudConfig) {
$cloudConfig = [
'acli_key' => $this->key,
'keys' => [
(string) ($this->key) => [
Expand All @@ -448,15 +433,20 @@ protected function createMockCloudConfigFile(mixed $defaultValues = []): void
DataStoreContract::SEND_TELEMETRY => false,
];
}
$cloudConfig = array_merge($defaultValues, $this->cloudConfig);
$contents = json_encode($cloudConfig, JSON_THROW_ON_ERROR);
$filepath = $this->cloudConfigFilepath;
$this->fs->dumpFile($filepath, $contents);
}

protected function createMockAcliConfigFile(string $cloudAppUuid): void
protected function createMockAcliConfigFile(mixed $cloudAppUuid = null): void
{
$this->datastoreAcli->set('cloud_app_uuid', $cloudAppUuid);
$acliConfig = [];
if (is_string($cloudAppUuid)) {
$acliConfig['cloud_app_uuid'] = $cloudAppUuid;
} elseif (is_array($cloudAppUuid)) {
$acliConfig = $cloudAppUuid;
}
$this->fs->dumpFile($this->acliConfigFilepath, json_encode($acliConfig, JSON_THROW_ON_ERROR));
}

/**
Expand Down

0 comments on commit 456c3ba

Please sign in to comment.