-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMessageLoaderTest.php
More file actions
174 lines (141 loc) · 5.38 KB
/
MessageLoaderTest.php
File metadata and controls
174 lines (141 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace FormatPHP\Test;
use FormatPHP\Config;
use FormatPHP\Exception\InvalidArgumentException;
use FormatPHP\Exception\LocaleNotFoundException;
use FormatPHP\Format\Reader\FormatPHPReader;
use FormatPHP\Format\ReaderInterface;
use FormatPHP\Intl\Locale;
use FormatPHP\MessageCollection;
use FormatPHP\MessageInterface;
use FormatPHP\MessageLoader;
use function sprintf;
/**
* @psalm-import-type ReaderType from ReaderInterface
*/
class MessageLoaderTest extends TestCase
{
public function testExceptionWhenDirectoryIsNotValid(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(
'Messages directory "%s" is not a valid directory',
__FILE__,
));
/** @var ReaderInterface $reader */
$reader = $this->mockery(ReaderInterface::class);
new MessageLoader(
__FILE__,
new Config(new Locale('en')),
$reader,
);
}
public function testExceptionWhenUnableToFindSuitableLocale(): void
{
// Esperanto, Latin script, US region.
$locale = new Locale('eo-Latn-US');
$messagesDirectory = __DIR__ . '/fixtures/locales';
/** @var ReaderInterface $reader */
$reader = $this->mockery(ReaderInterface::class);
$loader = new MessageLoader(
$messagesDirectory,
new Config($locale),
$reader,
);
$this->expectException(LocaleNotFoundException::class);
$this->expectExceptionMessage(
'Unable to find a suitable locale for "eo-Latn-US" in ' . $messagesDirectory
. '; please set a default locale',
);
$loader->loadMessages();
}
public function testLoadMessagesFallsBackToDefaultLocale(): void
{
// Esperanto, Latin script, US region.
$locale = new Locale('eo-Latn-US');
$defaultLocale = new Locale('en');
$loader = new MessageLoader(
__DIR__ . '/fixtures/locales',
new Config($locale, $defaultLocale),
new FormatPHPReader(),
);
$collection = $loader->loadMessages();
$this->assertGreaterThanOrEqual(1, $collection->count());
$this->assertNotNull($collection['about.inspire']);
$this->assertInstanceOf(MessageInterface::class, $collection['about.inspire']);
}
public function testLoadMessagesWithFallback(): void
{
$locale = new Locale('ar-Arab-AE-VARIANT1');
$defaultLocale = new Locale('en');
$loader = new MessageLoader(
__DIR__ . '/fixtures/locales',
new Config($locale, $defaultLocale),
new FormatPHPReader(),
);
$collection = $loader->loadMessages();
$this->assertCount(1, $collection);
$this->assertNotNull($collection['about.inspire']);
$this->assertInstanceOf(MessageInterface::class, $collection['about.inspire']);
$this->assertSame(
'في Skillshare ، نقوم بتمكين الأعضاء للحصول على الإلهام.',
$collection['about.inspire']->getMessage(),
);
}
/**
* @param ReaderType $customReader
*
* @dataProvider provideCustomReader
*/
public function testLoadMessagesWithCustomReader($customReader): void
{
$locale = new Locale('ar');
$loader = new MessageLoader(
__DIR__ . '/fixtures/locales',
new Config($locale),
$customReader,
);
$collection = $loader->loadMessages();
$this->assertCount(1, $collection);
$this->assertNotNull($collection['about.inspire']);
$this->assertInstanceOf(MessageInterface::class, $collection['about.inspire']);
$this->assertSame(
'في Skillshare ، نقوم بتمكين الأعضاء للحصول على الإلهام.',
$collection['about.inspire']->getMessage(),
);
}
/**
* @return mixed[]
*/
public function provideCustomReader(): array
{
$customReader = new CustomMessageLoaderReader();
return [
['customReader' => CustomMessageLoaderReader::class],
['customReader' => $customReader],
['customReader' => fn (array $data): MessageCollection => (new FormatPHPReader())($data)],
['customReader' => [$customReader, '__invoke']],
['customReader' => __DIR__ . '/fixtures/custom-reader.php'],
['customReader' => null],
];
}
public function testLoadMessagesNormalizesFilenames(): void
{
$locale = new Locale('en-XB');
$defaultLocale = new Locale('en');
$loader = new MessageLoader(
__DIR__ . '/fixtures/locales',
new Config($locale, $defaultLocale),
new FormatPHPReader(),
);
$collection = $loader->loadMessages();
$this->assertGreaterThanOrEqual(1, $collection->count());
$this->assertNotNull($collection['about.inspire']);
$this->assertInstanceOf(MessageInterface::class, $collection['about.inspire']);
$this->assertSame(
'[!! Ḁṭ Ṡǩíííĺĺśśśḫâŕŕŕè, ẘè èṁṗṗṗŏẘèèèŕ ṁṁṁèṁḃḃḃèŕśśś ṭŏŏŏ ĝèèèṭ íííńśṗṗṗíŕèèèḋ. !!]',
$collection['about.inspire']->getMessage(),
);
}
}