From ec6fa49b95b918faf06729559b362224773afe23 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Mon, 27 Nov 2023 10:16:21 -0800 Subject: [PATCH] add tests --- tests/phpunit/src/CommandTestBase.php | 5 +-- .../Commands/Pull/PullDatabaseCommandTest.php | 31 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index 0983d2023..4c20c7a41 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -358,11 +358,12 @@ protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper } protected function mockExecutePvExists( - ObjectProphecy $localMachineHelper + ObjectProphecy $localMachineHelper, + bool $pvExists = TRUE ): void { $localMachineHelper ->commandExists('pv') - ->willReturn(TRUE) + ->willReturn($pvExists) ->shouldBeCalled(); } diff --git a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php index 9eeed79d3..7c7a6e913 100644 --- a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php @@ -69,6 +69,15 @@ public function testPullDatabasesLocalConnectionFailure(): void { ], $inputs); } + public function testPullDatabaseNoPv(): void { + $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, 0, TRUE, FALSE); + $inputs = $this->getInputs(); + + $this->executeCommand(['--no-scripts' => TRUE], $inputs); + $output = $this->getDisplay(); + $this->assertStringContainsString(' [WARNING] Install `pv` to see progress bar', $output); + } + public function testPullMultipleDatabases(): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE); $inputs = [ @@ -177,7 +186,7 @@ public function testPullDatabaseWithMySqlImportError(): void { /** * @dataProvider providerTestPullDatabaseWithInvalidSslCertificate */ - public function testPullDatabaseWithInvalidSslCertificate(mixed $errorCode): void { + public function testPullDatabaseWithInvalidSslCertificate(int $errorCode): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, $errorCode); $inputs = $this->getInputs(); @@ -187,7 +196,7 @@ public function testPullDatabaseWithInvalidSslCertificate(mixed $errorCode): voi $this->assertStringContainsString('Trying alternative host other.example.com', $output); } - protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDropSuccessful, bool $mysqlCreateSuccessful, bool $mysqlImportSuccessful, bool $mockIdeFs = FALSE, bool $onDemand = FALSE, bool $mockGetAcsfSites = TRUE, bool $multidb = FALSE, int $curlCode = 0, bool $existingBackups = TRUE): void { + protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDropSuccessful, bool $mysqlCreateSuccessful, bool $mysqlImportSuccessful, bool $mockIdeFs = FALSE, bool $onDemand = FALSE, bool $mockGetAcsfSites = TRUE, bool $multidb = FALSE, int $curlCode = 0, bool $existingBackups = TRUE, bool $pvExists = TRUE): void { $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); $environmentsResponse = $this->mockAcsfEnvironmentsRequest($applicationsResponse); @@ -231,8 +240,10 @@ protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDr // Database. $this->mockExecuteMySqlDropDb($localMachineHelper, $mysqlDropSuccessful); $this->mockExecuteMySqlCreateDb($localMachineHelper, $mysqlCreateSuccessful); - $this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful); - + $this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful, $pvExists); + if ($multidb) { + $this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful, $pvExists, 'profserv2', 'profserv2dev', 'drupal'); + } $this->command->localMachineHelper = $localMachineHelper->reveal(); $this->command->sshHelper = $sshHelper->reveal(); } @@ -291,14 +302,20 @@ protected function mockExecuteMySqlCreateDb( protected function mockExecuteMySqlImport( ObjectProphecy $localMachineHelper, - bool $success + bool $success, + bool $pvExists, + string $dbName = 'jxr5000596dev', + string $dbMachineName = 'db554675', + string $localDbName = 'jxr5000596dev' ): void { $localMachineHelper->checkRequiredBinariesExist(['gunzip', 'mysql'])->shouldBeCalled(); - $this->mockExecutePvExists($localMachineHelper); + $this->mockExecutePvExists($localMachineHelper, $pvExists); $process = $this->mockProcess($success); + $tmpDir = sys_get_temp_dir(); + $command = $pvExists ? "pv $tmpDir/dev-$dbName-$dbMachineName-2012-05-15T12:00:00Z.sql.gz --bytes --rate | gunzip | MYSQL_PWD=drupal mysql --host=localhost --user=drupal $localDbName" : "gunzip -c $tmpDir/dev-$dbName-$dbMachineName-2012-05-15T12:00:00Z.sql.gz | MYSQL_PWD=drupal mysql --host=localhost --user=drupal $localDbName"; // MySQL import command. $localMachineHelper - ->executeFromCmd(Argument::type('string'), Argument::type('callable'), + ->executeFromCmd($command, Argument::type('callable'), NULL, TRUE, NULL) ->willReturn($process->reveal()) ->shouldBeCalled();