From d8cb285bc48fd857b8aa2bbd4284c09ee3e8c8d2 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Thu, 23 May 2024 10:20:19 -0400 Subject: [PATCH] Adding a test. --- src/Helpers/TelemetryHelper.php | 44 ++++++++------- .../phpunit/src/Misc/TelemetryHelperTest.php | 54 +++++++++++++++++++ 2 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 tests/phpunit/src/Misc/TelemetryHelperTest.php diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index eab5c33f9..82fd42ead 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -135,31 +135,15 @@ private function getTelemetryUserData(): array { return $data; } - private function getEnvironmentProvider(): ?string { - // Define the environment variables associated with each provider. - $providers = [ - // Assuming Acquia has a specific environment variable, just for example purposes. - 'acquia' => ['ACQUIA_ENVIRONMENT'], - 'bamboo' => ['BAMBOO_BUILDNUMBER'], - 'bitbucket' => ['BITBUCKET_BRANCH'], - 'circleci' => ['CIRCLECI'], - 'codebuild' => ['CODEBUILD_BUILD_ID'], - 'drone' => ['DRONE'], - 'github' => ['GITHUB_ACTIONS'], - 'gitlab' => ['GITLAB_CI'], - 'heroku' => ['HEROKU_TEST_RUN_ID'], - 'jenkins' => ['JENKINS_URL'], - 'octopus' => ['OCTOPUS_DEPLOYMENT_ID'], - 'teamcity' => ['TEAMCITY_VERSION'], - 'travis' => ['TRAVIS'], - ]; + public static function getEnvironmentProvider(): ?string { + $providers = self::getProviders(); // Check for an Acquia environment first as it uses a method call rather than getenv. if (AcquiaDrupalEnvironmentDetector::getAhEnv()) { return 'acquia'; } - // Check for CI/CD environment variables. + // Check for environment variables. foreach ($providers as $provider => $vars) { foreach ($vars as $var) { if (getenv($var) !== FALSE) @@ -216,4 +200,26 @@ private function getDefaultUserData(): array { ]; } + /** + * @return array[] + * An array of providers and their associated environment variables. + */ + public static function getProviders(): array { + // Define the environment variables associated with each provider. + return [ + 'bamboo' => ['BAMBOO_BUILDNUMBER'], + 'bitbucket' => ['BITBUCKET_BRANCH'], + 'circleci' => ['CIRCLECI'], + 'codebuild' => ['CODEBUILD_BUILD_ID'], + 'drone' => ['DRONE'], + 'github' => ['GITHUB_ACTIONS'], + 'gitlab' => ['GITLAB_CI'], + 'heroku' => ['HEROKU_TEST_RUN_ID'], + 'jenkins' => ['JENKINS_URL'], + 'octopus' => ['OCTOPUS_DEPLOYMENT_ID'], + 'teamcity' => ['TEAMCITY_VERSION'], + 'travis' => ['TRAVIS'], + ]; + } + } diff --git a/tests/phpunit/src/Misc/TelemetryHelperTest.php b/tests/phpunit/src/Misc/TelemetryHelperTest.php new file mode 100644 index 000000000..022202fd5 --- /dev/null +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -0,0 +1,54 @@ +providerTestEnvironmentProvider() as $args) { + $envVars = array_merge($envVars, $args[1]); + } + + TestBase::unsetEnvVars($envVars); + } + + protected function createCommand(): CommandBase { + return $this->injectCommand(ClearCacheCommand::class); + } + + /** + * @return array[] + */ + public function providerTestEnvironmentProvider(): array { + $providersList = TelemetryHelper::getProviders(); + $providersArray = []; + foreach ($providersList as $provider => $envVars) { + $env_vars_with_values = []; + foreach ($envVars as $var_name) { + $env_vars_with_values[$var_name] = 'test'; + } + $providersArray[] = [$provider, $env_vars_with_values]; + } + + return $providersArray; + } + + /** + * @dataProvider providerTestEnvironmentProvider() + */ + public function testEnvironmentProvider(string $provider, array $envVars): void { + TestBase::setEnvVars($envVars); + $this->assertEquals($provider, TelemetryHelper::getEnvironmentProvider()); + } + +}