From 26b140fccdf5241198abb7554973343afb3db1d2 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 7 May 2024 12:48:38 -0700 Subject: [PATCH] kill mutant --- tests/phpunit/src/CommandTestBase.php | 8 +- .../Commands/Push/PushDatabaseCommandTest.php | 102 ++++++++++++------ 2 files changed, 76 insertions(+), 34 deletions(-) diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index c8c973dc6..0374d1b81 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -81,7 +81,7 @@ protected function setCommand(CommandBase $command): void { * An array of strings representing each input passed to the command input * stream. */ - protected function executeCommand(array $args = [], array $inputs = []): void { + protected function executeCommand(array $args = [], array $inputs = [], int $verbosity = Output::VERBOSITY_VERY_VERBOSE): void { $cwd = $this->projectDir; $tester = $this->getCommandTester(); $tester->setInputs($inputs); @@ -95,7 +95,7 @@ protected function executeCommand(array $args = [], array $inputs = []): void { } try { - $tester->execute($args, ['verbosity' => Output::VERBOSITY_VERY_VERBOSE]); + $tester->execute($args, ['verbosity' => $verbosity]); } catch (Exception $e) { if (getenv('ACLI_PRINT_COMMAND_OUTPUT')) { @@ -374,12 +374,12 @@ protected function mockNotificationResponseFromObject(object $responseWithNotifi return $this->mockRequest('getNotificationByUuid', $uuid); } - protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper): void { + protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper, bool $printOutput = TRUE): void { $localMachineHelper->checkRequiredBinariesExist(["mysqldump", "gzip"])->shouldBeCalled(); $process = $this->mockProcess(); $process->getOutput()->willReturn(''); $command = 'MYSQL_PWD=drupal mysqldump --host=localhost --user=drupal drupal | pv --rate --bytes | gzip -9 > ' . sys_get_temp_dir() . '/acli-mysql-dump-drupal.sql.gz'; - $localMachineHelper->executeFromCmd($command, Argument::type('callable'), NULL, TRUE)->willReturn($process->reveal()) + $localMachineHelper->executeFromCmd($command, Argument::type('callable'), NULL, $printOutput)->willReturn($process->reveal()) ->shouldBeCalled(); } diff --git a/tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php b/tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php index 67d49c94e..03568a58c 100644 --- a/tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php +++ b/tests/phpunit/src/Commands/Push/PushDatabaseCommandTest.php @@ -6,16 +6,32 @@ use Acquia\Cli\Command\CommandBase; use Acquia\Cli\Command\Push\PushDatabaseCommand; +use Acquia\Cli\Helpers\LocalMachineHelper; +use Acquia\Cli\Helpers\SshHelper; use Acquia\Cli\Tests\CommandTestBase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Path; +use Symfony\Component\Process\Process; /** * @property \Acquia\Cli\Command\Push\PushDatabaseCommand $command */ class PushDatabaseCommandTest extends CommandTestBase { + /** + * @return mixed[] + */ + public function providerTestPushDatabase(): array { + return [ + [OutputInterface::VERBOSITY_NORMAL, FALSE], + [OutputInterface::VERBOSITY_VERY_VERBOSE, TRUE], + ]; + } + protected function createCommand(): CommandBase { + return $this->injectCommand(PushDatabaseCommand::class); } @@ -24,26 +40,34 @@ public function setUp(): void { parent::setUp(); } - public function testPushDatabase(): void { - $applicationsResponse = $this->mockApplicationsRequest(); - $this->mockApplicationRequest(); - $environmentsResponse = $this->mockAcsfEnvironmentsRequest($applicationsResponse); - $selectedEnvironment = $environmentsResponse->_embedded->items[0]; + /** + * @dataProvider providerTestPushDatabase + */ + public function testPushDatabase(int $verbosity, bool $printOutput): void { + $applications = $this->mockRequest('getApplications'); + $application = $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $tamper = function ($responses): void { + foreach ($responses as $response) { + $response->ssh_url = 'profserv2.01dev@profserv201dev.ssh.enterprise-g1.acquia-sites.com'; + $response->domains = ["profserv201dev.enterprise-g1.acquia-sites.com"]; + } + }; + $environments = $this->mockRequest('getApplicationEnvironments', $application->uuid, NULL, NULL, $tamper); $this->createMockGitConfigFile(); - $this->mockAcsfDatabasesResponse($selectedEnvironment); - $sshHelper = $this->mockSshHelper(); - $this->mockGetAcsfSites($sshHelper); + $this->mockAcsfDatabasesResponse($environments[self::$INPUT_DEFAULT_CHOICE]); $process = $this->mockProcess(); $localMachineHelper = $this->mockLocalMachineHelper(); + $localMachineHelper->checkRequiredBinariesExist(['ssh'])->shouldBeCalled(); + $this->mockGetAcsfSitesLMH($localMachineHelper); // Database. $this->mockExecutePvExists($localMachineHelper); - $this->mockCreateMySqlDumpOnLocal($localMachineHelper); - $this->mockUploadDatabaseDump($localMachineHelper, $process); - $this->mockImportDatabaseDumpOnRemote($sshHelper, $selectedEnvironment, $process); + $this->mockCreateMySqlDumpOnLocal($localMachineHelper, $printOutput); + $this->mockUploadDatabaseDump($localMachineHelper, $process, $printOutput); + $this->mockImportDatabaseDumpOnRemote($localMachineHelper, $process, $printOutput); - $this->command->sshHelper = $sshHelper->reveal(); + $this->command->sshHelper = new SshHelper($this->output, $localMachineHelper->reveal(), $this->logger); $inputs = [ // Would you like Acquia CLI to search for a Cloud application that matches your local git config? @@ -60,7 +84,7 @@ public function testPushDatabase(): void { 'y', ]; - $this->executeCommand([], $inputs); + $this->executeCommand([], $inputs, $verbosity); $output = $this->getDisplay(); @@ -76,7 +100,8 @@ public function testPushDatabase(): void { protected function mockUploadDatabaseDump( ObjectProphecy $localMachineHelper, - ObjectProphecy $process + ObjectProphecy $process, + bool $printOutput = TRUE, ): void { $localMachineHelper->checkRequiredBinariesExist(['rsync'])->shouldBeCalled(); $command = [ @@ -86,22 +111,7 @@ protected function mockUploadDatabaseDump( sys_get_temp_dir() . '/acli-mysql-dump-drupal.sql.gz', 'profserv2.01dev@profserv201dev.ssh.enterprise-g1.acquia-sites.com:/mnt/tmp/profserv2.01dev/acli-mysql-dump-drupal.sql.gz', ]; - $localMachineHelper->execute($command, Argument::type('callable'), NULL, TRUE, NULL) - ->willReturn($process->reveal()) - ->shouldBeCalled(); - } - - protected function mockImportDatabaseDumpOnRemote( - ObjectProphecy $sshHelper, - object $environmentsResponse, - mixed $process - ): void { - $sshHelper->executeCommand( - $environmentsResponse->ssh_url, - ['pv /mnt/tmp/profserv2.01dev/acli-mysql-dump-drupal.sql.gz --bytes --rate | gunzip | MYSQL_PWD=password mysql --host=fsdb-74.enterprise-g1.hosting.acquia.com.enterprise-g1.hosting.acquia.com --user=s164 profserv2db14390'], - TRUE, - NULL - ) + $localMachineHelper->execute($command, Argument::type('callable'), NULL, $printOutput, NULL) ->willReturn($process->reveal()) ->shouldBeCalled(); } @@ -118,4 +128,36 @@ protected function mockExecuteMySqlImport( ->shouldBeCalled(); } + protected function mockGetAcsfSitesLMH(ObjectProphecy $localMachineHelper): void { + $acsfMultisiteFetchProcess = $this->mockProcess(); + $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); + $acsfMultisiteFetchProcess->getOutput()->willReturn($multisiteConfig)->shouldBeCalled(); + $cmd = [ + 0 => 'ssh', + 1 => 'profserv2.01dev@profserv201dev.ssh.enterprise-g1.acquia-sites.com', + 2 => '-t', + 3 => '-o StrictHostKeyChecking=no', + 4 => '-o AddressFamily inet', + 5 => '-o LogLevel=ERROR', + 6 => 'cat', + 7 => '/var/www/site-php/profserv2.01dev/multisite-config.json', + ]; + $localMachineHelper->execute($cmd, Argument::type('callable'), NULL, FALSE, NULL)->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); + } + + private function mockImportDatabaseDumpOnRemote(ObjectProphecy|LocalMachineHelper $localMachineHelper, Process|ObjectProphecy $process, bool $printOutput = TRUE): void { + $cmd = [ + 0 => 'ssh', + 1 => 'profserv2.01dev@profserv201dev.ssh.enterprise-g1.acquia-sites.com', + 2 => '-t', + 3 => '-o StrictHostKeyChecking=no', + 4 => '-o AddressFamily inet', + 5 => '-o LogLevel=ERROR', + 6 => 'pv /mnt/tmp/profserv2.01dev/acli-mysql-dump-drupal.sql.gz --bytes --rate | gunzip | MYSQL_PWD=password mysql --host=fsdb-74.enterprise-g1.hosting.acquia.com.enterprise-g1.hosting.acquia.com --user=s164 profserv2db14390', + ]; + $localMachineHelper->execute($cmd, Argument::type('callable'), NULL, $printOutput, NULL) + ->willReturn($process->reveal()) + ->shouldBeCalled(); + } + }