8
8
use Magento \Framework \Filesystem \Directory \ReadFactory ;
9
9
use Magento \Framework \Filesystem \Directory \ReadInterface ;
10
10
use Magento \Framework \Filesystem \DriverPool ;
11
+ use Magento \Framework \Filesystem \File \Read ;
12
+ use Magento \Framework \Filesystem \File \Write ;
11
13
use Magento \Framework \Filesystem \File \WriteFactory ;
14
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
15
+ use PHPUnit \Framework \MockObject \MockObject ;
16
+ use PHPUnit \Framework \TestCase ;
12
17
13
- class VsCodeTest extends \ PHPUnit \ Framework \ TestCase
18
+ class VsCodeTest extends TestCase
14
19
{
15
20
/**
16
- * @var Magento\Developer\Model\XmlCatalog\Format\ VsCode
21
+ * @var VsCode
17
22
*/
18
- protected $ vscodeFormat ;
23
+ private $ vscodeFormat ;
19
24
20
25
/**
21
- * @var Magento\Framework\Filesystem\Directory\ ReadFactory
26
+ * @var MockObject| ReadFactory
22
27
*/
23
- protected $ readFactory ;
28
+ private $ readFactoryMock ;
24
29
25
30
/**
26
- * @var Magento\Framework\Filesystem\File\ WriteFactory
31
+ * @var MockObject| WriteFactory
27
32
*/
28
- protected $ fileWriteFactory ;
33
+ private $ fileWriteFactoryMock ;
29
34
30
35
/**
31
- * @var Magento\Framework\DomDocument\ DomDocumentFactory
36
+ * @var DomDocumentFactory
32
37
*/
33
- protected $ domFactory ;
38
+ private $ domFactory ;
34
39
35
- protected $ dictionary = [
36
- 'urn:magento:framework:Acl/etc/acl.xsd ' => 'vendor/magento/framework/Acl/etc/acl.xsd ' ,
37
- 'urn:magento:module:Magento_Store:etc/config.xsd ' => 'vendor/magento/module-store/etc/config.xsd ' ,
38
- 'urn:magento:module:Magento_Cron:etc/crontab.xsd ' => 'vendor/magento/module-cron/etc/crontab.xsd ' ,
39
- 'urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd ' => 'vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd ' ,
40
- ];
40
+ /**
41
+ * @var ObjectManager
42
+ */
43
+ private $ objectManagerHelper ;
41
44
42
45
public function setUp ()
43
46
{
47
+ $ this ->objectManagerHelper = new ObjectManager ($ this );
44
48
45
- $ currentDirRead = $ this ->createMock (ReadInterface::class);
46
- $ currentDirRead ->expects ($ this ->any ())
49
+ $ currentDirReadMock = $ this ->createMock (ReadInterface::class);
50
+ $ currentDirReadMock ->expects ($ this ->any ())
47
51
->method ('getRelativePath ' )
48
52
->willReturnCallback (function ($ xsdPath ) {
49
53
return $ xsdPath ;
50
54
});
51
55
52
- $ this ->readFactory = $ this ->createMock (ReadFactory::class);
53
- $ this ->readFactory ->expects ($ this ->once ())
56
+ $ this ->readFactoryMock = $ this ->createMock (ReadFactory::class);
57
+ $ this ->readFactoryMock ->expects ($ this ->once ())
54
58
->method ('create ' )
55
59
->withAnyParameters ()
56
- ->willReturn ($ currentDirRead );
57
-
58
- $ this ->fileWriteFactory = $ this ->createMock (WriteFactory::class);
59
- $ this ->domFactory = new DomDocumentFactory ();
60
-
61
- $ this ->vscodeFormat = new VsCode (
62
- $ this ->readFactory ,
63
- $ this ->fileWriteFactory ,
64
- $ this ->domFactory
60
+ ->willReturn ($ currentDirReadMock );
61
+
62
+ $ this ->fileWriteFactoryMock = $ this ->createMock (WriteFactory::class);
63
+ $ this ->domFactory = $ this ->objectManagerHelper ->getObject (DomDocumentFactory::class);
64
+
65
+ $ vscodeFormatArgs = $ this ->objectManagerHelper ->getConstructArguments (
66
+ VsCode::class,
67
+ [
68
+ 'readFactory ' => $ this ->readFactoryMock ,
69
+ 'fileWriteFactory ' => $ this ->fileWriteFactoryMock ,
70
+ 'domDocumentFactory ' => $ this ->domFactory ,
71
+ ]
65
72
);
73
+
74
+ $ this ->vscodeFormat = $ this ->objectManagerHelper ->getObject (VsCode::class, $ vscodeFormatArgs );
66
75
}
67
76
68
- public function testGenerateNewValidCatalog ()
77
+ /**
78
+ * Test generation of new valid catalog
79
+ *
80
+ * @param string $content
81
+ * @param array $dictionary
82
+ * @dataProvider dictionaryDataProvider
83
+ * @return void
84
+ */
85
+ public function testGenerateNewValidCatalog ($ content , $ dictionary )
69
86
{
70
87
$ configFile = 'test ' ;
71
- $ fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml ' ;
72
- $ content = file_get_contents ($ fixtureXmlFile );
73
88
74
89
$ message = __ ("The \"%1.xml \" file doesn't exist. " , $ configFile );
75
90
76
- $ this ->fileWriteFactory ->expects ($ this ->at (0 ))
91
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (0 ))
77
92
->method ('create ' )
78
93
->with (
79
94
$ configFile ,
@@ -82,136 +97,177 @@ public function testGenerateNewValidCatalog()
82
97
)
83
98
->willThrowException (new FileSystemException ($ message ));
84
99
85
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Write::class);
86
- $ file ->expects ($ this ->once ())
100
+ $ fileMock = $ this ->createMock (Write::class);
101
+ $ fileMock ->expects ($ this ->once ())
87
102
->method ('write ' )
88
103
->with ($ content );
89
104
90
- $ this ->fileWriteFactory ->expects ($ this ->at (1 ))
105
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (1 ))
91
106
->method ('create ' )
92
107
->with (
93
108
$ configFile ,
94
109
DriverPool::FILE ,
95
110
VsCode::FILE_MODE_WRITE
96
111
)
97
- ->willReturn ($ file );
112
+ ->willReturn ($ fileMock );
98
113
99
- $ this ->vscodeFormat ->generateCatalog ($ this -> dictionary , $ configFile );
114
+ $ this ->vscodeFormat ->generateCatalog ($ dictionary , $ configFile );
100
115
}
101
116
102
- public function testGenerateExistingValidCatalog ()
117
+ /**
118
+ * Test modify existing valid catalog
119
+ *
120
+ * @param string $content
121
+ * @param array $dictionary
122
+ * @dataProvider dictionaryDataProvider
123
+ * @return void
124
+ */
125
+ public function testGenerateExistingValidCatalog ($ content , $ dictionary )
103
126
{
104
127
$ configFile = 'test ' ;
105
- $ fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml ' ;
106
- $ content = file_get_contents ($ fixtureXmlFile );
107
128
108
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Read::class);
109
- $ file ->expects ($ this ->once ())
129
+ $ fileMock = $ this ->createMock (Read::class);
130
+ $ fileMock ->expects ($ this ->once ())
110
131
->method ('readAll ' )
111
132
->withAnyParameters ()
112
133
->willReturn ($ content );
113
134
114
- $ this ->fileWriteFactory ->expects ($ this ->at (0 ))
135
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (0 ))
115
136
->method ('create ' )
116
137
->with (
117
138
$ configFile ,
118
139
DriverPool::FILE ,
119
140
VsCode::FILE_MODE_READ
120
141
)
121
- ->willReturn ($ file );
142
+ ->willReturn ($ fileMock );
122
143
123
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Write::class);
124
- $ file ->expects ($ this ->once ())
144
+ $ fileMock = $ this ->createMock (Write::class);
145
+ $ fileMock ->expects ($ this ->once ())
125
146
->method ('write ' )
126
147
->with ($ content );
127
148
128
- $ this ->fileWriteFactory ->expects ($ this ->at (1 ))
149
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (1 ))
129
150
->method ('create ' )
130
151
->with (
131
152
$ configFile ,
132
153
DriverPool::FILE ,
133
154
VsCode::FILE_MODE_WRITE
134
155
)
135
- ->willReturn ($ file );
156
+ ->willReturn ($ fileMock );
136
157
137
- $ this ->vscodeFormat ->generateCatalog ($ this -> dictionary , $ configFile );
158
+ $ this ->vscodeFormat ->generateCatalog ($ dictionary , $ configFile );
138
159
}
139
160
140
- public function testGenerateExistingEmptyValidCatalog ()
161
+ /**
162
+ * Test modify existing empty catalog
163
+ *
164
+ * @param string $content
165
+ * @param array $dictionary
166
+ * @dataProvider dictionaryDataProvider
167
+ * @return void
168
+ */
169
+ public function testGenerateExistingEmptyValidCatalog ($ content , $ dictionary )
141
170
{
142
171
$ configFile = 'test ' ;
143
- $ fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml ' ;
144
- $ content = file_get_contents ($ fixtureXmlFile );
145
172
146
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Read::class);
147
- $ file ->expects ($ this ->once ())
173
+ $ fileMock = $ this ->createMock (Read::class);
174
+ $ fileMock ->expects ($ this ->once ())
148
175
->method ('readAll ' )
149
176
->withAnyParameters ()
150
177
->willReturn ('' );
151
178
152
- $ this ->fileWriteFactory ->expects ($ this ->at (0 ))
179
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (0 ))
153
180
->method ('create ' )
154
181
->with (
155
182
$ configFile ,
156
183
DriverPool::FILE ,
157
184
VsCode::FILE_MODE_READ
158
185
)
159
- ->willReturn ($ file );
186
+ ->willReturn ($ fileMock );
160
187
161
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Write::class);
162
- $ file ->expects ($ this ->once ())
188
+ $ fileMock = $ this ->createMock (Write::class);
189
+ $ fileMock ->expects ($ this ->once ())
163
190
->method ('write ' )
164
191
->with ($ content );
165
192
166
- $ this ->fileWriteFactory ->expects ($ this ->at (1 ))
193
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (1 ))
167
194
->method ('create ' )
168
195
->with (
169
196
$ configFile ,
170
197
DriverPool::FILE ,
171
198
VsCode::FILE_MODE_WRITE
172
199
)
173
- ->willReturn ($ file );
200
+ ->willReturn ($ fileMock );
174
201
175
- $ this ->vscodeFormat ->generateCatalog ($ this -> dictionary , $ configFile );
202
+ $ this ->vscodeFormat ->generateCatalog ($ dictionary , $ configFile );
176
203
}
177
204
178
- public function testGenerateExistingInvalidValidCatalog ()
205
+ /**
206
+ * Test modify existing invalid catalog
207
+ *
208
+ * @param string $content
209
+ * @param array $dictionary
210
+ * @dataProvider dictionaryDataProvider
211
+ * @return void
212
+ */
213
+ public function testGenerateExistingInvalidValidCatalog ($ content , $ dictionary , $ invalidContent )
179
214
{
180
215
$ configFile = 'test ' ;
181
- $ invalidXmlFile = __DIR__ . '/_files/invalid_catalog.xml ' ;
182
- $ invalidContent = file_get_contents ($ invalidXmlFile );
183
- $ validXmlFile = __DIR__ . '/_files/valid_catalog.xml ' ;
184
- $ validContent = file_get_contents ($ validXmlFile );
185
216
186
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Read::class);
187
- $ file ->expects ($ this ->once ())
217
+ $ fileMock = $ this ->createMock (Read::class);
218
+ $ fileMock ->expects ($ this ->once ())
188
219
->method ('readAll ' )
189
220
->withAnyParameters ()
190
221
->willReturn ($ invalidContent );
191
222
192
- $ this ->fileWriteFactory ->expects ($ this ->at (0 ))
223
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (0 ))
193
224
->method ('create ' )
194
225
->with (
195
226
$ configFile ,
196
227
DriverPool::FILE ,
197
228
VsCode::FILE_MODE_READ
198
229
)
199
- ->willReturn ($ file );
230
+ ->willReturn ($ fileMock );
200
231
201
- $ file = $ this ->createMock (\ Magento \ Framework \ Filesystem \ File \ Write::class);
202
- $ file ->expects ($ this ->once ())
232
+ $ fileMock = $ this ->createMock (Write::class);
233
+ $ fileMock ->expects ($ this ->once ())
203
234
->method ('write ' )
204
- ->with ($ validContent );
235
+ ->with ($ content );
205
236
206
- $ this ->fileWriteFactory ->expects ($ this ->at (1 ))
237
+ $ this ->fileWriteFactoryMock ->expects ($ this ->at (1 ))
207
238
->method ('create ' )
208
239
->with (
209
240
$ configFile ,
210
241
DriverPool::FILE ,
211
242
VsCode::FILE_MODE_WRITE
212
243
)
213
- ->willReturn ($ file );
244
+ ->willReturn ($ fileMock );
245
+
246
+ $ this ->vscodeFormat ->generateCatalog ($ dictionary , $ configFile );
247
+ }
248
+
249
+ /**
250
+ * Data provider for test
251
+ *
252
+ * @return array
253
+ */
254
+ public function dictionaryDataProvider ()
255
+ {
256
+ $ fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml ' ;
257
+ $ content = file_get_contents ($ fixtureXmlFile );
258
+ $ invalidXmlFile = __DIR__ . '/_files/invalid_catalog.xml ' ;
259
+ $ invalidContent = file_get_contents ($ invalidXmlFile );
214
260
215
- $ this ->vscodeFormat ->generateCatalog ($ this ->dictionary , $ configFile );
261
+ return [
262
+ [
263
+ $ content ,
264
+ ['urn:magento:framework:Acl/etc/acl.xsd ' => 'vendor/magento/framework/Acl/etc/acl.xsd ' ,
265
+ 'urn:magento:module:Magento_Store:etc/config.xsd ' => 'vendor/magento/module-store/etc/config.xsd ' ,
266
+ 'urn:magento:module:Magento_Cron:etc/crontab.xsd ' => 'vendor/magento/module-cron/etc/crontab.xsd ' ,
267
+ 'urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd ' => 'vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd ' ],
268
+ $ invalidContent ,
269
+ ],
270
+ ];
216
271
}
272
+
217
273
}
0 commit comments