config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/EnvironmentDetectionTest.php
tests/src/Kernel/EnvironmentDetectionTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\config_preview_deploy\Kernel;
use Drupal\config_preview_deploy\ConfigVerifier;
use Drupal\KernelTests\KernelTestBase;
/**
* Test environment name detection.
*
* @group config_preview_deploy
*/
class EnvironmentDetectionTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['config_preview_deploy', 'system'];
/**
* The config verifier service.
*/
protected ConfigVerifier $configVerifier;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['config_preview_deploy']);
$this->configVerifier = $this->container->get('config_preview_deploy.config_verifier');
}
/**
* Tests PHAPP_ENV environment variable detection.
*/
public function testPhappEnvDetection(): void {
// Set PHAPP_ENV variable.
putenv('PHAPP_ENV=staging');
$environmentName = $this->configVerifier->getEnvironmentName();
$this->assertEquals('staging', $environmentName);
// Clean up.
putenv('PHAPP_ENV');
}
/**
* Tests priority order of environment variables.
*/
public function testEnvironmentVariablePriority(): void {
// Set multiple environment variables.
putenv('PHAPP_ENV=phapp-env');
putenv('LAGOON_ENVIRONMENT=lagoon-env');
putenv('PLATFORM_BRANCH=platform-env');
// PHAPP_ENV should have highest priority.
$environmentName = $this->configVerifier->getEnvironmentName();
$this->assertEquals('phapp-env', $environmentName);
// Clean up.
putenv('PHAPP_ENV');
putenv('LAGOON_ENVIRONMENT');
putenv('PLATFORM_BRANCH');
}
/**
* Tests fallback when no environment variables are set.
*/
public function testFallbackEnvironmentName(): void {
// Ensure no environment variables are set.
$envVars = [
'PHAPP_ENV',
'LAGOON_ENVIRONMENT',
'PLATFORM_BRANCH',
'PANTHEON_ENVIRONMENT',
'ACQUIA_ENVIRONMENT',
'CI_ENVIRONMENT_SLUG',
];
foreach ($envVars as $var) {
putenv($var);
}
$environmentName = $this->configVerifier->getEnvironmentName();
// Should fallback to hostname or 'local'.
$this->assertNotEmpty($environmentName);
$this->assertIsString($environmentName);
}
}
