ParentTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanGetDefaultValueForEnvVar() 0 8 1
A getEnvironmentVariable() 0 23 4
1
<?php declare( strict_types=1 );
2
3
namespace Coco\SourceWatcher\Tests\Common;
4
5
use Dotenv\Dotenv;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Class ParentTest
10
 *
11
 * @package Coco\SourceWatcher\Tests\Common
12
 */
13
class ParentTest extends TestCase
14
{
15
    protected function getEnvironmentVariable ( string $variableName, $default, $castingFunctionName = null )
16
    {
17
        $keyExists = array_key_exists( $variableName, $_ENV );
18
        $value = null;
19
20
        if ( $keyExists ) {
21
            $value = $_ENV[$variableName];
22
        } else {
23
            $dotenv = Dotenv::createImmutable( __DIR__ . "/../../" );
24
            $dotenv->load();
25
26
            $value = getenv( $variableName );
27
28
            if ( empty( $value ) ) {
29
                $value = $default;
30
            }
31
        }
32
33
        if ( !empty( $castingFunctionName ) ) {
34
            return call_user_func( $castingFunctionName, $value );
35
        }
36
37
        return $value;
38
    }
39
40
    public function testCanGetDefaultValueForEnvVar () : void
41
    {
42
        $variableName = "SOMETHING_NOT_SETUP_IN_ENV";
43
        $default = "a default value";
44
        $result = $this->getEnvironmentVariable( $variableName, $default );
45
        $expected = "a default value";
46
47
        $this->assertEquals( $expected, $result );
48
    }
49
}
50