|
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 ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
fclose( $envFile ); |
|
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
|
|
|
|