|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Mockery as m; |
|
4
|
|
|
|
|
5
|
|
|
class JsLocalizationControllerTest extends TestCase |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
public function testCreateJsMessages() |
|
9
|
|
|
{ |
|
10
|
|
|
// Prepare & Request |
|
11
|
|
|
|
|
12
|
|
|
$this->mockLang($locale = 'en'); |
|
13
|
|
|
Artisan::call('js-localization:refresh'); |
|
14
|
|
|
|
|
15
|
|
|
$response = $this->call('GET', '/js-localization/messages'); |
|
16
|
|
|
$content = $response->getContent(); |
|
17
|
|
|
|
|
18
|
|
|
$this->assertTrue($response->isOk()); |
|
19
|
|
|
|
|
20
|
|
|
// Test for Lang.addMessages() |
|
21
|
|
|
|
|
22
|
|
|
$this->assertLangAddMessages($content, $this->testMessagesFlat); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testCreateJsConfig() |
|
26
|
|
|
{ |
|
27
|
|
|
// Prepare & Request |
|
28
|
|
|
|
|
29
|
|
|
$this->mockLang($locale = 'en'); |
|
30
|
|
|
Artisan::call('js-localization:refresh'); |
|
31
|
|
|
|
|
32
|
|
|
$response = $this->call('GET', '/js-localization/config'); |
|
33
|
|
|
$content = $response->getContent(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertTrue($response->isOk()); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertNotNull($response->getLastModified()); |
|
38
|
|
|
$this->assertNull($response->getEtag()); |
|
39
|
|
|
|
|
40
|
|
|
// Test for Config.addConfig() |
|
41
|
|
|
|
|
42
|
|
|
$this->assertConfigAddConfig($content, $this->testConfigExportFlat); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testCreateJsConfigWithCacheDisabled() |
|
46
|
|
|
{ |
|
47
|
|
|
// Prepare & Request |
|
48
|
|
|
|
|
49
|
|
|
$this->mockLang($locale = 'en'); |
|
50
|
|
|
Config::set('js-localization.disable_config_cache', true); |
|
51
|
|
|
|
|
52
|
|
|
$response = $this->call('GET', '/js-localization/config'); |
|
53
|
|
|
$content = $response->getContent(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertTrue($response->isOk()); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertNull($response->getLastModified()); |
|
58
|
|
|
$this->assertEquals('"'.md5($content).'"', $response->getEtag()); |
|
59
|
|
|
|
|
60
|
|
|
// Test for Config.addConfig() |
|
61
|
|
|
|
|
62
|
|
|
$this->assertConfigAddConfig($content, $this->testConfigExportFlat); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testBackwardsCompatibility() |
|
66
|
|
|
{ |
|
67
|
|
|
// Prepare & Request |
|
68
|
|
|
|
|
69
|
|
|
$this->mockLang($locale = 'en'); |
|
70
|
|
|
$this->mockMessageCachingService($this->testMessagesFlat['en']); |
|
71
|
|
|
|
|
72
|
|
|
$response = $this->call('GET', '/js-localization/messages'); |
|
73
|
|
|
$content = $response->getContent(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertTrue($response->isOk()); |
|
76
|
|
|
|
|
77
|
|
|
// Test for Lang.addMessages() |
|
78
|
|
|
|
|
79
|
|
|
$this->assertLangAddMessages($content, $this->testMessagesFlat); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function mockMessageCachingService(array $messages) |
|
83
|
|
|
{ |
|
84
|
|
|
$service = m::mock('CachingServiceMock'); |
|
85
|
|
|
JsLocalization\Facades\MessageCachingService::swap($service); |
|
86
|
|
|
|
|
87
|
|
|
$service->shouldReceive('getMessagesJson') |
|
88
|
|
|
->andReturn(json_encode($messages)); |
|
89
|
|
|
|
|
90
|
|
|
$service->shouldReceive('getLastRefreshTimestamp') |
|
91
|
|
|
->andReturn(new DateTime); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function assertLangAddMessages($jsContent, array $expectedMessages) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->assertJsCall($jsContent, 'Lang.addMessages', $expectedMessages); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function assertConfigAddConfig($jsContent, array $expectedConfig) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->assertJsCall($jsContent, 'Config.addConfig', $expectedConfig); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function assertJsCall($jsContent, $functionName, $functionParam) |
|
105
|
|
|
{ |
|
106
|
|
|
$functionName = str_replace('.', '\\.', $functionName); |
|
107
|
|
|
$functionNameRegex = '/' . $functionName . '\( (\{.*?\}) \);/x'; |
|
108
|
|
|
|
|
109
|
|
|
preg_match($functionNameRegex, $jsContent, $matches); |
|
110
|
|
|
$paramJson = $matches[1]; |
|
111
|
|
|
|
|
112
|
|
|
$this->assertJson($paramJson); |
|
113
|
|
|
$parsedParam = json_decode($paramJson, true); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals($functionParam, $parsedParam); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|