Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add env_is_... helper functions #31

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- Add utility functions to test the current environment ([#31](https://github.com/studiometa/wp-toolkit/pull/31))


## v2.1.0 - 2024.03.12

### Added
Expand Down
63 changes: 63 additions & 0 deletions src/Helpers/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,67 @@ public static function get(string $key): string
// so we use `getenv` as a fallback to try and get the value.
return $env[ $key ] ?? (string) getenv($key);
}

/**
* Get the current APP_ENV configuration.
*
* @return string
*/
private static function get_app_env(): string
{
return strtolower(self::get('APP_ENV'));
}

/**
* Test if the current environment is production.
* Both `production` and `prod` values are tested on the `APP_ENV` or
* `WP_ENV` environment variable or the `WP_ENV` constant.
*
* @return bool
*/
public static function is_prod(): bool
{
$env = self::get_app_env();
return $env === 'production' || $env === 'prod';
}

/**
* Test if the current environment is preprod.
*
* @return bool
*/
public static function is_preprod(): bool
{
return self::get_app_env() === 'preprod';
}

/**
* Test if the current environment is local.
*
* @return bool
*/
public static function is_local(): bool
{
return self::get_app_env() === 'local';
}

/**
* Test if the current environment is staging.
*
* @return bool
*/
public static function is_staging(): bool
{
return self::get_app_env() === 'staging';
}

/**
* Test if the current environment is development.
*
* @return bool
*/
public static function is_development(): bool
{
return self::get_app_env() === 'development';
}
}
40 changes: 40 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ function env(string $key): string
return Env::get($key);
}

/**
* Test if the current environment is prod.
*/
function env_is_prod(): bool
{
return Env::is_prod();
}

/**
* Test if the current environment is preprod.
*/
function env_is_preprod(): bool
{
return Env::is_preprod();
}

/**
* Test if the current environment is local.
*/
function env_is_local(): bool
{
return Env::is_local();
}

/**
* Test if the current environment is staging.
*/
function env_is_staging(): bool
{
return Env::is_staging();
}

/**
* Test if the current environment is development.
*/
function env_is_development(): bool
{
return Env::is_development();
}

/**
* Get a Request instance from the symfony/http-foundation package.
*
Expand Down
35 changes: 31 additions & 4 deletions tests/Helpers/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Studiometa\WPToolkitTest;

use WP_UnitTestCase;
use Studiometa\WPToolkit\Helpers\Env as EnvClass;
use PHPUnit\Framework\TestCase;
use Studiometa\WPToolkit\Helpers\Env;
use function Studiometa\WPToolkit\env;

/**
* EnvTest test case.
*/
class EnvTest extends WP_UnitTestCase
class EnvTest extends TestCase
{

/**
Expand All @@ -20,6 +20,33 @@ class EnvTest extends WP_UnitTestCase
public function test_type_of_request_function_helper()
{
$this->assertTrue(is_string(env('missing')));
$this->assertTrue(is_string(EnvClass::get('missing')));
$this->assertTrue(is_string(Env::get('missing')));
}

/**
* Test the `env_is_...` functions and methods.
*
* @return void
*/
public function test_env_is_functions()
{
$mapping = [
'is_local' => 'local',
'is_prod' => 'production',
'is_prod' => 'prod',
'is_preprod' => 'preprod',
'is_development' => 'development',
'is_staging' => 'staging',
];

foreach ($mapping as $name => $value) {
$fn = sprintf('\Studiometa\WPToolkit\env_%s', $name);
$_ENV['APP_ENV'] = strtolower($value);
$this->assertTrue($fn());
$this->assertTrue(Env::$name());
$_ENV['APP_ENV'] = strtoupper($value);
$this->assertTrue($fn());
$this->assertTrue(Env::$name());
}
}
}
Loading