TestCase::addTestMessage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
sh<?php
2
3
use Mockery as m;
4
5
class TestCase extends Orchestra\Testbench\TestCase
6
{
7
8
    protected $testMessagesConfig = [
9
        'test_string',
10
        'test'              // this includes all messages with key 'test.*'
11
    ];
12
    
13
    protected $testMessages = [
14
        'en' => [
15
            'test_string' => 'This is: test_string',
16
            'test' => [
17
                'nested' => [
18
                    'leaf' => 'I am deeply nested!'
19
                ],
20
                'string' => 'This is: test.string'
21
            ],
22
23
            'test.string' => 'This is: test.string'
24
        ]
25
    ];
26
27
    protected $testMessagesFlat = [
28
        'en' => [
29
            'test_string' => 'This is: test_string',
30
            'test.nested.leaf' => 'I am deeply nested!',
31
            'test.string' => 'This is: test.string'
32
        ]
33
    ];
34
35
    protected $testConfigExportFlat = [
36
        'js-localization.test-config' => 'some test property value'
37
    ];
38
39
40
    public function setUp()
41
    {
42
        parent::setUp();
43
44
        $this->updateMessagesConfig($this->testMessagesConfig);
45
        $this->updateConfigExportConfig($this->testConfigExportFlat);
46
        $this->mockLang();
47
    }
48
49
    public function tearDown()
50
    {
51
        m::close();
52
53
        parent::tearDown();
54
    }
55
56
    protected function getPackageProviders($app)
57
    {
58
        return ['JsLocalization\JsLocalizationServiceProvider'];
59
    }
60
61
    protected function updateMessagesConfig(array $config)
62
    {
63
        Config::set('js-localization.messages', $config);
64
    }
65
    
66
    protected function updateConfigExportConfig(array $configData)
67
    {
68
        Config::set('js-localization.config', array_keys($configData));
69
        
70
        foreach ($configData as $propertyName => $propertyValue) {
71
            Config::set($propertyName, $propertyValue);
72
        }
73
    }
74
75
    protected function mockLang($locale = "en")
76
    {
77
        Illuminate\Support\Facades\Lang::swap($lang = m::mock('LangMock'));
78
79
        $lang->shouldReceive('setLocale');
80
        $lang->shouldReceive('locale')->andReturn($locale);
81
82
        foreach ($this->testMessages[$locale] as $key=>$message) {
83
            $lang->shouldReceive('get')
84
                ->with($key)->andReturn($message);
85
            $lang->shouldReceive('get')
86
                ->with($key, m::any(), $locale)->andReturn($message);
87
        }
88
    }
89
90
    protected function addTestMessage($locale, $messageKey, $message)
91
    {
92
        $this->testMessagesConfig[] = $messageKey;
93
94
        $this->testMessages[$locale][$messageKey] = $message;
95
        $this->testMessagesFlat[$locale][$messageKey] = $message;
96
97
        $keys = explode('.', $messageKey);
98
        if (count($keys) == 2) {
99
            if (!isset($this->testMessages[$locale][$keys[0]])) {
100
                $this->testMessages[$locale][$keys[0]] = [];
101
            }
102
            $this->testMessages[$locale][$keys[0]][$keys[1]] = $message;
103
        }
104
105
        $this->updateMessagesConfig($this->testMessagesConfig);
106
        $this->mockLang();
107
    }
108
109
}
110