From 7b933c8aa50006c80d973d3280a82a0f4d786cd1 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Sat, 18 Jan 2025 10:08:26 +0530 Subject: [PATCH 1/8] PHP error : undefined array key handled --- src/Command/CommandBase.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 09ef0e790..e4c2655d8 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -1436,6 +1436,10 @@ protected function promptChooseAcsfSite(EnvironmentResponse $cloudEnvironment): foreach ($acsfSites['sites'] as $domain => $acsfSite) { $choices[] = "{$acsfSite['name']} ($domain)"; } + if (!count($choices)) { + throw new AcquiaCliException("Could not get ACSF sites"); + } + $choice = $this->io->choice('Choose a site', $choices, $choices[0]); $key = array_search($choice, $choices, true); $sites = array_values($acsfSites['sites']); From 87bb014c27fef207d7f25004b1deea7e4c9b88ec Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:08:46 +0530 Subject: [PATCH 2/8] Fix test case: --- tests/phpunit/src/CommandTestBase.php | 21 ++++++++---- .../Commands/Pull/PullFilesCommandTest.php | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index c4faf3dbd..b4e210eba 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -283,18 +283,27 @@ public function mockAcsfEnvironmentsRequest( /** * @return array */ - protected function mockGetAcsfSites(mixed $sshHelper): array + protected function mockGetAcsfSites(mixed $sshHelper, $existAcsfSites = true): array { $acsfMultisiteFetchProcess = $this->mockProcess(); $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); $acsfMultisiteFetchProcess->getOutput() ->willReturn($multisiteConfig) ->shouldBeCalled(); - $sshHelper->executeCommand( - Argument::type('string'), - ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], - false - )->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); + if ($existAcsfSites) { + $sshHelper->executeCommand( + Argument::type('string'), + ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], + false + )->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); + } else { + $sshHelper->executeCommand( + Argument::type('string'), + ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], + false + )->willReturn('')->shouldBeCalled(); + } + return json_decode($multisiteConfig, true); } diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 2b373a6be..2522e9a42 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -69,6 +69,40 @@ public function testRefreshAcsfFiles(): void $this->assertStringContainsString('[0] Dev, dev (vcs: master)', $output); } + public function testPullAcsfSitesException(): void + { + $applicationsResponse = $this->mockApplicationsRequest(); + $this->mockApplicationRequest(); + $environmentsResponse = $this->mockAcsfEnvironmentsRequest($applicationsResponse); + $selectedEnvironment = $environmentsResponse->_embedded->items[0]; + $sshHelper = $this->mockSshHelper(); + $this->mockGetAcsfSites($sshHelper, false); + $localMachineHelper = $this->mockLocalMachineHelper(); + $this->mockGetFilesystem($localMachineHelper); + $this->mockExecuteRsync($localMachineHelper, $selectedEnvironment, '/mnt/files/profserv2.01dev/sites/g/files/jxr5000596dev/files/', $this->projectDir . '/docroot/sites/jxr5000596dev/files'); + + $this->command->sshHelper = $sshHelper->reveal(); + + $inputs = [ + // Would you like Acquia CLI to search for a Cloud application that matches your local git config? + 'n', + // Select a Cloud Platform application: + 0, + // Would you like to link the project at ... ? + 'n', + // Choose an Acquia environment: + 0, + // Choose site from which to copy files: + 0, + ]; + + + $this->expectException(AcquiaCliException::class); + $this->expectExceptionMessage('Could not get ACSF sites'); + $this->executeCommand([], $inputs); + + } + public function testRefreshCloudFiles(): void { $applicationsResponse = $this->mockApplicationsRequest(); From e6814d2cdab50e18899a498b869cb94113870c97 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:09:08 +0530 Subject: [PATCH 3/8] Fix test case: --- tests/phpunit/src/CommandTestBase.php | 2 +- tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index b4e210eba..ae3fef11b 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -283,7 +283,7 @@ public function mockAcsfEnvironmentsRequest( /** * @return array */ - protected function mockGetAcsfSites(mixed $sshHelper, $existAcsfSites = true): array + protected function mockGetAcsfSites(mixed $sshHelper, bool $existAcsfSites = true): array { $acsfMultisiteFetchProcess = $this->mockProcess(); $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 2522e9a42..9187b1bf9 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -100,7 +100,6 @@ public function testPullAcsfSitesException(): void $this->expectException(AcquiaCliException::class); $this->expectExceptionMessage('Could not get ACSF sites'); $this->executeCommand([], $inputs); - } public function testRefreshCloudFiles(): void From 69674ca6a4254e3900df6348144bdb075ab9f476 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:19:50 +0530 Subject: [PATCH 4/8] fix the failure test case --- tests/phpunit/src/CommandTestBase.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index ae3fef11b..dab00f5e0 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -286,23 +286,19 @@ public function mockAcsfEnvironmentsRequest( protected function mockGetAcsfSites(mixed $sshHelper, bool $existAcsfSites = true): array { $acsfMultisiteFetchProcess = $this->mockProcess(); - $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); - $acsfMultisiteFetchProcess->getOutput() - ->willReturn($multisiteConfig) - ->shouldBeCalled(); if ($existAcsfSites) { - $sshHelper->executeCommand( - Argument::type('string'), - ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], - false - )->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); + $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); } else { - $sshHelper->executeCommand( - Argument::type('string'), - ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], - false - )->willReturn('')->shouldBeCalled(); + $multisiteConfig = "{}"; } + $acsfMultisiteFetchProcess->getOutput() + ->willReturn($multisiteConfig) + ->shouldBeCalled(); + $sshHelper->executeCommand( + Argument::type('string'), + ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], + false + )->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); return json_decode($multisiteConfig, true); } From 7f2ccb08b20fa22073de7975cbca895a01322760 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:32:43 +0530 Subject: [PATCH 5/8] fix the failure testcase --- tests/fixtures/no-multisite-config.json | 1 + tests/phpunit/src/CommandTestBase.php | 2 +- tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/no-multisite-config.json diff --git a/tests/fixtures/no-multisite-config.json b/tests/fixtures/no-multisite-config.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/tests/fixtures/no-multisite-config.json @@ -0,0 +1 @@ +{} diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index dab00f5e0..143601e37 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -289,7 +289,7 @@ protected function mockGetAcsfSites(mixed $sshHelper, bool $existAcsfSites = tru if ($existAcsfSites) { $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/multisite-config.json')); } else { - $multisiteConfig = "{}"; + $multisiteConfig = file_get_contents(Path::join($this->realFixtureDir, '/no-multisite-config.json')); } $acsfMultisiteFetchProcess->getOutput() ->willReturn($multisiteConfig) diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 9187b1bf9..53d5fd1f3 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -98,7 +98,6 @@ public function testPullAcsfSitesException(): void $this->expectException(AcquiaCliException::class); - $this->expectExceptionMessage('Could not get ACSF sites'); $this->executeCommand([], $inputs); } From fcc3c7931a44b73dbb785de9725b161aee64c979 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:42:44 +0530 Subject: [PATCH 6/8] Fix the failure test case --- tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 53d5fd1f3..49c1542a4 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -78,9 +78,7 @@ public function testPullAcsfSitesException(): void $sshHelper = $this->mockSshHelper(); $this->mockGetAcsfSites($sshHelper, false); $localMachineHelper = $this->mockLocalMachineHelper(); - $this->mockGetFilesystem($localMachineHelper); $this->mockExecuteRsync($localMachineHelper, $selectedEnvironment, '/mnt/files/profserv2.01dev/sites/g/files/jxr5000596dev/files/', $this->projectDir . '/docroot/sites/jxr5000596dev/files'); - $this->command->sshHelper = $sshHelper->reveal(); $inputs = [ From bea64ed408fe89da91a3dc03c13e893d6a6f8c92 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 22 Jan 2025 05:47:31 +0530 Subject: [PATCH 7/8] fix mutation test --- tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 49c1542a4..80f546d9a 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -77,8 +77,6 @@ public function testPullAcsfSitesException(): void $selectedEnvironment = $environmentsResponse->_embedded->items[0]; $sshHelper = $this->mockSshHelper(); $this->mockGetAcsfSites($sshHelper, false); - $localMachineHelper = $this->mockLocalMachineHelper(); - $this->mockExecuteRsync($localMachineHelper, $selectedEnvironment, '/mnt/files/profserv2.01dev/sites/g/files/jxr5000596dev/files/', $this->projectDir . '/docroot/sites/jxr5000596dev/files'); $this->command->sshHelper = $sshHelper->reveal(); $inputs = [ From 81d261d27af392666910dfa38d9c3cfefd741201 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Thu, 23 Jan 2025 12:16:15 -0800 Subject: [PATCH 8/8] cleanup --- phpunit.xml.dist | 2 +- src/Command/CommandBase.php | 3 +-- tests/fixtures/no-multisite-config.json | 4 +++- tests/phpunit/src/CommandTestBase.php | 4 ++-- .../Commands/Pull/PullFilesCommandTest.php | 22 ++++++++++++++----- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2f5d6be9c..1668c800e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ - + diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 28ba7b3b9..4a16eaf67 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -1518,9 +1518,8 @@ private function promptChooseAcsfSite(EnvironmentResponse $cloudEnvironment): mi $choices[] = "{$acsfSite['name']} ($domain)"; } if (!count($choices)) { - throw new AcquiaCliException("Could not get ACSF sites"); + throw new AcquiaCliException('No sites found in this environment'); } - $choice = $this->io->choice('Choose a site', $choices, $choices[0]); $key = array_search($choice, $choices, true); $sites = array_values($acsfSites['sites']); diff --git a/tests/fixtures/no-multisite-config.json b/tests/fixtures/no-multisite-config.json index 0967ef424..2ed37d370 100644 --- a/tests/fixtures/no-multisite-config.json +++ b/tests/fixtures/no-multisite-config.json @@ -1 +1,3 @@ -{} +{"cloud":{"site":"profserv2","env":"01dev"},"memcache_inc":"profiles\/gardens\/modules\/acquia\/memcache\/memcache.inc", + "sites": { + }} diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index 143601e37..3546266c3 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -282,8 +282,9 @@ public function mockAcsfEnvironmentsRequest( /** * @return array + * @throws \Acquia\Cli\Exception\AcquiaCliException */ - protected function mockGetAcsfSites(mixed $sshHelper, bool $existAcsfSites = true): array + protected function mockGetAcsfSites(SshHelper|ObjectProphecy $sshHelper, bool $existAcsfSites = true): array { $acsfMultisiteFetchProcess = $this->mockProcess(); if ($existAcsfSites) { @@ -299,7 +300,6 @@ protected function mockGetAcsfSites(mixed $sshHelper, bool $existAcsfSites = tru ['cat', '/var/www/site-php/profserv2.01dev/multisite-config.json'], false )->willReturn($acsfMultisiteFetchProcess->reveal())->shouldBeCalled(); - return json_decode($multisiteConfig, true); } diff --git a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php index 80f546d9a..cf5b102c4 100644 --- a/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullFilesCommandTest.php @@ -32,7 +32,10 @@ protected function createCommand(): CommandBase ); } - public function testRefreshAcsfFiles(): void + /** + * @throws \Exception + */ + public function testPullFilesAcsf(): void { $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); @@ -69,12 +72,14 @@ public function testRefreshAcsfFiles(): void $this->assertStringContainsString('[0] Dev, dev (vcs: master)', $output); } - public function testPullAcsfSitesException(): void + /** + * @throws \Exception + */ + public function testPullFilesAcsfNoSites(): void { $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); - $environmentsResponse = $this->mockAcsfEnvironmentsRequest($applicationsResponse); - $selectedEnvironment = $environmentsResponse->_embedded->items[0]; + $this->mockAcsfEnvironmentsRequest($applicationsResponse); $sshHelper = $this->mockSshHelper(); $this->mockGetAcsfSites($sshHelper, false); $this->command->sshHelper = $sshHelper->reveal(); @@ -94,10 +99,14 @@ public function testPullAcsfSitesException(): void $this->expectException(AcquiaCliException::class); + $this->expectExceptionMessage('No sites found in this environment'); $this->executeCommand([], $inputs); } - public function testRefreshCloudFiles(): void + /** + * @throws \Exception + */ + public function testPullFilesCloud(): void { $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); @@ -136,6 +145,9 @@ public function testRefreshCloudFiles(): void $this->assertStringContainsString('[0] Dev, dev (vcs: master)', $output); } + /** + * @throws \Exception + */ public function testInvalidCwd(): void { IdeHelper::setCloudIdeEnvVars();