From 8644c92543f07993f54a54ec8978938c3c521d51 Mon Sep 17 00:00:00 2001 From: Rajeshreeputra Date: Fri, 28 Jun 2024 00:10:49 +0530 Subject: [PATCH] ACMS-3647: Add helper function to get segmented Acquia environment names. (#84) * ACMS-3417: Add helper function to get segmented Acquia environment names. * ACMS-3417: Add tests for getAhEnvGroup method. * ACMS-3647: Fix static code analysis warnings. * ACMS-3647: Fix failing tests. --------- Co-authored-by: Dane Powell --- composer.json | 18 +++++++------- src/AcquiaDrupalEnvironmentDetector.php | 33 +++++++++++++++++++++++++ tests/EnvironmentDetectorTest.php | 15 ++++++++--- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 20da03b..d38d105 100644 --- a/composer.json +++ b/composer.json @@ -13,25 +13,25 @@ "acquia/coding-standards": "^2.0.0", "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0" }, + "conflict": { + "acquia/blt": ">=12.0.0 <13.5.2" + }, + "autoload": { + "psr-4": { + "Acquia\\DrupalEnvironmentDetector\\": "src/", + "Acquia\\DrupalEnvironmentDetector\\Tests\\": "tests/" + } + }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true }, "sort-packages": true }, - "conflict": { - "acquia/blt": ">=12.0.0 <13.5.2" - }, "extra": { "branch-alias": { "dev-main": "1.x-dev" }, "phpcodesniffer-search-depth": "4" - }, - "autoload": { - "psr-4": { - "Acquia\\DrupalEnvironmentDetector\\": "src/", - "Acquia\\DrupalEnvironmentDetector\\Tests\\": "tests/" - } } } diff --git a/src/AcquiaDrupalEnvironmentDetector.php b/src/AcquiaDrupalEnvironmentDetector.php index 83e4978..e3557ca 100644 --- a/src/AcquiaDrupalEnvironmentDetector.php +++ b/src/AcquiaDrupalEnvironmentDetector.php @@ -266,4 +266,37 @@ public static function isCodeStudioEnv(): bool { return (bool) (!empty($gitlab_job_id) && !empty($gitlab_token)); } + /** + * Helper function to get environment Group. + * + * @param string $ah_env + * Environment machine name. + * + * @return string + * The environment Group. + */ + public static function getAhEnvGroup(string $ah_env): string { + if (EnvironmentNames::isAhProdEnv($ah_env)) { + return 'prod'; + } + elseif (EnvironmentNames::isAhStageEnv($ah_env)) { + return 'stage'; + } + elseif (EnvironmentNames::isAhDevEnv($ah_env)) { + return 'dev'; + } + elseif (EnvironmentNames::isAhOdeEnv($ah_env)) { + return 'ode'; + } + elseif (EnvironmentNames::isAhIdeEnv($ah_env)) { + return 'ide'; + } + elseif (self::isAhEnv()) { + return 'other_acquia_env'; + } + else { + return 'non_acquia_env'; + } + } + } diff --git a/tests/EnvironmentDetectorTest.php b/tests/EnvironmentDetectorTest.php index 15e27b1..3111ce3 100644 --- a/tests/EnvironmentDetectorTest.php +++ b/tests/EnvironmentDetectorTest.php @@ -68,6 +68,14 @@ public function testIsAhOdeEnv($ah_site_env, $expected_env) { $this::assertEquals($expected_env === 'ode', AcquiaDrupalEnvironmentDetector::isAhOdeEnv()); } + /** + * Tests EnvironmentDetector::isAcquiaLandoEnv(). + */ + public function testIsAcquiaLandoEnv() { + putenv("AH_SITE_ENVIRONMENT=LANDO"); + $this::assertEquals('lando', AcquiaDrupalEnvironmentDetector::isAcquiaLandoEnv()); + } + /** * Tests EnvironmentDetector::isAcquiaLandoEnv(). * @@ -78,9 +86,9 @@ public function testIsAhOdeEnv($ah_site_env, $expected_env) { * * @dataProvider providerTestIsEnv */ - public function testIsAcquiaLandoEnv($ah_site_env, $expected_env) { + public function testGetAhEnvGroup($ah_site_env, $expected_env) { putenv("AH_SITE_ENVIRONMENT=$ah_site_env"); - $this::assertEquals($expected_env === 'lando', AcquiaDrupalEnvironmentDetector::isAcquiaLandoEnv()); + $this::assertEquals($expected_env, AcquiaDrupalEnvironmentDetector::getAhEnvGroup($ah_site_env)); } /** @@ -105,7 +113,8 @@ public function providerTestIsEnv() { ['02live', 'prod'], ['ode1', 'ode'], ['ode2', 'ode'], - ['LANDO', 'lando'], + ['01update', 'other_acquia_env'], + ['', 'non_acquia_env'], ]; }