-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathJsLocalizationControllerTest.php
72 lines (48 loc) · 1.97 KB
/
JsLocalizationControllerTest.php
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
<?php
use Mockery as m;
class JsLocalizationControllerTest extends TestCase
{
public function testCreateJsMessages ()
{
// Prepare & Request
$this->mockLang($locale = 'en');
Artisan::call('js-localization:refresh');
$response = $this->action('GET', 'JsLocalizationController@createJsMessages');
$content = $response->getContent();
$this->assertTrue($response->isOk());
// Test for Lang.setLocale()
$this->assertRegExp('/Lang\.setLocale\("'.$locale.'"\);/', $content);
// Test for Lang.addMessages()
$this->assertLangAddMessages($content, $this->testMessagesFlat);
}
public function testBackwardsCompatibility ()
{
// Prepare & Request
$this->mockLang($locale = 'en');
$this->mockCachingService($this->testMessagesFlat['en']);
$response = $this->action('GET', 'JsLocalizationController@createJsMessages');
$content = $response->getContent();
$this->assertTrue($response->isOk());
// Test for Lang.addMessages()
$this->assertLangAddMessages($content, $this->testMessagesFlat);
}
private function mockCachingService (array $messages)
{
$service = m::mock('CachingServiceMock');
JsLocalization\Facades\CachingService::swap($service);
$service->shouldReceive('getMessagesJson')
->andReturn(json_encode($messages));
$service->shouldReceive('getLastRefreshTimestamp')
->andReturn(time());
}
private function assertLangAddMessages ($jsContent, array $expectedMessages)
{
$addMessagesRegex = '/Lang\.addMessages\( (\{.*?\}) \);/x';
$this->assertRegExp($addMessagesRegex, $jsContent);
preg_match($addMessagesRegex, $jsContent, $matches);
$messagesJson = $matches[1];
$this->assertJson($messagesJson);
$messages = json_decode($messagesJson, true);
$this->assertEquals($expectedMessages, $messages);
}
}