Skip to content

Commit

Permalink
Merge pull request #39 from lion-packages/new
Browse files Browse the repository at this point in the history
Extended helper support
  • Loading branch information
Sleon4 authored Mar 7, 2024
2 parents c0b99d1 + b028825 commit 0539023
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/LionBundle/Helpers/Bundle/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Psr7\Response;
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;
Expand Down Expand Up @@ -256,6 +258,21 @@ function jwt(): array|object|string
*/
function fake(string $locale = Factory::DEFAULT_LOCALE): Generator
{
return Factory::create($locale);
return Fake::get($locale);
}
}

if (!function_exists('env')) {
/**
* Gets the value defined for an environment variable
*
* @param string $key [Property name]
* @param mixed $default [Default value]
*
* @return mixed
*/
function env(string $key, mixed $default = null): mixed
{
return Env::get($key, $default);
}
}
68 changes: 68 additions & 0 deletions src/LionBundle/Helpers/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Lion\Bundle\Helpers;

/**
* Environment variable manager
*
* @package Lion\Bundle\Helpers
*/
class Env
{
/**
* Gets the value defined for an environment variable
*
* @param string $key [Property name]
* @param mixed $default [Default value]
*
* @return mixed
*/
public static function get(string $key, mixed $default = null): mixed
{
return self::getOption($key, $default);
}

/**
* Gets and transforms the possible value of environment variables
*
* @param string $key [Property name]
* @param mixed $default [Default value]
*
* @return mixed
*/
private static function getOption(string $key, mixed $default): mixed
{
$transform = function(mixed $value): mixed {
switch ($value) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return null;
default:
return $value;
}
};

if (empty($_ENV[$key])) {
return $transform($default);
}

$value = $transform($_ENV[$key]);

if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
return $matches[2];
}

return $value;
}
}
39 changes: 39 additions & 0 deletions src/LionBundle/Helpers/Fake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Lion\Bundle\Helpers;

use Faker\Factory;
use Faker\Generator;

/**
* Create Generator objects with a single instance
*
* @package Lion\Bundle\Helpers
*/
class Fake
{
/**
* [Generator class object]
*
* @var Generator|null $generator
*/
private static ?Generator $generator = null;

/**
* Function that generates a Generator object to obtain fake data
*
* @param string $locale [Regional configuration]
*
* @return Generator
*/
public static function get(string $locale = Factory::DEFAULT_LOCALE): Generator
{
if (null === self::$generator) {
self::$generator = Factory::create($locale);
}

return self::$generator;
}
}
11 changes: 11 additions & 0 deletions tests/Helpers/Bundle/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
use Lion\Route\Route;
use Lion\Test\Test;
use Tests\Providers\EnviromentProviderTrait;
use Tests\Providers\Helpers\HelpersProviderTrait;

class HelpersTest extends Test
{
use EnviromentProviderTrait;
use HelpersProviderTrait;

const PATH_URL = 'storage/';
const PATH_URL_INDEX = '../storage/';
Expand Down Expand Up @@ -153,5 +155,14 @@ public function testIsSuccess(): void
public function testFake(): void
{
$this->assertInstanceOf(Generator::class, fake());
$this->assertSame(fake(), fake());
}

/**
* @dataProvider envProvider
*/
public function testEnv(string $envKey, mixed $envValue, mixed $return): void
{
$this->assertSame($return, env($envKey, $envValue));
}
}
74 changes: 74 additions & 0 deletions tests/Providers/Helpers/HelpersProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Tests\Providers\Helpers;

trait HelpersProviderTrait
{
public static function envProvider(): array
{
return [
[
'envKey' => '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
]
];
}
}

0 comments on commit 0539023

Please sign in to comment.