Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI-1328: Pull command asks for environment twice #1732

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Command/Pull/PullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ protected function configure(): void {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
parent::execute($input, $output);

$this->setDirAndRequireProjectCwd($input);
$clone = $this->determineCloneProject($output);
$sourceEnvironment = $this->determineEnvironment($input, $output, TRUE);
Expand All @@ -50,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (!$input->getOption('no-databases')) {
$this->pullDatabase($input, $output);
$this->pullDatabase($input, $output, $sourceEnvironment);
}

if (!$input->getOption('no-scripts')) {
Expand Down
9 changes: 1 addition & 8 deletions src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use React\EventLoop\Loop;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -112,10 +111,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
$this->checklist = new Checklist($output);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
return Command::SUCCESS;
}

protected function pullCode(InputInterface $input, OutputInterface $output, bool $clone, EnvironmentResponse $sourceEnvironment): void {
if ($clone) {
$this->checklist->addItem('Cloning git repository from the Cloud Platform');
Expand All @@ -132,14 +127,12 @@ protected function pullCode(InputInterface $input, OutputInterface $output, bool
* @param bool $onDemand Force on-demand backup.
* @param bool $noImport Skip import.
*/
protected function pullDatabase(InputInterface $input, OutputInterface $output, bool $onDemand = FALSE, bool $noImport = FALSE, bool $multipleDbs = FALSE): void {
$this->setDirAndRequireProjectCwd($input);
protected function pullDatabase(InputInterface $input, OutputInterface $output, EnvironmentResponse $sourceEnvironment, bool $onDemand = FALSE, bool $noImport = FALSE, bool $multipleDbs = FALSE): void {
if (!$noImport) {
// Verify database connection.
$this->connectToLocalDatabase($this->getLocalDbHost(), $this->getLocalDbUser(), $this->getLocalDbName(), $this->getLocalDbPassword(), $this->getOutputCallback($output, $this->checklist));
}
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$sourceEnvironment = $this->determineEnvironment($input, $output, TRUE);
$site = $this->determineSite($sourceEnvironment, $input);
$databases = $this->determineCloudDatabases($acquiaCloudClient, $sourceEnvironment, $site, $multipleDbs);

Expand Down
4 changes: 3 additions & 1 deletion src/Command/Pull/PullDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$multipleDbs = $input->hasOption('multiple-dbs') && $input->getOption('multiple-dbs');
// $noImport implies $noScripts.
$noScripts = $noImport || $noScripts;
$this->pullDatabase($input, $output, $onDemand, $noImport, $multipleDbs);
$this->setDirAndRequireProjectCwd($input);
$sourceEnvironment = $this->determineEnvironment($input, $output, TRUE);
$this->pullDatabase($input, $output, $sourceEnvironment, $onDemand, $noImport, $multipleDbs);
if (!$noScripts) {
$this->runDrushCacheClear($this->getOutputCallback($output, $this->checklist), $this->checklist);
$this->runDrushSqlSanitize($this->getOutputCallback($output, $this->checklist), $this->checklist);
Expand Down
1 change: 0 additions & 1 deletion src/Command/Pull/PullFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ protected function configure(): void {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
parent::execute($input, $output);
$this->setDirAndRequireProjectCwd($input);
$sourceEnvironment = $this->determineEnvironment($input, $output, TRUE);
$this->pullFiles($input, $output, $sourceEnvironment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testArchiveExport(): void {
$localMachineHelper->getFilesystem()->willReturn($fileSystem->reveal())->shouldBeCalled();
$this->mockExecutePvExists($localMachineHelper);
$this->mockExecuteDrushExists($localMachineHelper);
$this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir);
$this->mockExecuteDrushStatus($localMachineHelper, $this->projectDir);
$this->mockCreateMySqlDumpOnLocal($localMachineHelper);
$localMachineHelper->checkRequiredBinariesExist(["tar"])->shouldBeCalled();
$localMachineHelper->execute(Argument::type('array'), Argument::type('callable'), NULL, TRUE)->willReturn($this->mockProcess())->shouldBeCalled();
Expand Down
34 changes: 4 additions & 30 deletions tests/phpunit/src/Commands/Pull/PullCodeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testWithScripts(): void {
$this->mockExecuteComposerExists($localMachineHelper);
$this->mockExecuteComposerInstall($localMachineHelper, $process);
$this->mockExecuteDrushExists($localMachineHelper);
$this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir);
$this->mockExecuteDrushStatus($localMachineHelper, $this->projectDir);
$this->mockExecuteDrushCacheRebuild($localMachineHelper, $process);

$this->executeCommand([], self::inputChooseEnvironment());
Expand All @@ -139,7 +139,7 @@ public function testNoComposerJson(): void {
$this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir);
$process = $this->mockProcess();
$this->mockExecuteDrushExists($localMachineHelper);
$this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir);
$this->mockExecuteDrushStatus($localMachineHelper, $this->projectDir);
$this->mockExecuteDrushCacheRebuild($localMachineHelper, $process);

$this->executeCommand([], self::inputChooseEnvironment());
Expand Down Expand Up @@ -169,7 +169,7 @@ public function testNoComposer(): void {
->willReturn(FALSE)
->shouldBeCalled();
$this->mockExecuteDrushExists($localMachineHelper);
$this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir);
$this->mockExecuteDrushStatus($localMachineHelper, $this->projectDir);
$this->mockExecuteDrushCacheRebuild($localMachineHelper, $process);

$this->executeCommand([], self::inputChooseEnvironment());
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testWithVendorDir(): void {
$process = $this->mockProcess();
$this->mockExecuteComposerExists($localMachineHelper);
$this->mockExecuteDrushExists($localMachineHelper);
$this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir);
$this->mockExecuteDrushStatus($localMachineHelper, $this->projectDir);
$this->mockExecuteDrushCacheRebuild($localMachineHelper, $process);

$this->executeCommand([], self::inputChooseEnvironment());
Expand Down Expand Up @@ -289,30 +289,4 @@ protected function mockExecuteGitClone(
->shouldBeCalled();
}

protected function mockExecuteGitFetchAndCheckout(
ObjectProphecy $localMachineHelper,
ObjectProphecy $process,
mixed $cwd,
mixed $vcsPath
): void {
$localMachineHelper->execute([
'git',
'fetch',
'--all',
], Argument::type('callable'), $cwd, FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
$this->mockExecuteGitCheckout($localMachineHelper, $vcsPath, $cwd, $process);
}

protected function mockExecuteGitCheckout(ObjectProphecy $localMachineHelper, mixed $vcsPath, mixed $cwd, ObjectProphecy $process): void {
$localMachineHelper->execute([
'git',
'checkout',
$vcsPath,
], Argument::type('callable'), $cwd, FALSE)
->willReturn($process->reveal())
->shouldBeCalled();
}

}
61 changes: 56 additions & 5 deletions tests/phpunit/src/Commands/Pull/PullCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
use Acquia\Cli\Command\Pull\PullCommand;
use Acquia\Cli\Exception\AcquiaCliException;
use GuzzleHttp\Client;
use Prophecy\Argument;

/**
* @property \Acquia\Cli\Command\Pull\PullCommand $command
*/
class PullCommandTest extends PullCommandTestBase {

public function setUp(): void {
parent::setUp();
$this->setupFsFixture();
}

protected function createCommand(): CommandBase {
$this->httpClientProphecy = $this->prophet->prophesize(Client::class);

Expand All @@ -37,7 +33,62 @@ protected function createCommand(): CommandBase {
);
}

public function testPull(): void {
// Pull code.
$environment = $this->mockGetEnvironment();
$this->createMockGitConfigFile();
$localMachineHelper = $this->mockLocalMachineHelper();
$localMachineHelper->checkRequiredBinariesExist(["git"])->shouldBeCalled();
$finder = $this->mockFinder();
$localMachineHelper->getFinder()->willReturn($finder->reveal());
$process = $this->mockProcess();
$this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environment->vcs->path);
$this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir);

// Pull files.
$sshHelper = $this->mockSshHelper();
$this->mockGetCloudSites($sshHelper, $environment);
$this->mockGetFilesystem($localMachineHelper);
$parts = explode('.', $environment->ssh_url);
$sitegroup = reset($parts);
$this->mockExecuteRsync($localMachineHelper, $environment, '/mnt/files/' . $sitegroup . '.' . $environment->name . '/sites/bar/files/', $this->projectDir . '/docroot/sites/bar/files');
$this->command->sshHelper = $sshHelper->reveal();

// Pull database.
$this->mockExecuteMySqlConnect($localMachineHelper, TRUE);
$this->mockGetBackup($environment);
$this->mockExecuteMySqlListTables($localMachineHelper, 'drupal');
$process = $this->mockProcess();
$localMachineHelper
->execute(Argument::type('array'), Argument::type('callable'), NULL, FALSE, NULL, ['MYSQL_PWD' => $this->dbPassword])
->willReturn($process->reveal())
->shouldBeCalled();
$this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE, 'my_db', 'my_dbdev', 'drupal');
$this->executeCommand([
'--no-scripts' => TRUE,
], [
// Would you like Acquia CLI to search for a Cloud application that matches your local git config?
'n',
// Select a Cloud Platform application:
self::$INPUT_DEFAULT_CHOICE,
// Would you like to link the project at ... ?
'n',
// Choose an Acquia environment:
self::$INPUT_DEFAULT_CHOICE,
self::$INPUT_DEFAULT_CHOICE,
]);

$output = $this->getDisplay();

$this->assertStringContainsString('Select a Cloud Platform application:', $output);
$this->assertStringContainsString('[0] Sample application 1', $output);
$this->assertStringContainsString('Choose a Cloud Platform environment', $output);
$this->assertStringContainsString('[0] Dev, dev (vcs: master)', $output);
$this->assertStringContainsString('Choose a database [my_db (default)]:', $output);
}

public function testMissingLocalRepo(): void {
$this->setupFsFixture();
// Unset repo root. Mimics failing to find local git repo. Command must be re-created
// to re-inject the parameter into the command.
$this->acliRepoRoot = '';
Expand Down
Loading