From 14ac0e94d6968ddf168957dfc6bb967501052fcf Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Thu, 16 May 2024 16:42:37 -0700 Subject: [PATCH] checklist --- .../src/CloudApi/AccessTokenConnectorTest.php | 188 ------------------ tests/phpunit/src/Commands/ChecklistTest.php | 51 +++++ 2 files changed, 51 insertions(+), 188 deletions(-) delete mode 100644 tests/phpunit/src/CloudApi/AccessTokenConnectorTest.php create mode 100644 tests/phpunit/src/Commands/ChecklistTest.php diff --git a/tests/phpunit/src/CloudApi/AccessTokenConnectorTest.php b/tests/phpunit/src/CloudApi/AccessTokenConnectorTest.php deleted file mode 100644 index 8b4063fcd..000000000 --- a/tests/phpunit/src/CloudApi/AccessTokenConnectorTest.php +++ /dev/null @@ -1,188 +0,0 @@ -cloudCredentials->getCloudAccessToken()); - $connectorFactory = new ConnectorFactory( - [ - 'accessToken' => $this->cloudCredentials->getCloudAccessToken(), - 'accessTokenExpiry' => $this->cloudCredentials->getCloudAccessTokenExpiry(), - 'key' => NULL, - 'secret' => NULL, - ]); - $connector = $connectorFactory->createConnector(); - self::assertInstanceOf(AccessTokenConnector::class, $connector); - self::assertEquals(self::$accessToken, $connector->getAccessToken()->getToken()); - - $verb = 'get'; - $path = 'api'; - - // Make sure that new access tokens are fetched using the refresh token. - $mockProvider = $this->prophet->prophesize(GenericProvider::class); - $mockProvider->getAuthenticatedRequest($verb, ConnectorInterface::BASE_URI . $path, Argument::type(AccessTokenInterface::class)) - ->willReturn($this->prophet->prophesize(RequestInterface::class)->reveal()) - ->shouldBeCalled(); - $connector->setProvider($mockProvider->reveal()); - $connector->createRequest($verb, $path); - - $this->prophet->checkPredictions(); - } - - public function testTokenFile(): void { - $accessTokenExpiry = time() + 300; - $directory = [ - 'expiry' => (string) $accessTokenExpiry . "\n", - 'token' => self::$accessToken . "\n", -]; - $vfs = vfsStream::setup('root', NULL, $directory); - $tokenFile = Path::join($vfs->url(), 'token'); - $expiryFile = Path::join($vfs->url(), 'expiry'); - putenv('ACLI_ACCESS_TOKEN_FILE=' . $tokenFile); - putenv('ACLI_ACCESS_TOKEN_EXPIRY_FILE=' . $expiryFile); - self::assertEquals(self::$accessToken, $this->cloudCredentials->getCloudAccessToken()); - self::assertEquals($accessTokenExpiry, $this->cloudCredentials->getCloudAccessTokenExpiry()); - } - - public function testMissingTokenFile(): void { - $accessTokenExpiry = time() + 300; - $directory = [ - 'expiry' => (string) $accessTokenExpiry, - ]; - $vfs = vfsStream::setup('root', NULL, $directory); - $tokenFile = Path::join($vfs->url(), 'token'); - $expiryFile = Path::join($vfs->url(), 'expiry'); - putenv('ACLI_ACCESS_TOKEN_FILE=' . $tokenFile); - putenv('ACLI_ACCESS_TOKEN_EXPIRY_FILE=' . $expiryFile); - $this->expectException(AcquiaCliException::class); - $this->expectExceptionMessage('Access token file not found at ' . $tokenFile); - $this->cloudCredentials->getCloudAccessToken(); - } - - public function testMissingExpiryFile(): void { - $directory = [ - 'token' => self::$accessToken, - ]; - $vfs = vfsStream::setup('root', NULL, $directory); - $tokenFile = Path::join($vfs->url(), 'token'); - $expiryFile = Path::join($vfs->url(), 'expiry'); - putenv('ACLI_ACCESS_TOKEN_FILE=' . $tokenFile); - putenv('ACLI_ACCESS_TOKEN_EXPIRY_FILE=' . $expiryFile); - $this->expectException(AcquiaCliException::class); - $this->expectExceptionMessage('Access token expiry file not found at ' . $expiryFile); - $this->cloudCredentials->getCloudAccessTokenExpiry(); - } - - /** - * Validate that if both an access token and API key/secret pair are present, - * the pair is used. - */ - public function testConnector(): void { - self::setAccessTokenEnvVars(); - // Ensure that ACLI_ACCESS_TOKEN was used to populate the refresh token. - self::assertEquals(self::$accessToken, $this->cloudCredentials->getCloudAccessToken()); - $connectorFactory = new ConnectorFactory( - [ - 'accessToken' => $this->cloudCredentials->getCloudAccessToken(), - 'accessTokenExpiry' => $this->cloudCredentials->getCloudAccessTokenExpiry(), - 'key' => $this->cloudCredentials->getCloudKey(), - 'secret' => $this->cloudCredentials->getCloudSecret(), - ]); - $connector = $connectorFactory->createConnector(); - self::assertInstanceOf(Connector::class, $connector); - } - - public function testExpiredAccessToken(): void { - self::setAccessTokenEnvVars(TRUE); - $connectorFactory = new ConnectorFactory( - [ - 'accessToken' => $this->cloudCredentials->getCloudAccessToken(), - 'accessTokenExpiry' => $this->cloudCredentials->getCloudAccessTokenExpiry(), - 'key' => NULL, - 'secret' => NULL, - ]); - $connector = $connectorFactory->createConnector(); - self::assertInstanceOf(Connector::class, $connector); - } - - public function testConnectorConfig(): void { - self::setAccessTokenEnvVars(); - $connectorFactory = new ConnectorFactory( - [ - 'accessToken' => NULL, - 'key' => $this->cloudCredentials->getCloudKey(), - 'secret' => $this->cloudCredentials->getCloudSecret(), - ]); - $clientService = new ClientService($connectorFactory, $this->application, $this->cloudCredentials); - $client = $clientService->getClient(); - $options = $client->getOptions(); - $this->assertEquals(['User-Agent' => [0 => 'acli/UNKNOWN']], $options['headers']); - - $this->prophet->checkPredictions(); - } - - public function testIdeHeader(): void { - self::setAccessTokenEnvVars(); - IdeHelper::setCloudIdeEnvVars(); - $connectorFactory = new ConnectorFactory( - [ - 'accessToken' => NULL, - 'key' => $this->cloudCredentials->getCloudKey(), - 'secret' => $this->cloudCredentials->getCloudSecret(), - ]); - $clientService = new ClientService($connectorFactory, $this->application, $this->cloudCredentials); - $client = $clientService->getClient(); - $options = $client->getOptions(); - $this->assertEquals(['User-Agent' => [0 => 'acli/UNKNOWN'], 'X-Cloud-IDE-UUID' => IdeHelper::$remoteIdeUuid], $options['headers']); - - $this->prophet->checkPredictions(); - IdeHelper::unsetCloudIdeEnvVars(); - } - -} diff --git a/tests/phpunit/src/Commands/ChecklistTest.php b/tests/phpunit/src/Commands/ChecklistTest.php new file mode 100644 index 000000000..c1c203449 --- /dev/null +++ b/tests/phpunit/src/Commands/ChecklistTest.php @@ -0,0 +1,51 @@ +section() + // method which is only available for ConsoleOutput. Could make a custom testing + // output class with the method. + parent::setUp(); + $this->output = new ConsoleOutput(); + } + + public function testSpinner(): void { + putenv('PHPUNIT_RUNNING=1'); + $checklist = new Checklist($this->output); + $checklist->addItem('Testing!'); + + // Make the spinner spin with some output. + $outputCallback = static function (string $type, string $buffer) use ($checklist): void { + $checklist->updateProgressBar($buffer); + }; + $this->localMachineHelper->execute(['echo', 'hello world'], $outputCallback, NULL, FALSE); + + // Complete the item. + $checklist->completePreviousItem(); + $items = $checklist->getItems(); + /** @var \Symfony\Component\Console\Helper\ProgressBar $progressBar */ + $progressBar = $items[0]['spinner']->getProgressBar(); + $this->assertEquals('Testing!', $progressBar->getMessage()); + $this->assertEquals('', $progressBar->getBarCharacter()); + $this->assertEquals('⢸', $progressBar->getProgressCharacter()); + $this->assertEquals('⌛', $progressBar->getEmptyBarCharacter()); + $this->assertEquals(1, $progressBar->getBarWidth()); + $this->assertEquals('', $progressBar->getMessage('detail')); + + putenv('PHPUNIT_RUNNING'); + } + +}