From de7fb0e37bcf9911effe9cb25b2489b3f787c30a Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 1 Oct 2024 11:16:43 -0700 Subject: [PATCH] CLI-1410: self:info command (#1806) * CLI-1410: self:status command * fix tests * cleanup * coverage --- src/Command/CommandBase.php | 12 +++--- src/Command/Self/SelfInfoCommand.php | 32 ++++++++++++++++ src/Helpers/TelemetryHelper.php | 17 +++++---- tests/phpunit/src/Application/KernelTest.php | 1 + .../{ => Self}/ClearCacheCommandTest.php | 2 +- .../src/Commands/Self/SelfInfoCommandTest.php | 38 +++++++++++++++++++ .../{ => Self}/TelemetryCommandTest.php | 2 +- .../TelemetryDisableCommandTest.php | 2 +- .../{ => Self}/TelemetryEnableCommandTest.php | 2 +- 9 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 src/Command/Self/SelfInfoCommand.php rename tests/phpunit/src/Commands/{ => Self}/ClearCacheCommandTest.php (98%) create mode 100644 tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php rename tests/phpunit/src/Commands/{ => Self}/TelemetryCommandTest.php (98%) rename tests/phpunit/src/Commands/{ => Self}/TelemetryDisableCommandTest.php (95%) rename tests/phpunit/src/Commands/{ => Self}/TelemetryEnableCommandTest.php (95%) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 78262e937..a70b62b78 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -761,17 +761,19 @@ private function findCloudApplicationByGitUrl( return null; } - protected function createTable(OutputInterface $output, string $title, array $headers, mixed $widths): Table + protected function createTable(OutputInterface $output, string $title, array $headers, ?array $widths = null): Table { $terminalWidth = (new Terminal())->getWidth(); $terminalWidth *= .90; $table = new Table($output); $table->setHeaders($headers); $table->setHeaderTitle($title); - $setWidths = static function (mixed $width) use ($terminalWidth) { - return (int) ($terminalWidth * $width); - }; - $table->setColumnWidths(array_map($setWidths, $widths)); + if ($widths !== null) { + $setWidths = static function (float $width) use ($terminalWidth) { + return (int) ($terminalWidth * $width); + }; + $table->setColumnWidths(array_map($setWidths, $widths)); + } return $table; } diff --git a/src/Command/Self/SelfInfoCommand.php b/src/Command/Self/SelfInfoCommand.php new file mode 100644 index 000000000..c1a189600 --- /dev/null +++ b/src/Command/Self/SelfInfoCommand.php @@ -0,0 +1,32 @@ +createTable($output, 'Acquia CLI information', ['Property', 'Value']); + $table->addRow(['Version', $this->getApplication()->getVersion()]); + $table->addRow(['Cloud datastore', $this->datastoreCloud->filepath]); + $table->addRow(['ACLI datastore', $this->datastoreAcli->filepath]); + $table->addRow(['Telemetry enabled', var_export($this->telemetryHelper->telemetryEnabled(), true)]); + $table->addRow(['User ID', $this->telemetryHelper->getUserId()]); + foreach ($this->telemetryHelper->getTelemetryUserData() as $key => $value) { + $table->addRow([$key, $value]); + } + $table->render(); + return Command::SUCCESS; + } +} diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index e5e575a66..d306b1f65 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -39,8 +39,7 @@ public function initializeBugsnag(): void if (empty($this->bugSnagKey)) { return; } - $sendTelemetry = $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY); - if ($sendTelemetry === false) { + if (!$this->telemetryEnabled()) { return; } // It's safe-ish to make this key public. @@ -89,11 +88,10 @@ public function initializeAmplitude(): void if (empty($this->amplitudeKey)) { return; } - $sendTelemetry = $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY); $amplitude = Amplitude::getInstance(); - $amplitude->setOptOut($sendTelemetry === false); + $amplitude->setOptOut($this->telemetryEnabled()); - if ($sendTelemetry === false) { + if (!$this->telemetryEnabled()) { return; } try { @@ -109,6 +107,11 @@ public function initializeAmplitude(): void } } + public function telemetryEnabled(): bool + { + return (bool) $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY); + } + /** * @param string $ah_env * Environment name from AH_ENV. @@ -140,7 +143,7 @@ public static function normalizeAhEnv(string $ah_env): string * * @return array Telemetry user data. */ - private function getTelemetryUserData(): array + public function getTelemetryUserData(): array { $data = [ 'ah_app_uuid' => getenv('AH_APPLICATION_UUID'), @@ -179,7 +182,7 @@ public static function getEnvironmentProvider(): ?string return null; } - private function getUserId(): ?string + public function getUserId(): ?string { $user = $this->getUserData(); if ($user && isset($user['uuid'])) { diff --git a/tests/phpunit/src/Application/KernelTest.php b/tests/phpunit/src/Application/KernelTest.php index a0954e8d0..6212521c9 100644 --- a/tests/phpunit/src/Application/KernelTest.php +++ b/tests/phpunit/src/Application/KernelTest.php @@ -105,6 +105,7 @@ private function getEnd(): string remote:ssh [ssh] Use SSH to open a shell or run a command in a Cloud Platform environment self self:clear-caches [cc|cr] Clears local Acquia CLI caches + self:info Print information about the running version of Acquia CLI self:telemetry:disable [telemetry:disable] Disable anonymous sharing of usage and performance data self:telemetry:enable [telemetry:enable] Enable anonymous sharing of usage and performance data self:telemetry:toggle [telemetry] Toggle anonymous sharing of usage and performance data diff --git a/tests/phpunit/src/Commands/ClearCacheCommandTest.php b/tests/phpunit/src/Commands/Self/ClearCacheCommandTest.php similarity index 98% rename from tests/phpunit/src/Commands/ClearCacheCommandTest.php rename to tests/phpunit/src/Commands/Self/ClearCacheCommandTest.php index 3124de0b3..166cb53dc 100644 --- a/tests/phpunit/src/Commands/ClearCacheCommandTest.php +++ b/tests/phpunit/src/Commands/Self/ClearCacheCommandTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Acquia\Cli\Tests\Commands; +namespace Acquia\Cli\Tests\Commands\Self; use Acquia\Cli\Command\CommandBase; use Acquia\Cli\Command\Ide\IdeListCommand; diff --git a/tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php b/tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php new file mode 100644 index 000000000..ce6a47129 --- /dev/null +++ b/tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php @@ -0,0 +1,38 @@ +injectCommand(SelfInfoCommand::class); + } + + /** + * @throws \Exception + */ + public function testSelfInfoCommand(): void + { + $this->mockRequest('getAccount'); + $this->executeCommand(); + $output = $this->getDisplay(); + $this->assertStringContainsString('Property', $output); + $this->assertStringContainsString('--------', $output); + $this->assertStringContainsString('Version', $output); + $this->assertStringContainsString('Cloud datastore', $output); + $this->assertStringContainsString('ACLI datastore', $output); + $this->assertStringContainsString('Telemetry enabled', $output); + $this->assertStringContainsString('User ID', $output); + $this->assertStringContainsString('is_acquian', $output); + } +} diff --git a/tests/phpunit/src/Commands/TelemetryCommandTest.php b/tests/phpunit/src/Commands/Self/TelemetryCommandTest.php similarity index 98% rename from tests/phpunit/src/Commands/TelemetryCommandTest.php rename to tests/phpunit/src/Commands/Self/TelemetryCommandTest.php index 937952544..60ab9fac7 100644 --- a/tests/phpunit/src/Commands/TelemetryCommandTest.php +++ b/tests/phpunit/src/Commands/Self/TelemetryCommandTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Acquia\Cli\Tests\Commands; +namespace Acquia\Cli\Tests\Commands\Self; use Acquia\Cli\Command\App\LinkCommand; use Acquia\Cli\Command\CommandBase; diff --git a/tests/phpunit/src/Commands/TelemetryDisableCommandTest.php b/tests/phpunit/src/Commands/Self/TelemetryDisableCommandTest.php similarity index 95% rename from tests/phpunit/src/Commands/TelemetryDisableCommandTest.php rename to tests/phpunit/src/Commands/Self/TelemetryDisableCommandTest.php index 69797251a..950acc982 100644 --- a/tests/phpunit/src/Commands/TelemetryDisableCommandTest.php +++ b/tests/phpunit/src/Commands/Self/TelemetryDisableCommandTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Acquia\Cli\Tests\Commands; +namespace Acquia\Cli\Tests\Commands\Self; use Acquia\Cli\Command\CommandBase; use Acquia\Cli\Command\Self\TelemetryDisableCommand; diff --git a/tests/phpunit/src/Commands/TelemetryEnableCommandTest.php b/tests/phpunit/src/Commands/Self/TelemetryEnableCommandTest.php similarity index 95% rename from tests/phpunit/src/Commands/TelemetryEnableCommandTest.php rename to tests/phpunit/src/Commands/Self/TelemetryEnableCommandTest.php index 05dcd6cb3..41bfe6bf5 100644 --- a/tests/phpunit/src/Commands/TelemetryEnableCommandTest.php +++ b/tests/phpunit/src/Commands/Self/TelemetryEnableCommandTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Acquia\Cli\Tests\Commands; +namespace Acquia\Cli\Tests\Commands\Self; use Acquia\Cli\Command\CommandBase; use Acquia\Cli\Command\Self\TelemetryEnableCommand;