Skip to content

Commit

Permalink
kill mutant
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed May 3, 2024
1 parent 82a9af6 commit d6becf8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
22 changes: 12 additions & 10 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ protected function setUpdateClient(int $statusCode = 200): void {
$this->command->setUpdateClient($guzzleClient->reveal());
}

protected function mockPollCloudViaSsh(object $environmentsResponse): ObjectProphecy {
protected function mockPollCloudViaSsh(array $environmentsResponse, bool $ssh = TRUE): ObjectProphecy {
$process = $this->prophet->prophesize(Process::class);
$process->isSuccessful()->willReturn(TRUE);
$process->getExitCode()->willReturn(0);
Expand All @@ -428,18 +428,20 @@ protected function mockPollCloudViaSsh(object $environmentsResponse): ObjectProp
$gitProcess->getExitCode()->willReturn(128);
$sshHelper = $this->mockSshHelper();
// Mock Git.
$urlParts = explode(':', $environmentsResponse->_embedded->items[0]->vcs->url);
$urlParts = explode(':', $environmentsResponse[0]->vcs->url);
$sshHelper->executeCommand($urlParts[0], ['ls'], FALSE)
->willReturn($gitProcess->reveal())
->shouldBeCalled();
// Mock non-prod.
$sshHelper->executeCommand($environmentsResponse->_embedded->items[0]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
// Mock prod.
$sshHelper->executeCommand($environmentsResponse->_embedded->items[1]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
if ($ssh) {
// Mock non-prod.
$sshHelper->executeCommand($environmentsResponse[0]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
// Mock prod.
$sshHelper->executeCommand($environmentsResponse[1]->ssh_url, ['ls'], FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
}
return $sshHelper;
}

Expand Down
56 changes: 55 additions & 1 deletion tests/phpunit/src/Commands/Ssh/SshKeyUploadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testUpload(array $args, array $inputs, bool $perms): void {

if ($perms) {
$environmentsResponse = $this->mockEnvironmentsRequest($applicationsResponse);
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse);
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse->_embedded->items);
$this->command->sshHelper = $sshHelper->reveal();
}

Expand All @@ -103,6 +103,60 @@ public function testUpload(array $args, array $inputs, bool $perms): void {
$this->assertStringContainsString('Your SSH key is ready for use!', $output);
}

// Ensure permission checks aren't against a Node environment.
public function testUploadNode(): void {
$sshKeysRequestBody = $this->getMockRequestBodyFromSpec('/account/ssh-keys');
$body = [
'json' => [
'label' => $sshKeysRequestBody['label'],
'public_key' => $sshKeysRequestBody['public_key'],
],
];
$this->mockRequest('postAccountSshKeys', NULL, $body);
$this->mockListSshKeyRequestWithUploadedKey($sshKeysRequestBody);
$applicationsResponse = $this->mockApplicationsRequest();
$applicationResponse = $this->mockApplicationRequest();
$this->mockPermissionsRequest($applicationResponse, TRUE);

$localMachineHelper = $this->mockLocalMachineHelper();
/** @var Filesystem|\Prophecy\Prophecy\ObjectProphecy $fileSystem */
$fileSystem = $this->prophet->prophesize(Filesystem::class);
$fileName = $this->mockGetLocalSshKey($localMachineHelper, $fileSystem, $sshKeysRequestBody['public_key']);

$localMachineHelper->getFilesystem()->willReturn($fileSystem);
$fileSystem->exists(Argument::type('string'))->willReturn(TRUE);
$localMachineHelper->getLocalFilepath(Argument::containingString('id_rsa'))->willReturn('id_rsa.pub');
$localMachineHelper->readFile(Argument::type('string'))->willReturn($sshKeysRequestBody['public_key']);

$tamper = function ($responses): void {
foreach ($responses as $response) {
$response->type = 'node';
}
};
$environmentsResponse = $this->mockRequest('getApplicationEnvironments', $applicationsResponse->_embedded->items[0]->uuid, NULL, NULL, $tamper);
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse, FALSE);
$this->command->sshHelper = $sshHelper->reveal();

// Choose a local SSH key to upload to the Cloud Platform.
$inputs = [
// Choose key.
'0',
// Enter a Cloud Platform label for this SSH key:
$sshKeysRequestBody['label'],
// Would you like to wait until Cloud Platform is ready? (yes/no)
'y',
// Would you like Acquia CLI to search for a Cloud application that matches your local git config? (yes/no)
'n',
];
$this->executeCommand([], $inputs);

// Assert.
$output = $this->getDisplay();
$this->assertStringContainsString("Uploaded $fileName to the Cloud Platform with label " . $sshKeysRequestBody['label'], $output);
$this->assertStringContainsString('Would you like to wait until your key is installed on all of your application\'s servers?', $output);
$this->assertStringContainsString('Your SSH key is ready for use!', $output);
}

public function testInvalidFilepath(): void {
$inputs = [
// Choose key.
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/src/Commands/WizardTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function runTestCreate(): void {
$localMachineHelper = $this->mockLocalMachineHelper();

// Poll Cloud.
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse);
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse->_embedded->items);
$this->command->sshHelper = $sshHelper->reveal();

$fileSystem = $this->prophet->prophesize(Filesystem::class);
Expand Down Expand Up @@ -132,7 +132,7 @@ protected function runTestSshKeyAlreadyUploaded(): void {
$this->mockRequest('postAccountSshKeys', NULL, $body);

// Poll Cloud.
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse);
$sshHelper = $this->mockPollCloudViaSsh($environmentsResponse->_embedded->items);
$this->command->sshHelper = $sshHelper->reveal();

$fileSystem = $this->prophet->prophesize(Filesystem::class);
Expand Down

0 comments on commit d6becf8

Please sign in to comment.