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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Test\Unit\Module\I18n\Parser;
use Magento\Setup\Module\I18n\Parser as Parser;
class ParserTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Setup\Module\I18n\Parser\AbstractParser|\PHPUnit_Framework_MockObject_MockObject
*/
protected $parser;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Module\I18n\FilesCollector
*/
protected $filesCollector;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Module\I18n\Factory
*/
protected $factory;
protected function setUp()
{
$this->filesCollector = $this->createMock(\Magento\Setup\Module\I18n\FilesCollector::class);
$this->factory = $this->createMock(\Magento\Setup\Module\I18n\Factory::class);
$this->parser = new Parser\Parser($this->filesCollector, $this->factory);
}
/**
* @param array $options
* @param array $phpFiles
* @param array $jsFiles
* @param array $phpMap
* @param array $jsMap
* @param array $phraseFactoryMap
* @param array $expectedResult
* @dataProvider addPhraseDataProvider
*/
public function testAddPhrase($options, $phpFiles, $jsFiles, $phpMap, $jsMap, $phraseFactoryMap, $expectedResult)
{
// 1. Create mocks
$phpAdapter = new AdapterStub;
$jsAdapter = new AdapterStub;
// 2. Set mocks
$this->parser->addAdapter('php', $phpAdapter);
$this->parser->addAdapter('js', $jsAdapter);
//3. Set fixtures
$phpAdapter->setValueMap($phpMap);
$jsAdapter->setValueMap($jsMap);
$this->factory->expects($this->any())
->method('createPhrase')
->with()
->willReturnMap($phraseFactoryMap);
//4. Set expectations
$this->filesCollector->expects($this->any())
->method('getFiles')
->will($this->returnValueMap([
[$options[0]['paths'], '', $phpFiles],
[$options[1]['paths'], '', $jsFiles],
]));
$result = $this->parser->parse($options);
$this->assertEquals($expectedResult, $result);
}
/**
* @return array
*/
public function addPhraseDataProvider()
{
$phraseMock1 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock2 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock3 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock4 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock5 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock6 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock7 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock8 = $this->createMock(\Magento\Setup\Module\I18n\Dictionary\Phrase::class);
$phraseMock1->expects($this->any())->method('getCompiledPhrase')->willReturn('php phrase111');
$phraseMock2->expects($this->any())->method('getCompiledPhrase')->willReturn('php phrase112');
$phraseMock3->expects($this->any())->method('getCompiledPhrase')->willReturn('php phrase121');
$phraseMock4->expects($this->any())->method('getCompiledPhrase')->willReturn('php phrase122');
$phraseMock5->expects($this->any())->method('getCompiledPhrase')->willReturn('js phrase111');
$phraseMock6->expects($this->any())->method('getCompiledPhrase')->willReturn('js phrase112');
$phraseMock7->expects($this->any())->method('getCompiledPhrase')->willReturn('js phrase121');
$phraseMock8->expects($this->any())->method('getCompiledPhrase')->willReturn('js phrase122');
return [
[
'options' => [
['type' => 'php', 'paths' => ['php/path/1', 'php/path/2']],
['type' => 'js', 'paths' => ['js/path/1', 'js/path/2']],
],
'phpFiles' => ['php/path1/file11', 'php/path1/file12', 'php/path2/file21'],
'jsFiles' => ['js/path1/file11', 'js/path1/file12', 'js/path2/file21'],
'phpMap' => [
'php/path1/file11' => [
[
'phrase' => 'php phrase111',
'quote' => "'"
],
[ 'phrase' => 'php phrase112',
'quote' => '"'
]
],
'php/path1/file12' => [
[
'phrase' => 'php phrase121',
'quote' => "'"
],
[ 'phrase' => 'php phrase122',
'quote' => '"'
]
],
'php/path2/file21' => []
],
'jsMap' => [
'js/path1/file11' => [
[
'phrase' => 'js phrase111',
'quote' => "'"
],
[ 'phrase' => 'js phrase112',
'quote' => '"'
]
],
'js/path1/file12' => [
[
'phrase' => 'js phrase121',
'quote' => "'"
],
[ 'phrase' => 'js phrase122',
'quote' => '"'
]
],
'js/path2/file21' => []
],
'phraseFactoryMap' => [
[['phrase' => 'php phrase111', 'translation' => 'php phrase111', 'quote' => "'"], $phraseMock1],
[['phrase' => 'php phrase112', 'translation' => 'php phrase112', 'quote' => '"'], $phraseMock2],
[['phrase' => 'php phrase121', 'translation' => 'php phrase121', 'quote' => "'"], $phraseMock3],
[['phrase' => 'php phrase122', 'translation' => 'php phrase122', 'quote' => '"'], $phraseMock4],
[['phrase' => 'js phrase111', 'translation' => 'js phrase111', 'quote' => "'"], $phraseMock5],
[['phrase' => 'js phrase112', 'translation' => 'js phrase112', 'quote' => '"'], $phraseMock6],
[['phrase' => 'js phrase121', 'translation' => 'js phrase121', 'quote' => "'"], $phraseMock7],
[['phrase' => 'js phrase122', 'translation' => 'js phrase122', 'quote' => '"'], $phraseMock8],
],
'expectedResult' => [
'php phrase111' => $phraseMock1,
'php phrase112' => $phraseMock2,
'php phrase121' => $phraseMock3,
'php phrase122' => $phraseMock4,
'js phrase111' => $phraseMock5,
'js phrase112' => $phraseMock6,
'js phrase121' => $phraseMock7,
'js phrase122' => $phraseMock8,
],
]
];
}
}
// @codingStandardsIgnoreStart
class AdapterStub implements Parser\AdapterInterface
{
/**
* @var string
*/
private $file;
/**
* @var array
*/
private $map = [];
/**
* {@inheritdoc}
*/
public function parse($file)
{
$this->file = $file;
}
/**
* {@inheritdoc}
*/
public function getPhrases()
{
return $this->map[$this->file];
}
/**
* @param array $map
*/
public function setValueMap($map)
{
$this->map = $map;
}
}