Passed
Push — master ( e67abf...41a654 )
by Jean Paul
01:31
created

i18nTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
c 3
b 0
f 0
dl 0
loc 80
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetInstance() 0 3 1
A tearDown() 0 4 2
A testGetText() 0 7 1
A setUp() 0 17 3
A testNoEnvFileDefaultToEnglish() 0 7 1
1
<?php declare( strict_types = 1 );
2
3
namespace Coco\SourceWatcher\Tests\Utils;
4
5
use Coco\SourceWatcher\Utils\i18n;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Class i18nTest
10
 * @package Coco\SourceWatcher\Tests\Utils
11
 */
12
class i18nTest extends TestCase
13
{
14
    /**
15
     * @var string
16
     */
17
    private string $envFileLocation = __DIR__ . "/../../" . ".env";
18
19
    /**
20
     * @var bool
21
     */
22
    private bool $preExistingEnvFile = false;
23
24
    /**
25
     * @var bool[][]
26
     */
27
    private array $envConditionsPerFunction = [ "testGetInstance" => [ "add_i18n_property" => true ], "testGetText" => [ "add_i18n_property" => true ], "testNoEnvFileDefaultToEnglish" => [ "add_i18n_property" => false ] ];
28
29
    /**
30
     *
31
     */
32
    public function setUp () : void
33
    {
34
        $nextFunctionToBeCalled = $this->getName();
35
36
        if ( file_exists( $this->envFileLocation ) ) {
37
            $this->preExistingEnvFile = true;
38
        } else {
39
            $addi18nProperty = $this->envConditionsPerFunction[$nextFunctionToBeCalled]["add_i18n_property"];
40
41
            $envFile = fopen( $this->envFileLocation, "w" );
42
43
            if ( $addi18nProperty ) {
44
                $i18nSetting = "I18N_LANGUAGE=en_US";
45
                fwrite( $envFile, $i18nSetting );
0 ignored issues
show
Bug introduced by
It seems like $envFile can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                fwrite( /** @scrutinizer ignore-type */ $envFile, $i18nSetting );
Loading history...
46
            }
47
48
            fclose( $envFile );
0 ignored issues
show
Bug introduced by
It seems like $envFile can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            fclose( /** @scrutinizer ignore-type */ $envFile );
Loading history...
49
        }
50
    }
51
52
    /**
53
     *
54
     */
55
    public function tearDown () : void
56
    {
57
        if ( !$this->preExistingEnvFile ) {
58
            unlink( $this->envFileLocation );
59
        }
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testGetInstance () : void
66
    {
67
        $this->assertEquals( new i18n(), i18n::getInstance() );
68
    }
69
70
    /**
71
     *
72
     */
73
    public function testGetText () : void
74
    {
75
        $expectedString = "This is a test entry!";
76
        $actualString = i18n::getInstance()->getText( i18nTest::class, "This_Is_A_Test_Entry" );
77
78
        $this->assertNotNull( $actualString );
79
        $this->assertEquals( $expectedString, $actualString );
80
    }
81
82
    /**
83
     *
84
     */
85
    public function testNoEnvFileDefaultToEnglish () : void
86
    {
87
        $expectedString = "This is a test entry!";
88
        $actualString = i18n::getInstance()->getText( i18nTest::class, "This_Is_A_Test_Entry" );
89
90
        $this->assertNotNull( $actualString );
91
        $this->assertEquals( $expectedString, $actualString );
92
    }
93
}
94