14
14
use Magento \Analytics \ReportXml \ReportProvider ;
15
15
use Magento \Framework \Filesystem \Directory \WriteInterface as DirectoryWriteInterface ;
16
16
use Magento \Framework \Filesystem \File \WriteInterface as FileWriteInterface ;
17
- use Magento \Framework \TestFramework \ Unit \ Helper \ ObjectManager as ObjectManagerHelper ;
17
+ use PHPUnit \Framework \Attributes \ TestWith ;
18
18
use PHPUnit \Framework \MockObject \MockObject ;
19
19
use PHPUnit \Framework \TestCase ;
20
20
@@ -43,11 +43,6 @@ class ReportWriterTest extends TestCase
43
43
*/
44
44
private $ reportProviderMock ;
45
45
46
- /**
47
- * @var ObjectManagerHelper
48
- */
49
- private $ objectManagerHelper ;
50
-
51
46
/**
52
47
* @var DirectoryWriteInterface|MockObject
53
48
*/
@@ -58,111 +53,107 @@ class ReportWriterTest extends TestCase
58
53
*/
59
54
private $ reportWriter ;
60
55
61
- /**
62
- * @var string
63
- */
64
- private static $ reportName = 'testReport ' ;
65
-
66
- /**
67
- * @var string
68
- */
69
- private static $ providerName = 'testProvider ' ;
70
-
71
- /**
72
- * @var string
73
- */
74
- private static $ providerClass = 'Magento\Analytics\Provider ' ;
75
-
76
- /**
77
- * @return void
78
- */
79
56
protected function setUp (): void
80
57
{
81
- $ this ->configInterfaceMock = $ this ->getMockBuilder (ConfigInterface::class)
82
- ->getMockForAbstractClass ();
58
+ $ this ->configInterfaceMock = $ this ->createMock (ConfigInterface::class);
83
59
$ this ->reportValidatorMock = $ this ->createMock (ReportValidator::class);
84
60
$ this ->providerFactoryMock = $ this ->createMock (ProviderFactory::class);
85
61
$ this ->reportProviderMock = $ this ->createMock (ReportProvider::class);
86
- $ this ->directoryMock = $ this ->getMockBuilder (DirectoryWriteInterface::class)
87
- ->getMockForAbstractClass ();
88
- $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
62
+ $ this ->directoryMock = $ this ->createMock (DirectoryWriteInterface::class);
89
63
90
- $ this ->reportWriter = $ this ->objectManagerHelper ->getObject (
91
- ReportWriter::class,
92
- [
93
- 'config ' => $ this ->configInterfaceMock ,
94
- 'reportValidator ' => $ this ->reportValidatorMock ,
95
- 'providerFactory ' => $ this ->providerFactoryMock
96
- ]
64
+ $ this ->reportWriter = new ReportWriter (
65
+ $ this ->configInterfaceMock ,
66
+ $ this ->reportValidatorMock ,
67
+ $ this ->providerFactoryMock ,
97
68
);
98
69
}
99
70
100
71
/**
101
- * @param array $configData
102
72
* @param array $fileData
103
73
* @param array $expectedFileData
104
- * @return void
105
- *
106
- * @dataProvider writeDataProvider
107
74
*/
108
- public function testWrite (array $ configData , array $ fileData , array $ expectedFileData ): void
75
+ #[
76
+ TestWith([
77
+ [[['number ' => 1 , 'type ' => 'Shoes\"" Usual \\\\" ' ]]],
78
+ [['number ' => 1 , 'type ' => 'Shoes"" Usual" ' ]],
79
+ ]),
80
+ TestWith([
81
+ [[['number ' => 1 , 'type ' => 'hello "World" ' ]]],
82
+ [['number ' => 1 , 'type ' => 'hello "World" ' ]],
83
+ ]),
84
+ TestWith([
85
+ [[['number ' => 1 , 'type ' => 'hello \"World\" ' ]]],
86
+ [['number ' => 1 , 'type ' => 'hello "World" ' ]],
87
+ ]),
88
+ TestWith([
89
+ [[['number ' => 1 , 'type ' => 'hello \\"World \\" ' ]]],
90
+ [['number ' => 1 , 'type ' => 'hello "World" ' ]],
91
+ ]),
92
+ TestWith([
93
+ [[['number ' => 1 , 'type ' => 'hello \\\"World \\\" ' ]]],
94
+ [['number ' => 1 , 'type ' => 'hello "World" ' ]],
95
+ ]),
96
+ TestWith([
97
+ [
98
+ [['number ' => 1 , 'type ' => 'hello World 1 ' ]],
99
+ [['number ' => 2 , 'type ' => 'hello World 2 ' ]],
100
+ ],
101
+ [
102
+ ['number ' => 1 , 'type ' => 'hello World 1 ' ],
103
+ ['number ' => 2 , 'type ' => 'hello World 2 ' ],
104
+ ],
105
+ ]),
106
+ ]
107
+ public function testWrite (array $ fileData , array $ expectedFileData ): void
109
108
{
110
- $ fileData = new \IteratorIterator (new \ArrayIterator ($ fileData ));
111
- $ emptyFileData = new \IteratorIterator (new \ArrayIterator ([]));
112
- $ errors = [];
113
- $ this ->configInterfaceMock
114
- ->expects ($ this ->once ())
115
- ->method ('get ' )
116
- ->with ()
117
- ->willReturn ([$ configData ]);
118
- $ this ->providerFactoryMock
119
- ->expects ($ this ->once ())
109
+ $ fileData [] = [];
110
+ $ dataBatches = array_map (fn (array $ batch ) => new \IteratorIterator (new \ArrayIterator ($ batch )), $ fileData );
111
+ array_unshift ($ expectedFileData , ['number ' , 'type ' ]);
112
+
113
+ $ configData = [];
114
+ $ providerClass = 'Magento\Analytics\Provider ' ;
115
+ $ configData ['providers ' ] = [
116
+ [
117
+ 'name ' => 'testProvider ' ,
118
+ 'class ' => $ providerClass ,
119
+ 'parameters ' => ['name ' => 'testReport ' ],
120
+ ],
121
+ ];
122
+
123
+ $ this ->configInterfaceMock ->expects ($ this ->once ())->method ('get ' )->with ()->willReturn ([$ configData ]);
124
+ $ this ->providerFactoryMock ->expects ($ this ->once ())
120
125
->method ('create ' )
121
- ->with (self :: $ providerClass )
126
+ ->with ($ providerClass )
122
127
->willReturn ($ this ->reportProviderMock );
123
128
$ parameterName = isset (reset ($ configData )[0 ]['parameters ' ]['name ' ])
124
129
? reset ($ configData )[0 ]['parameters ' ]['name ' ]
125
130
: '' ;
126
- $ this ->reportProviderMock ->expects ($ this ->exactly (2 ))
131
+ $ this ->reportProviderMock ->expects ($ this ->exactly (count ( $ dataBatches ) ))
127
132
->method ('getBatchReport ' )
128
133
->with ($ parameterName ?: null )
129
- ->willReturnOnConsecutiveCalls ($ fileData , $ emptyFileData );
130
- $ errorStreamMock = $ this ->getMockBuilder (
131
- FileWriteInterface::class
132
- )->getMockForAbstractClass ();
133
- $ errorStreamMock
134
- ->expects ($ this ->once ())
135
- ->method ('lock ' )
136
- ->with ();
137
- $ errorStreamMock
138
- ->expects ($ this ->exactly (2 ))
134
+ ->willReturnOnConsecutiveCalls (...$ dataBatches );
135
+ $ errorStreamMock = $ this ->createMock (FileWriteInterface::class);
136
+ $ errorStreamMock ->expects ($ this ->once ())->method ('lock ' )->with ()->willReturn (true );
137
+ $ errorStreamMock ->expects ($ this ->exactly (count ($ dataBatches ))) //count of batches - empty batch + headers
139
138
->method ('writeCsv ' )
140
- ->willReturnCallback (function (... $ args ) use ($ expectedFileData ) {
139
+ ->willReturnCallback (function (array $ row ) use ($ expectedFileData ) {
141
140
static $ index = 0 ;
142
- $ expectedArgs = [
143
- [array_keys ($ expectedFileData [0 ])],
144
- [$ expectedFileData [0 ]]
145
- ];
146
- $ index ++;
147
- return $ args === $ expectedArgs [$ index - 1 ] ? null : null ;
141
+ $ this ->assertEquals ($ expectedFileData [$ index ++], $ row );
142
+ return true ;
148
143
});
149
144
150
145
$ errorStreamMock ->expects ($ this ->once ())->method ('unlock ' );
151
146
$ errorStreamMock ->expects ($ this ->once ())->method ('close ' );
152
147
if ($ parameterName ) {
153
- $ this ->reportValidatorMock
154
- ->expects ($ this ->once ())
148
+ $ this ->reportValidatorMock ->expects ($ this ->once ())
155
149
->method ('validate ' )
156
150
->with ($ parameterName )
157
- ->willReturn ($ errors );
151
+ ->willReturn ([] );
158
152
}
159
- $ this ->directoryMock
160
- ->expects ($ this ->once ())
153
+ $ this ->directoryMock ->expects ($ this ->once ())
161
154
->method ('openFile ' )
162
- ->with (
163
- $ this ->stringContains ('/var/tmp ' . $ parameterName ?: $ this ->reportName ),
164
- 'w+ '
165
- )->willReturn ($ errorStreamMock );
155
+ ->with ($ this ->stringContains ('/var/tmp ' . $ parameterName ), 'w+ ' )
156
+ ->willReturn ($ errorStreamMock );
166
157
$ this ->assertTrue ($ this ->reportWriter ->write ($ this ->directoryMock , '/var/tmp ' ));
167
158
}
168
159
@@ -176,9 +167,7 @@ public function testWriteErrorFile(array $configData): void
176
167
{
177
168
$ errors = ['orders ' , 'SQL Error: test ' ];
178
169
$ this ->configInterfaceMock ->expects ($ this ->once ())->method ('get ' )->willReturn ([$ configData ]);
179
- $ errorStreamMock = $ this ->getMockBuilder (
180
- FileWriteInterface::class
181
- )->getMockForAbstractClass ();
170
+ $ errorStreamMock = $ this ->createMock (FileWriteInterface::class);
182
171
$ errorStreamMock ->expects ($ this ->once ())->method ('lock ' );
183
172
$ errorStreamMock ->expects ($ this ->once ())->method ('writeCsv ' )->with ($ errors );
184
173
$ errorStreamMock ->expects ($ this ->once ())->method ('unlock ' );
@@ -200,71 +189,6 @@ public function testWriteEmptyReports(): void
200
189
$ this ->assertTrue ($ this ->reportWriter ->write ($ this ->directoryMock , '/var/tmp ' ));
201
190
}
202
191
203
- /**
204
- * @return array
205
- */
206
- public static function writeDataProvider (): array
207
- {
208
- $ configData = [
209
- 'providers ' => [
210
- [
211
- 'name ' => self ::$ providerName ,
212
- 'class ' => self ::$ providerClass ,
213
- 'parameters ' => [
214
- 'name ' => self ::$ reportName
215
- ],
216
- ]
217
- ]
218
- ];
219
- return [
220
- [
221
- 'configData ' => $ configData ,
222
- 'fileData ' => [
223
- ['number ' => 1 , 'type ' => 'Shoes\"" Usual \\\\" ' ]
224
- ],
225
- 'expectedFileData ' => [
226
- ['number ' => 1 , 'type ' => 'Shoes"" Usual" ' ]
227
- ]
228
- ],
229
- [
230
- 'configData ' => $ configData ,
231
- 'fileData ' => [
232
- ['number ' => 1 , 'type ' => 'hello "World" ' ]
233
- ],
234
- 'expectedFileData ' => [
235
- ['number ' => 1 , 'type ' => 'hello "World" ' ]
236
- ]
237
- ],
238
- [
239
- 'configData ' => $ configData ,
240
- 'fileData ' => [
241
- ['number ' => 1 , 'type ' => 'hello \"World\" ' ]
242
- ],
243
- 'expectedFileData ' => [
244
- ['number ' => 1 , 'type ' => 'hello "World" ' ]
245
- ]
246
- ],
247
- [
248
- 'configData ' => $ configData ,
249
- 'fileData ' => [
250
- ['number ' => 1 , 'type ' => 'hello \\"World \\" ' ]
251
- ],
252
- 'expectedFileData ' => [
253
- ['number ' => 1 , 'type ' => 'hello "World" ' ]
254
- ]
255
- ],
256
- [
257
- 'configData ' => $ configData ,
258
- 'fileData ' => [
259
- ['number ' => 1 , 'type ' => 'hello \\\"World \\\" ' ]
260
- ],
261
- 'expectedFileData ' => [
262
- ['number ' => 1 , 'type ' => 'hello "World" ' ]
263
- ]
264
- ],
265
- ];
266
- }
267
-
268
192
/**
269
193
* @return array
270
194
*/
@@ -275,11 +199,9 @@ public static function writeErrorFileDataProvider(): array
275
199
'configData ' => [
276
200
'providers ' => [
277
201
[
278
- 'name ' => self ::$ providerName ,
279
- 'class ' => self ::$ providerClass ,
280
- 'parameters ' => [
281
- 'name ' => self ::$ reportName
282
- ],
202
+ 'name ' => 'testProvider ' ,
203
+ 'class ' => 'Magento\Analytics\Provider ' ,
204
+ 'parameters ' => ['name ' => 'testReport ' ],
283
205
]
284
206
]
285
207
],
0 commit comments