Skip to content

Commit e282771

Browse files
committed
Support Env unit testing
1 parent 47f2baf commit e282771

File tree

3 files changed

+282
-179
lines changed

3 files changed

+282
-179
lines changed

.env.testing-types

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TEST_BOOL_FALSE=false
2+
TEST_BOOL_TRUE=true
3+
TEST_STRING=string
4+
TEST_INT=123
5+
TEST_FLOAT=1.2
6+
TEST_VERSION=1.3.4

src/Support/Env.php

Lines changed: 80 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@
2323
*/
2424
class Env
2525
{
26-
/**
27-
* Represents the initialization status of the code.
28-
*
29-
* This variable is used to keep track of whether the code has been initialized
30-
* or not.
31-
*
32-
* @var bool
33-
*/
34-
public static bool $initialized = false;
35-
3626
/**
3727
* Represents the instance of the Dotenv class.
3828
*
@@ -60,19 +50,19 @@ class Env
6050
* This variable represents the value of the paths and is initially set to null.
6151
* It can be assigned a different value during the runtime of the program.
6252
*
63-
* @var string[]|null
53+
* @var string[]|string|null
6454
*/
65-
public static ?array $paths = null;
55+
public static string|array|null $paths = null;
6656

6757
/**
6858
* Represents the array of filenames.
6959
*
7060
* The $names variable is an array that holds the filenames of the ".env" file(s) to be loaded.
7161
* This variable is used as an argument in the Dotenv class to specify the filenames to load.
7262
*
73-
* @var string[]
63+
* @var string[]|string|null
7464
*/
75-
public static array $names = ['.env'];
65+
public static string|array|null $names = null;
7666

7767
/**
7868
* Represents the type of data being declared.
@@ -107,105 +97,97 @@ class Env
10797
*/
10898
public static ?string $fileEncoding = null;
10999

110-
/**
111-
* Constructs a new instance of the class.
112-
*
113-
* Initializes the class by invoking the initialize method.
114-
*
115-
* @return void
116-
*/
117-
public function __construct()
118-
{
119-
self::initialize();
120-
}
121-
122100
/**
123101
* Initializes the Dotenv instance with the specified configurations
124-
* and returns the initialized instance.
102+
* and returns the loaded instance.
125103
*
126-
* @param array|null $paths The paths to search for dotenv files.
127-
* @param array|null $names The names of the dotenv files to load.
104+
* @param string|array|null $paths The paths to search for dotenv files.
105+
* @param string|array|null $names The names of the dotenv files to load.
128106
* @param bool|null $shortCircuit Whether to stop loading dotenv files after finding the first one.
129107
* @param string|null $fileEncoding The encoding of the dotenv files.
130108
* @param string|null $type The type of dotenv files to load.
131-
* @return Dotenv The initialized Dotenv instance.
109+
* @return Dotenv The loaded Dotenv instance.
132110
*/
133-
public static function initialize(?array $paths = null, ?array $names = null, ?bool $shortCircuit = null, ?string $fileEncoding = null, ?string $type = null): Dotenv
111+
public static function load(string|array|null $paths = null, string|array|null $names = null, ?bool $shortCircuit = true, ?string $fileEncoding = null, ?string $type = null): Dotenv
134112
{
135-
if (!self::$initialized) {
136-
$type ??= self::getType();
137-
$paths ??= self::getPaths();
138-
$names ??= self::getNames();
139-
$shortCircuit ??= self::getShortCircuit();
140-
$fileEncoding ??= self::getFileEncoding();
141-
self::$dotenv = Dotenv::{'create' . $type}($paths, $names, $shortCircuit, $fileEncoding);
142-
self::$vars = self::$dotenv->load();
143-
self::$initialized = true;
144-
}
113+
self::setPaths($paths);
114+
self::setNames($names);
115+
self::setShortCircuit($shortCircuit);
116+
self::setFileEncoding($fileEncoding);
117+
self::setType($type);
118+
119+
$type ??= self::getType();
120+
$paths ??= self::getPaths();
121+
$names ??= self::getNames();
122+
$shortCircuit ??= self::getShortCircuit();
123+
$fileEncoding ??= self::getFileEncoding();
124+
125+
self::$dotenv = Dotenv::{'create' . $type}($paths, $names, $shortCircuit, $fileEncoding);
126+
self::$vars = self::$dotenv->safeLoad();
145127

146128
return self::$dotenv;
147129
}
148130

149131
/**
150132
* Retrieves an array of paths.
151-
* If the paths array is not yet created, it will be initialized and returned.
133+
* If the paths array is not yet created, it will be loaded and returned.
152134
*
153-
* @return array The array of paths.
135+
* @return string|string[]|null The array of paths.
154136
*/
155-
public static function getPaths(): array
137+
public static function getPaths(): string|array|null
156138
{
157-
if (is_null(self::$paths)) {
158-
self::$paths = [];
159-
foreach (['ENV_PATH', 'ROOT_PATH', 'APP_PATH'] as $constant) {
160-
if (defined($constant)) {
161-
$path = constant($constant);
162-
if (!is_null($path)) {
163-
self::$paths [] = $constant === 'APP_PATH' ? dirname($path) : $path;
164-
break;
165-
}
166-
}
167-
}
168-
if (empty(self::$paths)) {
169-
self::$paths [] = getcwd();
170-
}
171-
}
172139
return self::$paths;
173140
}
174141

175142
/**
176143
* Sets the paths for the application. If no paths are provided,
177144
* the paths will be set to null.
178145
*
179-
* @param array|null $paths The paths to be set for the application.
180-
* If null is provided, the paths will be set to null.
181-
* Default is null.
146+
* @param string|string[]|null $paths The paths to be set for the application.
147+
* If null is provided, the paths will be set to null.
148+
* Default is null.
182149
*
183150
* @return void
184151
*/
185-
public static function setPaths(array $paths = null): void
152+
public static function setPaths(string|array|null $paths = null): void
186153
{
154+
if (!isset($paths)) {
155+
$paths = [];
156+
foreach (['ENV_PATH', 'ROOT_PATH', 'APP_PATH'] as $constant) {
157+
if (defined($constant)) {
158+
$path = constant($constant);
159+
if (!is_null($path)) {
160+
$paths [] = $constant === 'APP_PATH' ? dirname($path) : $path;
161+
break;
162+
}
163+
}
164+
}
165+
if (empty($paths)) {
166+
$paths [] = getcwd();
167+
}
168+
}
187169
self::$paths = $paths;
188170
}
189171

190172
/**
191173
* Get .env file names
192-
* @return string[]
174+
* @return string|string[]|null
193175
*/
194-
public static function getNames(): array
176+
public static function getNames(): string|array|null
195177
{
196178
return self::$names;
197179
}
198180

199181
/**
200182
* Sets the names array. If the specified array is null, the existing names array will be cleared.
201183
*
202-
* @param string[] $names The array of names. If null, the existing names array will be cleared.
184+
* @param string|string[]|null $names The array of names. If null, the existing names array will be cleared.
203185
*
204186
* @return void
205187
*/
206-
public static function setNames(array $names): void
188+
public static function setNames(string|array|null $names): void
207189
{
208-
self::$names = $names;
190+
self::$names = $names ?? ['.env'];
209191
}
210192

211193
/**
@@ -229,7 +211,7 @@ public static function getType(): string
229211
public static function setType(?string $type = null): void
230212
{
231213
$domain = ['mutable', 'immutable', 'unsafe-mutable', 'unsafe-immutable'];
232-
self::$type = isset($type) && !in_array(strtolower($type), $domain, true) ? strtolower($type) : 'mutable';
214+
self::$type = isset($type) && in_array(strtolower($type), $domain, true) ? strtolower($type) : 'mutable';
233215
}
234216

235217
/**
@@ -249,9 +231,9 @@ public static function getShortCircuit(): bool
249231
* @param bool $shortCircuit The new value for the shortCircuit property.
250232
* @return void
251233
*/
252-
public static function setShortCircuit(bool $shortCircuit = true): void
234+
public static function setShortCircuit(?bool $shortCircuit = true): void
253235
{
254-
self::$shortCircuit = $shortCircuit;
236+
self::$shortCircuit = $shortCircuit ?? true;
255237
}
256238

257239
/**
@@ -278,50 +260,13 @@ public static function setFileEncoding(?string $fileEncoding = null): void
278260

279261
/**
280262
* Retrieves the Dotenv instance. If the instance is not yet created,
281-
* it will be initialized and returned.
263+
* it will be loaded and returned.
282264
*
283265
* @return Dotenv The Dotenv instance.
284266
*/
285267
public static function getDotenv(): Dotenv
286268
{
287-
return self::$dotenv ?? self::initialize();
288-
}
289-
290-
/**
291-
* Get or set the environment variable
292-
* Ex. (SET): $this->SET_APPLICATION_ENV('production');
293-
* Ex. (SET): self::SET_APPLICATION_ENV('production');
294-
* Ex. (SET): Env::SET_APPLICATION_ENV('production');
295-
* Ex. (GET): $this->GET_APPLICATION_ENV('development');
296-
* Ex. (GET): self::GET_APPLICATION_ENV('development');
297-
* Ex. (GET): Env::GET_APPLICATION_ENV('development');
298-
* @param string $name key to get/set
299-
* @param mixed $arguments Default fallback value for get, or value to set for set
300-
* @return mixed Return void for set, and return the environment variable value, or default value for get
301-
*/
302-
public static function call(string $name, mixed $arguments): mixed
303-
{
304-
$getSet = 'set';
305-
306-
if (str_starts_with($name, 'SET_')) {
307-
$name = substr($name, 0, 4);
308-
}
309-
310-
elseif (str_starts_with($name, 'set')) {
311-
$name = substr($name, 0, 3);
312-
}
313-
314-
elseif (str_starts_with($name, 'GET_')) {
315-
$getSet = 'get';
316-
$name = substr($name, 0, 4);
317-
}
318-
319-
elseif (str_starts_with($name, 'get')) {
320-
$getSet = 'get';
321-
$name = substr($name, 0, 3);
322-
}
323-
324-
return self::$getSet($name, array_pop($arguments));
269+
return self::$dotenv ?? self::load();
325270
}
326271

327272
/**
@@ -334,30 +279,35 @@ public static function get(string $key, mixed $default = null): mixed
334279
{
335280
self::getDotenv();
336281

337-
$ret = self::$vars[$key] ?? null;
338-
$ret ??= !is_string($default) && is_callable($default)? $default() : $default;
282+
if (!isset(self::$vars[$key])) {
283+
return $default;
284+
}
285+
286+
$value = self::$vars[$key];
287+
288+
if (!is_string($value)) {
289+
return $value;
290+
}
291+
292+
// Check for boolean values
293+
if (strtolower($value) === 'true') {
294+
return true;
295+
} elseif (strtolower($value) === 'false') {
296+
return false;
297+
}
339298

340-
if (is_string($ret)) {
341-
switch (strtolower($ret)) {
342-
case 'true':
343-
$ret = true;
344-
break;
345-
346-
case 'false':
347-
$ret = false;
348-
break;
349-
350-
case 'empty':
351-
$ret = '';
352-
break;
353-
354-
case 'null':
355-
$ret = null;
356-
break;
299+
// Check for numeric values
300+
if (is_numeric($value)) {
301+
// Floats
302+
if (str_contains($value, '.')) {
303+
return floatval($value);
357304
}
305+
306+
// Integers
307+
return intval($value);
358308
}
359309

360-
return $ret;
310+
return $value;
361311
}
362312

363313
/**
@@ -368,54 +318,5 @@ public static function get(string $key, mixed $default = null): mixed
368318
public static function set(string $key, mixed $value): void
369319
{
370320
self::$vars[$key] = $value;
371-
$_ENV[$key] = $value;
372-
}
373-
374-
/**
375-
* Return the environment variable
376-
* @param string $name Env key to fetch
377-
* @return mixed Env value
378-
*/
379-
public function __get(string $name): mixed
380-
{
381-
return self::get($name);
382-
}
383-
384-
/**
385-
* Set the environment variable
386-
* @param string $name Env key to set
387-
* @param mixed $value Value to set
388-
*/
389-
public function __set(string $name, mixed $value): void
390-
{
391-
self::set($name, $value);
392-
}
393-
394-
/**
395-
* Get or set the environment variable
396-
* Ex. (SET): self::SET_APPLICATION_ENV('production');
397-
* Ex. (SET): Env::SET_APPLICATION_ENV('production');
398-
* Ex. (GET): self::GET_APPLICATION_ENV('development');
399-
* Ex. (GET): Env::GET_APPLICATION_ENV('development');
400-
* @param $name string Key to get/set
401-
* @param $arguments array Default fallback value for get, or value to set for set
402-
* @return mixed Return void for set, and return the environment variable value, or default value for get
403-
*/
404-
public static function __callStatic(string $name, array $arguments): mixed
405-
{
406-
return self::call($name, $arguments);
407-
}
408-
409-
/**
410-
* Get or set the environment variable
411-
* Ex. (SET): $this->SET_APPLICATION_ENV('production');
412-
* Ex. (GET): $this->GET_APPLICATION_ENV('development');
413-
* @param $name string Key to get/set
414-
* @param $arguments array Default fallback value for get, or value to set for set
415-
* @return mixed Return void for set, and return the environment variable value, or default value for get
416-
*/
417-
public function __call(string $name, array $arguments): mixed
418-
{
419-
return self::call($name, $arguments);
420321
}
421322
}

0 commit comments

Comments
 (0)