diff --git a/src/Command/Pull/PullCommandBase.php b/src/Command/Pull/PullCommandBase.php index f3be7792a..705c2ddcc 100644 --- a/src/Command/Pull/PullCommandBase.php +++ b/src/Command/Pull/PullCommandBase.php @@ -82,7 +82,7 @@ protected function getLocalFilesDir(string $site): string { return $this->dir . '/docroot/sites/' . $site . '/files'; } - public static function getBackupPath(mixed $environment, DatabaseResponse $database, mixed $backupResponse): string { + public static function getBackupPath(object $environment, object $database, object $backupResponse): string { // Databases have a machine name not exposed via the API; we can only // approximately reconstruct it and match the filename you'd get downloading // a backup from Cloud UI. @@ -445,7 +445,7 @@ protected function getLocalGitCommitHash(): string { return trim($process->getOutput()); } - private function promptChooseEnvironment(mixed $acquiaCloudClient, string $applicationUuid, bool $allowProduction = FALSE): EnvironmentResponse { + private function promptChooseEnvironment(Client $acquiaCloudClient, string $applicationUuid, bool $allowProduction = FALSE): EnvironmentResponse { $environmentResource = new Environments($acquiaCloudClient); $applicationEnvironments = iterator_to_array($environmentResource->getAll($applicationUuid)); $choices = []; @@ -542,7 +542,7 @@ protected function runComposerScripts(callable $outputCallback = NULL): void { $this->checklist->completePreviousItem(); } - private function determineSite(string|\AcquiaCloudApi\Response\EnvironmentResponse|array $environment, InputInterface $input): mixed { + private function determineSite(string|EnvironmentResponse|array $environment, InputInterface $input): mixed { if (isset($this->site)) { return $this->site; } @@ -710,7 +710,7 @@ protected function matchIdePhpVersion( } } - private function environmentPhpVersionMatches(\AcquiaCloudApi\Response\EnvironmentResponse $environment): bool { + private function environmentPhpVersionMatches(EnvironmentResponse $environment): bool { $currentPhpVersion = $this->getIdePhpVersion(); return $environment->configuration->php->version === $currentPhpVersion; } @@ -787,9 +787,9 @@ protected function getHostFromDatabaseResponse(mixed $environment, DatabaseRespo private function getDatabaseBackup( Client $acquiaCloudClient, - string|\AcquiaCloudApi\Response\EnvironmentResponse|array $environment, + string|EnvironmentResponse|array $environment, DatabaseResponse $database - ): mixed { + ): BackupResponse { $databaseBackups = new DatabaseBackups($acquiaCloudClient); $backupsResponse = $databaseBackups->getAll($environment->uuid, $database->name); if (!count($backupsResponse)) { diff --git a/tests/phpunit/src/CommandTestBase.php b/tests/phpunit/src/CommandTestBase.php index 9593655ea..4218ffb2e 100644 --- a/tests/phpunit/src/CommandTestBase.php +++ b/tests/phpunit/src/CommandTestBase.php @@ -192,6 +192,29 @@ protected function mockReadIdePhpVersion(string $phpVersion = '7.1'): LocalMachi return $localMachineHelper; } + /** + * @return array + */ + protected static function inputChooseEnvironment(): array { + return [ + // 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, + ]; + } + + public function mockGetEnvironment(): mixed { + $applications = $this->mockRequest('getApplications'); + $application = $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $environments = $this->mockRequest('getApplicationEnvironments', $application->uuid); + return $environments[self::$INPUT_DEFAULT_CHOICE]; + } + protected function mockLocalMachineHelper(): LocalMachineHelper|ObjectProphecy { $localMachineHelper = $this->prophet->prophesize(LocalMachineHelper::class); $localMachineHelper->useTty()->willReturn(FALSE); diff --git a/tests/phpunit/src/Commands/App/LogTailCommandTest.php b/tests/phpunit/src/Commands/App/LogTailCommandTest.php index 71ddb48cd..37e4c3221 100644 --- a/tests/phpunit/src/Commands/App/LogTailCommandTest.php +++ b/tests/phpunit/src/Commands/App/LogTailCommandTest.php @@ -18,10 +18,7 @@ protected function createCommand(): Command { } public function testLogTailCommand(): void { - - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); - $this->mockRequest('getApplicationEnvironments', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $this->mockGetEnvironment(); $this->mockLogStreamRequest(); $this->executeCommand([], [ // Would you like Acquia CLI to search for a Cloud application that matches your local git config? diff --git a/tests/phpunit/src/Commands/Pull/PullCodeCommandTest.php b/tests/phpunit/src/Commands/Pull/PullCodeCommandTest.php index ce840b99f..32a30a040 100644 --- a/tests/phpunit/src/Commands/Pull/PullCodeCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullCodeCommandTest.php @@ -28,16 +28,14 @@ public function testCloneRepo(): void { $this->acliRepoRoot = ''; $this->command = $this->createCommand(); // Client responses. - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); - $environments = $this->mockRequest('getApplicationEnvironments', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $environment = $this->mockGetEnvironment(); $localMachineHelper = $this->mockReadIdePhpVersion(); $process = $this->mockProcess(); $dir = Path::join($this->vfsRoot->url(), 'empty-dir'); mkdir($dir); $localMachineHelper->checkRequiredBinariesExist(["git"])->shouldBeCalled(); - $this->mockExecuteGitClone($localMachineHelper, $environments[self::$INPUT_DEFAULT_CHOICE], $process, $dir); - $this->mockExecuteGitCheckout($localMachineHelper, $environments[self::$INPUT_DEFAULT_CHOICE]->vcs->path, $dir, $process); + $this->mockExecuteGitClone($localMachineHelper, $environment, $process, $dir); + $this->mockExecuteGitCheckout($localMachineHelper, $environment->vcs->path, $dir, $process); $localMachineHelper->getFinder()->willReturn(new Finder()); $this->command->localMachineHelper = $localMachineHelper->reveal(); @@ -60,9 +58,7 @@ public function testCloneRepo(): void { } public function testPullCode(): void { - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); - $environments = $this->mockRequest('getApplicationEnvironments', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $environment = $this->mockGetEnvironment(); $this->createMockGitConfigFile(); $localMachineHelper = $this->mockReadIdePhpVersion(); @@ -72,23 +68,12 @@ public function testPullCode(): void { $this->command->localMachineHelper = $localMachineHelper->reveal(); $process = $this->mockProcess(); - $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environments[self::$INPUT_DEFAULT_CHOICE]->vcs->path); + $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environment->vcs->path); $this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir); - $inputs = [ - // 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, - ]; - $this->executeCommand([ '--no-scripts' => TRUE, - ], $inputs); + ], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); @@ -100,9 +85,7 @@ public function testPullCode(): void { public function testWithScripts(): void { touch(Path::join($this->projectDir, 'composer.json')); - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); - $environments = $this->mockRequest('getApplicationEnvironments', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $environment = $this->mockGetEnvironment(); $this->createMockGitConfigFile(); $localMachineHelper = $this->mockReadIdePhpVersion(); @@ -112,7 +95,7 @@ public function testWithScripts(): void { $this->command->localMachineHelper = $localMachineHelper->reveal(); $process = $this->mockProcess(); - $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environments[self::$INPUT_DEFAULT_CHOICE]->vcs->path); + $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environment->vcs->path); $this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir); $process = $this->mockProcess(); $this->mockExecuteComposerExists($localMachineHelper); @@ -121,18 +104,7 @@ public function testWithScripts(): void { $this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir); $this->mockExecuteDrushCacheRebuild($localMachineHelper, $process); - $inputs = [ - // 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, - ]; - - $this->executeCommand([], $inputs); + $this->executeCommand([], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); @@ -143,9 +115,7 @@ public function testWithScripts(): void { } public function testNoComposerJson(): void { - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); - $environments = $this->mockRequest('getApplicationEnvironments', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid); + $environment = $this->mockGetEnvironment(); $this->createMockGitConfigFile(); $localMachineHelper = $this->mockReadIdePhpVersion(); @@ -155,25 +125,14 @@ public function testNoComposerJson(): void { $this->command->localMachineHelper = $localMachineHelper->reveal(); $process = $this->mockProcess(); - $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environments[self::$INPUT_DEFAULT_CHOICE]->vcs->path); + $this->mockExecuteGitFetchAndCheckout($localMachineHelper, $process, $this->projectDir, $environment->vcs->path); $this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir); $process = $this->mockProcess(); $this->mockExecuteDrushExists($localMachineHelper); $this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir); $this->mockExecuteDrushCacheRebuild($localMachineHelper, $process); - $inputs = [ - // 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, - ]; - - $this->executeCommand([], $inputs); + $this->executeCommand([], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); $this->assertStringContainsString('composer.json file not found. Skipping composer install.', $output); @@ -204,18 +163,7 @@ public function testNoComposer(): void { $this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir); $this->mockExecuteDrushCacheRebuild($localMachineHelper, $process); - $inputs = [ - // 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, - ]; - - $this->executeCommand([], $inputs); + $this->executeCommand([], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); @@ -245,18 +193,7 @@ public function testWithVendorDir(): void { $this->mockExecuteDrushStatus($localMachineHelper, TRUE, $this->projectDir); $this->mockExecuteDrushCacheRebuild($localMachineHelper, $process); - $inputs = [ - // 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, - ]; - - $this->executeCommand([], $inputs); + $this->executeCommand([], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); diff --git a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php index 43de15c60..40115f7a2 100644 --- a/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php +++ b/tests/phpunit/src/Commands/Pull/PullDatabaseCommandTest.php @@ -8,7 +8,6 @@ use Acquia\Cli\Command\Pull\PullDatabaseCommand; use Acquia\Cli\Exception\AcquiaCliException; use Acquia\Cli\Helpers\LocalMachineHelper; -use AcquiaCloudApi\Response\DatabaseResponse; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Uri; use Prophecy\Argument; @@ -47,42 +46,30 @@ protected function createCommand(): Command { } public function testPullDatabases(): void { - $applications = $this->mockRequest('getApplications'); - $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid) - $environmentsResponse = $this->mockAcsfEnvironmentsRequest($applications); - $selectedEnvironment = $environmentsResponse->_embedded->items[0]; - $this->createMockGitConfigFile(); - - $databasesResponse = $this->mockAcsfDatabasesResponse($selectedEnvironment); - $databaseResponse = $databasesResponse[array_search('jxr5000596dev', array_column($databasesResponse, 'name'), TRUE)]; - $databaseBackupsResponse = $this->mockDatabaseBackupsResponse($selectedEnvironment, $databaseResponse->name, 1); - $this->mockDownloadBackup($databaseResponse, $selectedEnvironment, $databaseBackupsResponse->_embedded->items[0]); - - $sshHelper = $this->mockSshHelper(); - $this->mockGetAcsfSites($sshHelper); - - $fs = $this->prophet->prophesize(Filesystem::class); $localMachineHelper = $this->mockLocalMachineHelper(); $this->mockExecuteMySqlConnect($localMachineHelper, TRUE); - // Set up file system. - $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); - - // Mock IDE filesystem. - $this->mockDrupalSettingsRefresh($localMachineHelper); - $this->mockSettingsFiles($fs); - - // Database. - $this->mockExecuteMySqlListTables($localMachineHelper); - $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); - $this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE); - $this->command->localMachineHelper = $localMachineHelper->reveal(); + $environment = $this->mockGetEnvironment(); + $sshHelper = $this->mockSshHelper(); $this->command->sshHelper = $sshHelper->reveal(); - - $inputs = $this->getInputs(); + $process = $this->mockProcess(); + $process->getOutput()->willReturn('default')->shouldBeCalled(); + $sshHelper->executeCommand(Argument::type('object'), ['ls', '/mnt/files/site.dev/sites'], FALSE) + ->willReturn($process->reveal())->shouldBeCalled(); + $databases = $this->mockRequest('getEnvironmentsDatabases', $environment->id); + $tamper = function ($backups): void { + $backups[0]->completedAt = $backups[0]->completed_at; + }; + $backups = $this->mockRequest('getEnvironmentsDatabaseBackups', [$environment->id, 'my_db'], NULL, NULL, $tamper); + $this->mockDownloadBackup($databases[0], $environment, $backups[0]); + $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); + $this->mockExecuteMySqlDropDb($localMachineHelper, TRUE); + $this->mockExecuteMySqlImport($localMachineHelper, TRUE, TRUE, 'my_db', 'my_dbdev', 'drupal'); + $fs = $this->prophet->prophesize(Filesystem::class); + $localMachineHelper->getFilesystem()->willReturn($fs)->shouldBeCalled(); $this->executeCommand([ '--no-scripts' => TRUE, - ], $inputs); + ], self::inputChooseEnvironment()); $this->prophet->checkPredictions(); $output = $this->getDisplay(); @@ -90,25 +77,24 @@ public function testPullDatabases(): void { $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 site [jxr5000596dev (oracletest1.dev-profserv2.acsitefactory.com)]:', $output); - $this->assertStringContainsString('jxr5000596dev (oracletest1.dev-profserv2.acsitefactory.com)', $output); + $this->assertStringContainsString('Choose a database [my_db (default)]:', $output); $this->assertStringContainsString('Downloading backup 1', $output); } public function testPullDatabasesLocalConnectionFailure(): void { - $this->setupPullDatabase(FALSE, TRUE, TRUE, TRUE); - $inputs = $this->getInputs(); + $localMachineHelper = $this->mockLocalMachineHelper(); + $this->mockExecuteMySqlConnect($localMachineHelper, FALSE); $this->expectException(AcquiaCliException::class); $this->expectExceptionMessage('Unable to connect'); $this->executeCommand([ '--no-scripts' => TRUE, - ], $inputs); + ], self::inputChooseEnvironment()); } public function testPullDatabaseNoPv(): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, 0, TRUE, FALSE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->executeCommand(['--no-scripts' => TRUE], $inputs); $output = $this->getDisplay(); @@ -140,7 +126,7 @@ public function testPullMultipleDatabases(): void { public function testPullDatabasesOnDemand(): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, TRUE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->executeCommand([ '--no-scripts' => TRUE, @@ -159,7 +145,7 @@ public function testPullDatabasesOnDemand(): void { public function testPullDatabasesNoExistingBackup(): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, 0, FALSE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->executeCommand([ '--no-scripts' => TRUE, @@ -178,7 +164,7 @@ public function testPullDatabasesNoExistingBackup(): void { public function testPullDatabasesSiteArgument(): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->executeCommand([ '--no-scripts' => TRUE, @@ -196,7 +182,7 @@ public function testPullDatabasesSiteArgument(): void { public function testPullDatabaseWithMySqlDropError(): void { $this->setupPullDatabase(TRUE, FALSE, TRUE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->expectException(AcquiaCliException::class); $this->expectExceptionMessage('Unable to drop tables from database'); $this->executeCommand(['--no-scripts' => TRUE], $inputs); @@ -204,7 +190,7 @@ public function testPullDatabaseWithMySqlDropError(): void { public function testPullDatabaseWithMySqlImportError(): void { $this->setupPullDatabase(TRUE, TRUE, FALSE); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->expectException(AcquiaCliException::class); $this->expectExceptionMessage('Unable to import local database'); @@ -216,7 +202,7 @@ public function testPullDatabaseWithMySqlImportError(): void { */ public function testPullDatabaseWithInvalidSslCertificate(int $errorCode): void { $this->setupPullDatabase(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, $errorCode); - $inputs = $this->getInputs(); + $inputs = PullDatabaseCommandTest::inputChooseEnvironment(); $this->executeCommand(['--no-scripts' => TRUE], $inputs); $output = $this->getDisplay(); @@ -273,10 +259,15 @@ protected function setupPullDatabase(bool $mysqlConnectSuccessful, bool $mysqlDr $this->mockExecuteMySqlListTables($localMachineHelper, 'drupal'); $this->mockExecuteMySqlImport($localMachineHelper, $mysqlImportSuccessful, $pvExists, 'profserv2', 'profserv2dev', 'drupal'); } - $this->command->localMachineHelper = $localMachineHelper->reveal(); $this->command->sshHelper = $sshHelper->reveal(); } + protected function mockLocalMachineHelper(): LocalMachineHelper|ObjectProphecy { + $localMachineHelper = parent::mockLocalMachineHelper(); + $this->command->localMachineHelper = $localMachineHelper->reveal(); + return $localMachineHelper; + } + protected function mockExecuteMySqlConnect( ObjectProphecy $localMachineHelper, bool $success @@ -362,22 +353,6 @@ protected function mockDownloadMySqlDump(ObjectProphecy $localMachineHelper, mix ->shouldBeCalled(); } - /** - * @return array - */ - protected function getInputs(): array { - return [ - // 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, - ]; - } - protected function mockSettingsFiles(ObjectProphecy $fs): void { $fs->remove(Argument::type('string')) ->willReturn() @@ -399,7 +374,7 @@ public function testDownloadProgressDisplay(): void { $this->assertStringContainsString('100/100 [============================] 100%', $output->fetch()); } - protected function mockDownloadBackup(DatabaseResponse $selectedDatabase, object $selectedEnvironment, object $selectedBackup, int $curlCode = 0): DatabaseResponse { + protected function mockDownloadBackup(object $selectedDatabase, object $selectedEnvironment, object $selectedBackup, int $curlCode = 0): object { if ($curlCode) { $this->prophet->prophesize(StreamInterface::class); /** @var RequestException|ObjectProphecy $requestException */ diff --git a/tests/phpunit/src/TestBase.php b/tests/phpunit/src/TestBase.php index a9329ba0f..707b77d0a 100644 --- a/tests/phpunit/src/TestBase.php +++ b/tests/phpunit/src/TestBase.php @@ -22,6 +22,7 @@ use AcquiaCloudApi\Exception\ApiErrorException; use AcquiaCloudApi\Response\IdeResponse; use AcquiaLogstream\LogstreamManager; +use Closure; use GuzzleHttp\Psr7\Response; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use org\bovigo\vfs\vfsStream; @@ -31,6 +32,7 @@ use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophet; use Psr\Http\Message\StreamInterface; +use RuntimeException; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Adapter\PhpArrayAdapter; use Symfony\Component\Cache\CacheItem; @@ -431,12 +433,21 @@ protected function createMockCloudConfigFile(mixed $defaultValues = []): void { $this->fs->dumpFile($filepath, $contents); } - protected function createMockAcliConfigFile(mixed $cloudAppUuid): void { + protected function createMockAcliConfigFile(string $cloudAppUuid): void { $this->datastoreAcli->set('cloud_app_uuid', $cloudAppUuid); } - protected function mockRequest(string $operationId, string|array|null $params = NULL, ?array $body = NULL, ?string $exampleResponse = NULL, \Closure $tamper = NULL): object|array { + protected function mockRequest(string $operationId, string|array|null $params = NULL, ?array $body = NULL, ?string $exampleResponse = NULL, Closure $tamper = NULL): object|array { + if (is_string($params)) { + $params = [$params]; + } + else if (is_null($params)) { + $params = []; + } [$path, $method, $code] = $this->getPathMethodCodeFromSpec($operationId); + if (count($params) !== substr_count($path, '{')) { + throw new RuntimeException('Invalid number of parameters'); + } $response = $this->getMockResponseFromSpec($path, $method, $code); // This is a set of example responses. @@ -450,13 +461,8 @@ protected function mockRequest(string $operationId, string|array|null $params = if (isset($tamper)) { $tamper($response); } - if (isset($params)) { - if (is_string($params)) { - $params = [$params]; - } - foreach ($params as $param) { - $path = preg_replace('/\{\w*}/', $param, $path, 1); - } + foreach ($params as $param) { + $path = preg_replace('/\{\w*}/', $param, $path, 1); } $this->clientProphecy->request($method, $path, $body) ->willReturn($response)