From 6753a5079726ef36c1e5cbc6c3401758194f0eb6 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Thu, 7 Mar 2024 08:54:39 -0500 Subject: [PATCH 1/2] feat: env helper added --- src/LionBundle/Helpers/Bundle/helpers.php | 16 ++++ src/LionBundle/Helpers/Env.php | 68 +++++++++++++++++ tests/Helpers/Bundle/HelpersTest.php | 10 +++ .../Helpers/HelpersProviderTrait.php | 74 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 src/LionBundle/Helpers/Env.php create mode 100644 tests/Providers/Helpers/HelpersProviderTrait.php diff --git a/src/LionBundle/Helpers/Bundle/helpers.php b/src/LionBundle/Helpers/Bundle/helpers.php index 644b6398..419de58d 100644 --- a/src/LionBundle/Helpers/Bundle/helpers.php +++ b/src/LionBundle/Helpers/Bundle/helpers.php @@ -8,6 +8,7 @@ use GuzzleHttp\Psr7\Response; use Lion\Bundle\Enums\LogTypeEnum; use Lion\Bundle\Enums\StatusResponseEnum; +use Lion\Bundle\Helpers\Env; use Lion\Files\Store; use Lion\Request\Request; use Lion\Security\JWT; @@ -259,3 +260,18 @@ function fake(string $locale = Factory::DEFAULT_LOCALE): Generator return Factory::create($locale); } } + +if (!function_exists('env')) { + /** + * Gets the value defined for an environment variable + * + * @param string $key [Property name] + * @param mixed|null $default [Default value] + * + * @return mixed + */ + function env(string $key, ?mixed $default = null): mixed + { + return Env::get($key, $default); + } +} diff --git a/src/LionBundle/Helpers/Env.php b/src/LionBundle/Helpers/Env.php new file mode 100644 index 00000000..384381c2 --- /dev/null +++ b/src/LionBundle/Helpers/Env.php @@ -0,0 +1,68 @@ +assertInstanceOf(Generator::class, fake()); } + + /** + * @dataProvider envProvider + */ + public function testEnv(string $envKey, mixed $envValue, mixed $return): void + { + $this->assertSame($return, env($envKey, $envValue)); + } } diff --git a/tests/Providers/Helpers/HelpersProviderTrait.php b/tests/Providers/Helpers/HelpersProviderTrait.php new file mode 100644 index 00000000..6fa05a8c --- /dev/null +++ b/tests/Providers/Helpers/HelpersProviderTrait.php @@ -0,0 +1,74 @@ + 'APP_NAME', + 'envValue' => 'lion-bundle', + 'return' => 'lion-bundle' + ], + [ + 'envKey' => 'SERVER_URL', + 'envValue' => 'http://127.0.0.1:8000', + 'return' => 'http://127.0.0.1:8000' + ], + [ + 'envKey' => 'APP_NAME_TEST', + 'envValue' => 'lion-bundle', + 'return' => 'lion-bundle' + ], + [ + 'envKey' => 'SERVER_URL_TEST', + 'envValue' => 'http://127.0.0.1:8000', + 'return' => 'http://127.0.0.1:8000' + ], + [ + 'envKey' => 'MAIL_DEBUG_SUPP_TEST', + 'envValue' => 'true', + 'return' => true + ], + [ + 'envKey' => 'MAIL_DEBUG_SUPP_TEST', + 'envValue' => '(true)', + 'return' => true + ], + [ + 'envKey' => 'MAIL_DEBUG_SUPP_TEST', + 'envValue' => 'false', + 'return' => false + ], + [ + 'envKey' => 'MAIL_DEBUG_SUPP_TEST', + 'envValue' => '(false)', + 'return' => false + ], + [ + 'envKey' => 'APP_DEBUG', + 'envValue' => 'empty', + 'return' => '' + ], + [ + 'envKey' => 'APP_DEBUG', + 'envValue' => '(empty)', + 'return' => '' + ], + [ + 'envKey' => 'APP_DEBUG_TEST', + 'envValue' => 'null', + 'return' => null + ], + [ + 'envKey' => 'APP_DEBUG_TEST', + 'envValue' => '(null)', + 'return' => null + ] + ]; + } +} From b0288251c48b082403c9f31b48456dbb01d45ec3 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Thu, 7 Mar 2024 09:12:14 -0500 Subject: [PATCH 2/2] feat: support for fake helper has been extended --- src/LionBundle/Helpers/Bundle/helpers.php | 7 ++-- src/LionBundle/Helpers/Env.php | 8 ++--- src/LionBundle/Helpers/Fake.php | 39 +++++++++++++++++++++++ tests/Helpers/Bundle/HelpersTest.php | 1 + 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/LionBundle/Helpers/Fake.php diff --git a/src/LionBundle/Helpers/Bundle/helpers.php b/src/LionBundle/Helpers/Bundle/helpers.php index 419de58d..2363d039 100644 --- a/src/LionBundle/Helpers/Bundle/helpers.php +++ b/src/LionBundle/Helpers/Bundle/helpers.php @@ -9,6 +9,7 @@ use Lion\Bundle\Enums\LogTypeEnum; use Lion\Bundle\Enums\StatusResponseEnum; use Lion\Bundle\Helpers\Env; +use Lion\Bundle\Helpers\Fake; use Lion\Files\Store; use Lion\Request\Request; use Lion\Security\JWT; @@ -257,7 +258,7 @@ function jwt(): array|object|string */ function fake(string $locale = Factory::DEFAULT_LOCALE): Generator { - return Factory::create($locale); + return Fake::get($locale); } } @@ -266,11 +267,11 @@ function fake(string $locale = Factory::DEFAULT_LOCALE): Generator * Gets the value defined for an environment variable * * @param string $key [Property name] - * @param mixed|null $default [Default value] + * @param mixed $default [Default value] * * @return mixed */ - function env(string $key, ?mixed $default = null): mixed + function env(string $key, mixed $default = null): mixed { return Env::get($key, $default); } diff --git a/src/LionBundle/Helpers/Env.php b/src/LionBundle/Helpers/Env.php index 384381c2..0130d1af 100644 --- a/src/LionBundle/Helpers/Env.php +++ b/src/LionBundle/Helpers/Env.php @@ -15,11 +15,11 @@ class Env * Gets the value defined for an environment variable * * @param string $key [Property name] - * @param mixed|null $default [Default value] + * @param mixed $default [Default value] * * @return mixed */ - public static function get(string $key, ?mixed $default = null): mixed + public static function get(string $key, mixed $default = null): mixed { return self::getOption($key, $default); } @@ -28,11 +28,11 @@ public static function get(string $key, ?mixed $default = null): mixed * Gets and transforms the possible value of environment variables * * @param string $key [Property name] - * @param mixed|null $default [Default value] + * @param mixed $default [Default value] * * @return mixed */ - private static function getOption(string $key, ?mixed $default): mixed + private static function getOption(string $key, mixed $default): mixed { $transform = function(mixed $value): mixed { switch ($value) { diff --git a/src/LionBundle/Helpers/Fake.php b/src/LionBundle/Helpers/Fake.php new file mode 100644 index 00000000..a9efd311 --- /dev/null +++ b/src/LionBundle/Helpers/Fake.php @@ -0,0 +1,39 @@ +assertInstanceOf(Generator::class, fake()); + $this->assertSame(fake(), fake()); } /**