Passed
Push — master ( 7d22f9...1460f7 )
by Jean Paul
01:51
created

InternationalizationTest::testGetText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace Coco\SourceWatcher\Tests\Utils;
4
5
use Coco\SourceWatcher\Utils\Internationalization;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Class InternationalizationTest
10
 *
11
 * @package Coco\SourceWatcher\Tests\Utils
12
 */
13
class InternationalizationTest extends TestCase
14
{
15
    private string $envFileLocation = __DIR__ . "/../../" . ".env";
16
17
    private bool $preExistingEnvFile = false;
18
19
    private string $temporaryNameForEnvFile = "";
20
21
    private bool $envFileRenamed = false;
22
23
    private array $envConditionsPerFunction = [
24
        "testGetInstance" => [ "add_i18n_property" => true ], // #NOSONAR
25
        "testGetText" => [ "add_i18n_property" => true ],
26
        "testNoEnvFileDefaultToEnglish" => [ "add_i18n_property" => false ]
27
    ];
28
29
    public function setUp () : void
30
    {
31
        $nextFunctionToBeCalled = $this->getName();
32
33
        if ( file_exists( $this->envFileLocation ) ) {
34
            $this->preExistingEnvFile = true;
35
36
            $this->temporaryNameForEnvFile = sprintf( "%s-%s", $this->envFileLocation, time() );
37
38
            if ( rename( $this->envFileLocation, $this->temporaryNameForEnvFile ) ) {
39
                $this->envFileRenamed = true;
40
41
                unset( $_ENV["I18N_LANGUAGE"] );
42
            }
43
        } else {
44
            $addi18nProperty = $this->envConditionsPerFunction[$nextFunctionToBeCalled]["add_i18n_property"];
45
46
            $envFile = fopen( $this->envFileLocation, "w" );
47
48
            if ( $addi18nProperty ) {
49
                $i18nSetting = "I18N_LANGUAGE=en_US";
50
                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

50
                fwrite( /** @scrutinizer ignore-type */ $envFile, $i18nSetting );
Loading history...
51
            }
52
53
            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

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