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 ); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
fclose( $envFile ); |
|
|
|
|
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
|
|
|
|