5
5
*/
6
6
namespace Magento \Deploy \Test \Unit \Model ;
7
7
8
+ use Magento \Deploy \Model \Filesystem as DeployFilesystem ;
9
+ use Magento \Framework \Filesystem ;
10
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
11
+ use Magento \Framework \ObjectManagerInterface ;
12
+ use Magento \Framework \ShellInterface ;
13
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use Magento \Store \Model \Config \StoreView ;
15
+ use Magento \User \Model \ResourceModel \User \Collection ;
16
+ use Magento \User \Model \User ;
17
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
18
+ use Symfony \Component \Console \Output \OutputInterface ;
19
+ use Magento \Framework \Validator \Locale ;
20
+ use Magento \Framework \Setup \Lists ;
21
+
22
+ /**
23
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
+ */
8
25
class FilesystemTest extends \PHPUnit \Framework \TestCase
9
26
{
10
27
/**
11
- * @var \Magento\Store\Model\Config\ StoreView
28
+ * @var StoreView|MockObject
12
29
*/
13
- private $ storeViewMock ;
30
+ private $ storeView ;
14
31
15
32
/**
16
- * @var \Magento\Framework\ ShellInterface
33
+ * @var ShellInterface|MockObject
17
34
*/
18
- private $ shellMock ;
35
+ private $ shell ;
19
36
20
37
/**
21
- * @var \Magento\User\Model\ResourceModel\User\Collection
38
+ * @var OutputInterface|MockObject
22
39
*/
23
- private $ userCollectionMock ;
40
+ private $ output ;
24
41
25
42
/**
26
- * @var \Symfony\Component\Console\Output\OutputInterface
43
+ * @var Filesystem|MockObject
27
44
*/
28
- private $ outputMock ;
45
+ private $ filesystem ;
29
46
30
47
/**
31
- * @var \Magento\Framework\Filesystem
48
+ * @var WriteInterface|MockObject
32
49
*/
33
- private $ filesystemMock ;
50
+ private $ directoryWrite ;
34
51
35
52
/**
36
- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
53
+ * @var Collection|MockObject
37
54
*/
38
- private $ directoryWriteMock ;
55
+ private $ userCollection ;
39
56
40
57
/**
41
- * @var \Magento\Framework\ ObjectManagerInterface
58
+ * @var ObjectManagerInterface|MockObject
42
59
*/
43
- private $ objectManagerMock ;
60
+ private $ objectManager ;
44
61
45
62
/**
46
- * @var \Magento\Deploy\Model\Filesystem
63
+ * @var DeployFilesystem
47
64
*/
48
- private $ filesystem ;
65
+ private $ deployFilesystem ;
49
66
50
67
/**
51
68
* @var string
@@ -54,75 +71,146 @@ class FilesystemTest extends \PHPUnit\Framework\TestCase
54
71
55
72
protected function setUp ()
56
73
{
57
- $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
58
-
59
- $ this ->storeViewMock = $ this ->createMock (\Magento \Store \Model \Config \StoreView::class);
60
- $ this ->shellMock = $ this ->createMock (\Magento \Framework \ShellInterface::class);
61
- $ this ->userCollectionMock = $ this ->createMock (\Magento \User \Model \ResourceModel \User \Collection::class);
62
- $ this ->outputMock = $ this ->createMock (\Symfony \Component \Console \Output \OutputInterface::class);
63
- $ this ->objectManagerMock = $ this ->createMock (\Magento \Framework \ObjectManagerInterface::class);
64
- $ this ->filesystemMock = $ this ->createMock (\Magento \Framework \Filesystem::class);
65
- $ this ->directoryWriteMock = $ this ->createMock (\Magento \Framework \Filesystem \Directory \WriteInterface::class);
66
- $ this ->filesystemMock ->expects ($ this ->any ())
67
- ->method ('getDirectoryWrite ' )
68
- ->willReturn ($ this ->directoryWriteMock );
69
- $ this ->filesystem = $ objectManager ->getObject (
70
- \Magento \Deploy \Model \Filesystem::class,
74
+ $ objectManager = new ObjectManager ($ this );
75
+
76
+ $ this ->storeView = $ this ->getMockBuilder (StoreView::class)
77
+ ->disableOriginalConstructor ()
78
+ ->getMock ();
79
+ $ this ->shell = $ this ->getMockBuilder (ShellInterface::class)
80
+ ->disableOriginalConstructor ()
81
+ ->getMock ();
82
+ $ this ->output = $ this ->getMockBuilder (OutputInterface::class)
83
+ ->disableOriginalConstructor ()
84
+ ->getMock ();
85
+ $ this ->objectManager = $ this ->getMockBuilder (ObjectManagerInterface::class)
86
+ ->disableOriginalConstructor ()
87
+ ->getMock ();
88
+ $ this ->filesystem = $ this ->getMockBuilder (Filesystem::class)
89
+ ->disableOriginalConstructor ()
90
+ ->getMock ();
91
+ $ this ->directoryWrite = $ this ->getMockBuilder (WriteInterface::class)
92
+ ->disableOriginalConstructor ()
93
+ ->getMock ();
94
+ $ this ->filesystem ->method ('getDirectoryWrite ' )
95
+ ->willReturn ($ this ->directoryWrite );
96
+
97
+ $ this ->userCollection = $ this ->getMockBuilder (Collection::class)
98
+ ->disableOriginalConstructor ()
99
+ ->getMock ();
100
+ $ lists = $ this ->getMockBuilder (Lists::class)
101
+ ->disableOriginalConstructor ()
102
+ ->getMock ();
103
+
104
+ $ lists ->method ('getLocaleList ' )
105
+ ->willReturn ([
106
+ 'fr_FR ' => 'France ' ,
107
+ 'de_DE ' => 'Germany ' ,
108
+ 'nl_NL ' => 'Netherlands ' ,
109
+ 'en_US ' => 'USA ' ,
110
+ ]);
111
+ $ locale = $ objectManager ->getObject (Locale::class, ['lists ' => $ lists ]);
112
+
113
+ $ this ->deployFilesystem = $ objectManager ->getObject (
114
+ DeployFilesystem::class,
71
115
[
72
- 'storeView ' => $ this ->storeViewMock ,
73
- 'shell ' => $ this ->shellMock ,
74
- 'filesystem ' => $ this ->filesystemMock
116
+ 'storeView ' => $ this ->storeView ,
117
+ 'shell ' => $ this ->shell ,
118
+ 'filesystem ' => $ this ->filesystem ,
119
+ 'userCollection ' => $ this ->userCollection ,
120
+ 'locale ' => $ locale ,
75
121
]
76
122
);
77
123
78
- $ userCollection = new \ReflectionProperty (\Magento \Deploy \Model \Filesystem::class, 'userCollection ' );
79
- $ userCollection ->setAccessible (true );
80
- $ userCollection ->setValue ($ this ->filesystem , $ this ->userCollectionMock );
81
-
82
124
$ this ->cmdPrefix = PHP_BINARY . ' -f ' . BP . DIRECTORY_SEPARATOR . 'bin ' . DIRECTORY_SEPARATOR . 'magento ' ;
83
125
}
84
126
85
127
public function testRegenerateStatic ()
86
128
{
87
129
$ storeLocales = ['fr_FR ' , 'de_DE ' , 'nl_NL ' ];
88
- $ adminUserInterfaceLocales = ['de_DE ' , 'en_US ' ];
89
- $ this ->storeViewMock ->expects ($ this ->once ())
90
- ->method ('retrieveLocales ' )
130
+ $ this ->storeView ->method ('retrieveLocales ' )
91
131
->willReturn ($ storeLocales );
92
- $ userMock = $ this ->createMock (\Magento \User \Model \User::class);
93
- $ userMock ->expects ($ this ->once ())
94
- ->method ('getInterfaceLocale ' )
95
- ->willReturn ('en_US ' );
96
- $ this ->userCollectionMock ->expects ($ this ->once ())
97
- ->method ('getIterator ' )
98
- ->willReturn (new \ArrayIterator ([$ userMock ]));
99
-
100
- $ usedLocales = array_unique (
101
- array_merge ($ storeLocales , $ adminUserInterfaceLocales )
102
- );
103
- $ staticContentDeployCmd = $ this ->cmdPrefix . 'setup:static-content:deploy -f '
104
- . implode (' ' , $ usedLocales );
132
+
105
133
$ setupDiCompileCmd = $ this ->cmdPrefix . 'setup:di:compile ' ;
106
- $ this ->shellMock ->expects ($ this -> at (0 ))
134
+ $ this ->shell ->expects (self :: at (0 ))
107
135
->method ('execute ' )
108
136
->with ($ setupDiCompileCmd );
109
- $ this ->shellMock ->expects ($ this ->at (1 ))
137
+
138
+ $ this ->initAdminLocaleMock ('en_US ' );
139
+
140
+ $ usedLocales = ['fr_FR ' , 'de_DE ' , 'nl_NL ' , 'en_US ' ];
141
+ $ staticContentDeployCmd = $ this ->cmdPrefix . 'setup:static-content:deploy -f '
142
+ . implode (' ' , $ usedLocales );
143
+ $ this ->shell ->expects (self ::at (1 ))
110
144
->method ('execute ' )
111
145
->with ($ staticContentDeployCmd );
112
146
113
- $ this ->outputMock ->expects ($ this -> at (0 ))
147
+ $ this ->output ->expects (self :: at (0 ))
114
148
->method ('writeln ' )
115
149
->with ('Starting compilation ' );
116
- $ this ->outputMock ->expects ($ this -> at (2 ))
150
+ $ this ->output ->expects (self :: at (2 ))
117
151
->method ('writeln ' )
118
152
->with ('Compilation complete ' );
119
- $ this ->outputMock ->expects ($ this -> at (3 ))
153
+ $ this ->output ->expects (self :: at (3 ))
120
154
->method ('writeln ' )
121
155
->with ('Starting deployment of static content ' );
122
- $ this ->outputMock ->expects ($ this -> at (5 ))
156
+ $ this ->output ->expects (self :: at (5 ))
123
157
->method ('writeln ' )
124
158
->with ('Deployment of static content complete ' );
125
159
126
- $ this ->filesystem ->regenerateStatic ($ this ->outputMock );
160
+ $ this ->deployFilesystem ->regenerateStatic ($ this ->output );
161
+ }
162
+
163
+ /**
164
+ * Checks a case when configuration contains incorrect locale code.
165
+ *
166
+ * @return void
167
+ * @expectedException \InvalidArgumentException
168
+ * @expectedExceptionMessage ;echo argument has invalid value, run info:language:list for list of available locales
169
+ */
170
+ public function testGenerateStaticForNotAllowedStoreViewLocale ()
171
+ {
172
+ $ storeLocales = ['fr_FR ' , 'de_DE ' , ';echo ' ];
173
+ $ this ->storeView ->method ('retrieveLocales ' )
174
+ ->willReturn ($ storeLocales );
175
+
176
+ $ this ->initAdminLocaleMock ('en_US ' );
177
+
178
+ $ this ->deployFilesystem ->regenerateStatic ($ this ->output );
179
+ }
180
+
181
+ /**
182
+ * Checks as case when admin locale is incorrect.
183
+ *
184
+ * @return void
185
+ * @expectedException \InvalidArgumentException
186
+ * @expectedExceptionMessage ;echo argument has invalid value, run info:language:list for list of available locales
187
+ */
188
+ public function testGenerateStaticForNotAllowedAdminLocale ()
189
+ {
190
+ $ storeLocales = ['fr_FR ' , 'de_DE ' , 'en_US ' ];
191
+ $ this ->storeView ->method ('retrieveLocales ' )
192
+ ->willReturn ($ storeLocales );
193
+
194
+ $ this ->initAdminLocaleMock (';echo ' );
195
+
196
+ $ this ->deployFilesystem ->regenerateStatic ($ this ->output );
197
+ }
198
+
199
+ /**
200
+ * Initializes admin user locale.
201
+ *
202
+ * @param string $locale
203
+ * @return void
204
+ */
205
+ private function initAdminLocaleMock ($ locale )
206
+ {
207
+ /** @var User|MockObject $user */
208
+ $ user = $ this ->getMockBuilder (User::class)
209
+ ->disableOriginalConstructor ()
210
+ ->getMock ();
211
+ $ user ->method ('getInterfaceLocale ' )
212
+ ->willReturn ($ locale );
213
+ $ this ->userCollection ->method ('getIterator ' )
214
+ ->willReturn (new \ArrayIterator ([$ user ]));
127
215
}
128
216
}
0 commit comments