3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Translation \Test \Unit \Model \Js ;
8
9
9
10
use Magento \Framework \App \State ;
10
11
use Magento \Framework \App \Utility \Files ;
11
- use Magento \Framework \Filesystem ;
12
+ use Magento \Framework \Component \ComponentRegistrar ;
13
+ use Magento \Framework \Component \DirSearch ;
14
+ use Magento \Framework \Filesystem \File \Read ;
15
+ use Magento \Framework \Filesystem \File \ReadFactory ;
12
16
use Magento \Framework \Filesystem \File \ReadInterface ;
13
- use Magento \Translation \Model \Js \DataProvider ;
14
- use Magento \Translation \Model \Js \Config ;
15
17
use Magento \Framework \Phrase \Renderer \Translate ;
18
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
19
+ use Magento \Translation \Model \Js \Config ;
20
+ use Magento \Translation \Model \Js \DataProvider as ModelDataProvider ;
21
+ use PHPUnit \Framework \TestCase ;
22
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
16
23
17
24
/**
18
25
* Verify data provider translation
19
26
*
20
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
28
*/
22
- class DataProviderTest extends \ PHPUnit \ Framework \ TestCase
29
+ class DataProviderTest extends TestCase
23
30
{
24
31
/**
25
- * @var DataProvider
32
+ * @var ModelDataProvider
26
33
*/
27
- protected $ model ;
34
+ private $ model ;
28
35
29
36
/**
30
- * @var State|\PHPUnit_Framework_MockObject_MockObject
37
+ * @var State|MockObject
31
38
*/
32
- protected $ appStateMock ;
39
+ private $ appStateMock ;
33
40
34
41
/**
35
- * @var Config|\PHPUnit_Framework_MockObject_MockObject
42
+ * @var Config|MockObject
36
43
*/
37
- protected $ configMock ;
44
+ private $ configMock ;
38
45
39
46
/**
40
- * @var Files|\PHPUnit_Framework_MockObject_MockObject
47
+ * @var Files|MockObject
41
48
*/
42
- protected $ filesUtilityMock ;
49
+ private $ filesUtilityMock ;
43
50
44
51
/**
45
- * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
52
+ * @var ReadInterface|MockObject
46
53
*/
47
- protected $ fileReadMock ;
54
+ private $ fileReadMock ;
48
55
49
56
/**
50
- * @var Translate|\PHPUnit_Framework_MockObject_MockObject
57
+ * @var Translate|MockObject
51
58
*/
52
- protected $ translateMock ;
59
+ private $ translateMock ;
53
60
54
61
/**
55
- * @return void
62
+ * @inheritDoc
56
63
*/
57
64
protected function setUp ()
58
65
{
59
- $ this ->appStateMock = $ this ->createMock (\ Magento \ Framework \ App \ State::class);
60
- $ this ->configMock = $ this ->createMock (\ Magento \ Translation \ Model \ Js \ Config::class);
61
- $ this ->filesUtilityMock = $ this ->createMock (\ Magento \ Framework \ App \ Utility \ Files::class);
62
- $ fileReadFactory = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ ReadFactory::class);
63
- $ this ->fileReadMock = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Read::class);
64
- $ this ->translateMock = $ this ->createMock (\ Magento \ Framework \ Phrase \ Renderer \ Translate::class);
66
+ $ this ->appStateMock = $ this ->createMock (State::class);
67
+ $ this ->configMock = $ this ->createMock (Config::class);
68
+ $ this ->filesUtilityMock = $ this ->createMock (Files::class);
69
+ $ fileReadFactory = $ this ->createMock (ReadFactory::class);
70
+ $ this ->fileReadMock = $ this ->createMock (Read::class);
71
+ $ this ->translateMock = $ this ->createMock (Translate::class);
65
72
$ fileReadFactory ->expects ($ this ->atLeastOnce ())
66
73
->method ('create ' )
67
74
->willReturn ($ this ->fileReadMock );
68
- $ dirSearch = $ this ->createMock (\ Magento \ Framework \ Component \ DirSearch::class);
69
- $ objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
75
+ $ dirSearch = $ this ->createMock (DirSearch::class);
76
+ $ objectManager = new ObjectManager ($ this );
70
77
$ this ->model = $ objectManager ->getObject (
71
- \ Magento \ Translation \ Model \ Js \DataProvider ::class,
78
+ ModelDataProvider ::class,
72
79
[
73
80
'appState ' => $ this ->appStateMock ,
74
81
'config ' => $ this ->configMock ,
75
82
'fileReadFactory ' => $ fileReadFactory ,
76
83
'translate ' => $ this ->translateMock ,
77
84
'dirSearch ' => $ dirSearch ,
78
85
'filesUtility ' => $ this ->filesUtilityMock ,
79
- 'componentRegistrar ' =>
80
- $ this ->createMock (\Magento \Framework \Component \ComponentRegistrar::class)
86
+ 'componentRegistrar ' => $ this ->createMock (ComponentRegistrar::class)
81
87
]
82
88
);
83
89
}
84
90
85
91
/**
92
+ * Verify data translate.
93
+ *
94
+ * @param array $config
86
95
* @return void
96
+ * @dataProvider configDataProvider
87
97
*/
88
- public function testGetData ()
98
+ public function testGetData (array $ config ): void
89
99
{
90
100
$ themePath = 'blank ' ;
91
101
$ areaCode = 'adminhtml ' ;
@@ -101,36 +111,6 @@ public function testGetData()
101
111
[$ areaCode , $ themePath , '* ' , '* ' , [$ filePaths [3 ]]]
102
112
];
103
113
104
- $ expectedResult = [
105
- 'hello1 ' => 'hello1translated ' ,
106
- 'hello2 ' => 'hello2translated ' ,
107
- 'hello3 ' => 'hello3translated ' ,
108
- 'hello4 ' => 'hello4translated ' ,
109
- 'ko i18 ' => 'ko i18 translated ' ,
110
- 'underscore i18 ' => 'underscore i18 translated ' ,
111
- ];
112
-
113
- $ contentsMap = [
114
- 'content1$.mage.__("hello1")content1 ' ,
115
- 'content2$.mage.__("hello2")content2 ' ,
116
- 'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko --> ' ,
117
- 'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %> ' ,
118
- ];
119
-
120
- $ translateMap = [
121
- [['hello1 ' ], [], 'hello1translated ' ],
122
- [['hello2 ' ], [], 'hello2translated ' ],
123
- [['hello3 ' ], [], 'hello3translated ' ],
124
- [['hello4 ' ], [], 'hello4translated ' ],
125
- [['ko i18 ' ], [], 'ko i18 translated ' ],
126
- [['underscore i18 ' ], [], 'underscore i18 translated ' ],
127
- ];
128
-
129
- $ patterns = [
130
- '~\$\.mage\.__\(([ \'"])(.+?)\1\)~ ' ,
131
- '~(?:i18n\:|_\.i18n\()\s*([" \'])(.*?)(?<! \\\\)\1~ ' ,
132
- ];
133
-
134
114
$ this ->appStateMock ->expects ($ this ->once ())
135
115
->method ('getAreaCode ' )
136
116
->willReturn ($ areaCode );
@@ -141,42 +121,45 @@ public function testGetData()
141
121
->method ('getStaticHtmlFiles ' )
142
122
->willReturnMap ($ staticFilesMap );
143
123
144
- foreach ($ contentsMap as $ index => $ content ) {
124
+ foreach ($ config [ ' contentsMap ' ] as $ index => $ content ) {
145
125
$ this ->fileReadMock ->expects ($ this ->at ($ index ))
146
126
->method ('readAll ' )
147
127
->willReturn ($ content );
148
128
}
149
129
150
130
$ this ->configMock ->expects ($ this ->any ())
151
131
->method ('getPatterns ' )
152
- ->willReturn ($ patterns );
132
+ ->willReturn ($ config [ ' patterns ' ] );
153
133
$ this ->translateMock ->expects ($ this ->any ())
154
134
->method ('render ' )
155
- ->willReturnMap ($ translateMap );
135
+ ->willReturnMap ($ config [ ' translateMap ' ] );
156
136
157
137
$ actualResult = $ this ->model ->getData ($ themePath );
158
- $ this ->assertEquals ($ expectedResult , $ actualResult );
138
+ $ this ->assertEquals ($ config [ ' expectedResult ' ] , $ actualResult );
159
139
$ this ->assertEquals (
160
- json_encode ($ expectedResult ),
140
+ json_encode ($ config [ ' expectedResult ' ] ),
161
141
json_encode ($ actualResult ),
162
142
"Translations should be sorted by key "
163
143
);
164
144
}
165
145
166
146
/**
147
+ * Verify get data throwing exception.
148
+ *
149
+ * @param array $config
150
+ * @return void
151
+ * @dataProvider configDataProvider
167
152
* @expectedException \Magento\Framework\Exception\LocalizedException
168
153
*/
169
- public function testGetDataThrowingException ()
154
+ public function testGetDataThrowingException (array $ config ): void
170
155
{
171
156
$ themePath = 'blank ' ;
172
157
$ areaCode = 'adminhtml ' ;
173
-
174
- $ patterns = ['~\$\.mage\.__\(([ \'"])(.+?)\1\)~ ' ];
158
+ $ patterns = $ config ['patterns ' ];
175
159
176
160
$ this ->fileReadMock ->expects ($ this ->once ())
177
161
->method ('readAll ' )
178
162
->willReturn ('content1$.mage.__("hello1")content1 ' );
179
-
180
163
$ this ->appStateMock ->expects ($ this ->once ())
181
164
->method ('getAreaCode ' )
182
165
->willReturn ($ areaCode );
@@ -186,15 +169,57 @@ public function testGetDataThrowingException()
186
169
$ this ->filesUtilityMock ->expects ($ this ->any ())
187
170
->method ('getStaticHtmlFiles ' )
188
171
->willReturn (['test ' ]);
189
-
190
172
$ this ->configMock ->expects ($ this ->any ())
191
173
->method ('getPatterns ' )
192
174
->willReturn ($ patterns );
193
-
194
175
$ this ->translateMock ->expects ($ this ->once ())
195
176
->method ('render ' )
196
177
->willThrowException (new \Exception ('Test exception ' ));
197
178
198
179
$ this ->model ->getData ($ themePath );
199
180
}
181
+
182
+ /**
183
+ * Config data provider.
184
+ *
185
+ * @return array
186
+ */
187
+ public function configDataProvider (): array
188
+ {
189
+ return [
190
+ [
191
+ [
192
+ 'patterns ' => [
193
+ '~\$\.mage\.__\(([ \'"])(.+?)\1\)~ ' ,
194
+ '~(?:i18n\:|_\.i18n\()\s*([" \'])(.*?)(?<! \\\\)\1~ ' ,
195
+ '~translate\=(" \')([^ \'].*?) \'\"~ ' ,
196
+ '~(?s)\$t\(\s*([ \'"])(\?\<translate\>.+?)(?<! \\\)\1\s*(*SKIP)\)(?s)~ ' ,
197
+ '~translate args\=("| \'|" \'| \\\" \')([^ \'].*?)( \'\\\"| \'"| \'|")~ ' ,
198
+ ],
199
+ 'expectedResult ' => [
200
+ 'hello1 ' => 'hello1translated ' ,
201
+ 'hello2 ' => 'hello2translated ' ,
202
+ 'hello3 ' => 'hello3translated ' ,
203
+ 'hello4 ' => 'hello4translated ' ,
204
+ 'ko i18 ' => 'ko i18 translated ' ,
205
+ 'underscore i18 ' => 'underscore i18 translated ' ,
206
+ ],
207
+ 'contentsMap ' => [
208
+ 'content1$.mage.__("hello1")content1 ' ,
209
+ 'content2$.mage.__("hello2")content2 ' ,
210
+ 'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko --> ' ,
211
+ 'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %> ' ,
212
+ ],
213
+ 'translateMap ' => [
214
+ [['hello1 ' ], [], 'hello1translated ' ],
215
+ [['hello2 ' ], [], 'hello2translated ' ],
216
+ [['hello3 ' ], [], 'hello3translated ' ],
217
+ [['hello4 ' ], [], 'hello4translated ' ],
218
+ [['ko i18 ' ], [], 'ko i18 translated ' ],
219
+ [['underscore i18 ' ], [], 'underscore i18 translated ' ],
220
+ ]
221
+ ],
222
+ ]
223
+ ];
224
+ }
200
225
}
0 commit comments