Skip to content

Commit

Permalink
Adding a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash committed May 23, 2024
1 parent ad852ff commit d8cb285
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/Helpers/TelemetryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'],
];
}

}
54 changes: 54 additions & 0 deletions tests/phpunit/src/Misc/TelemetryHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Misc;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\ClearCacheCommand;
use Acquia\Cli\Helpers\TelemetryHelper;
use Acquia\Cli\Tests\CommandTestBase;
use Acquia\Cli\Tests\TestBase;

class TelemetryHelperTest extends CommandTestBase {

public function tearDown(): void {
parent::tearDown();
$envVars = [];
foreach ($this->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());
}

}

0 comments on commit d8cb285

Please sign in to comment.