1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2018 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
14
14
use Magento \Framework \DB \Query \BatchIteratorInterface ;
15
15
use Magento \Framework \DB \Query \Generator ;
16
16
use Magento \Framework \DB \Select ;
17
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
18
+ use Magento \Framework \EntityManager \MetadataPool ;
18
19
use PHPUnit \Framework \MockObject \MockObject ;
19
20
use PHPUnit \Framework \TestCase ;
20
21
21
22
class ImageTest extends TestCase
22
23
{
23
- /**
24
- * @var ObjectManager
25
- */
26
- protected $ objectManager ;
27
-
28
24
/**
29
25
* @var AdapterInterface | MockObject
30
26
*/
31
- protected $ connectionMock ;
27
+ private $ connectionMock ;
32
28
33
29
/**
34
30
* @var Generator | MockObject
35
31
*/
36
- protected $ generatorMock ;
32
+ private $ generatorMock ;
37
33
38
34
/**
39
35
* @var ResourceConnection | MockObject
40
36
*/
41
- protected $ resourceMock ;
37
+ private $ resourceMock ;
38
+
39
+ /**
40
+ * @var MetadataPool|MockObject
41
+ */
42
+ private $ metadataPoolMock ;
43
+
44
+ /**
45
+ * @var Image
46
+ */
47
+ private $ imageModel ;
42
48
43
49
protected function setUp (): void
44
50
{
45
- $ this ->objectManager =
46
- new ObjectManager ($ this );
47
- $ this ->connectionMock = $ this ->getMockForAbstractClass (AdapterInterface::class);
51
+ $ this ->connectionMock = $ this ->createMock (AdapterInterface::class);
48
52
$ this ->resourceMock = $ this ->createMock (ResourceConnection::class);
49
53
$ this ->resourceMock ->method ('getConnection ' )
50
54
->willReturn ($ this ->connectionMock );
51
55
$ this ->resourceMock ->method ('getTableName ' )
52
56
->willReturnArgument (0 );
53
57
$ this ->generatorMock = $ this ->createMock (Generator::class);
58
+ $ this ->metadataPoolMock = $ this ->createMock (MetadataPool::class);
59
+ $ metadata = $ this ->createMock (EntityMetadataInterface::class);
60
+ $ this ->metadataPoolMock ->method ('getMetadata ' )
61
+ ->willReturn ($ metadata );
62
+
63
+ $ this ->imageModel = new Image (
64
+ $ this ->generatorMock ,
65
+ $ this ->resourceMock ,
66
+ $ this ->metadataPoolMock ,
67
+ );
54
68
}
55
69
56
70
/**
57
71
* @return MockObject
58
72
*/
59
- protected function getVisibleImagesSelectMock (): MockObject
73
+ private function getVisibleImagesSelectMock (): MockObject
60
74
{
61
- $ selectMock = $ this ->getMockBuilder (Select::class)
62
- ->disableOriginalConstructor ()
63
- ->getMock ();
75
+ $ selectMock = $ this ->createMock (Select::class);
64
76
$ selectMock ->expects ($ this ->once ())
65
77
->method ('distinct ' )
66
78
->willReturnSelf ();
67
79
$ selectMock ->expects ($ this ->once ())
68
80
->method ('from ' )
69
- ->with (
70
- ['images ' => Gallery::GALLERY_TABLE ],
71
- 'value as filepath '
72
- )->willReturnSelf ();
81
+ ->with (['images ' => Gallery::GALLERY_TABLE ], 'value as filepath ' )
82
+ ->willReturnSelf ();
73
83
$ selectMock ->expects ($ this ->once ())
74
84
->method ('where ' )
75
85
->with ('disabled = 0 ' )
@@ -81,30 +91,26 @@ protected function getVisibleImagesSelectMock(): MockObject
81
91
/**
82
92
* @return MockObject
83
93
*/
84
- protected function getUsedImagesSelectMock (): MockObject
94
+ private function getUsedImagesSelectMock (): MockObject
85
95
{
86
- $ selectMock = $ this ->getMockBuilder (Select::class)
87
- ->disableOriginalConstructor ()
88
- ->getMock ();
96
+ $ selectMock = $ this ->createMock (Select::class);
89
97
$ selectMock ->expects ($ this ->once ())
90
98
->method ('distinct ' )
91
99
->willReturnSelf ();
92
100
$ selectMock ->expects ($ this ->once ())
93
101
->method ('from ' )
94
- ->with (
95
- ['images ' => Gallery::GALLERY_TABLE ],
96
- 'value as filepath '
97
- )->willReturnSelf ();
98
- $ selectMock ->expects ($ this ->once ())
102
+ ->with (['images ' => Gallery::GALLERY_TABLE ], 'value as filepath ' )
103
+ ->willReturnSelf ();
104
+ $ selectMock ->expects ($ this ->atLeastOnce ())
99
105
->method ('joinInner ' )
100
- ->with (
101
- ['image_value ' => Gallery::GALLERY_VALUE_TABLE ],
102
- 'images.value_id = image_value.value_id '
103
- )->willReturnSelf ();
106
+ ->willReturnSelf ();
104
107
$ selectMock ->expects ($ this ->once ())
105
108
->method ('where ' )
106
109
->with ('images.disabled = 0 AND image_value.disabled = 0 ' )
107
110
->willReturnSelf ();
111
+ $ selectMock ->expects ($ this ->once ())
112
+ ->method ('group ' )
113
+ ->willReturnSelf ();
108
114
109
115
return $ selectMock ;
110
116
}
@@ -140,17 +146,9 @@ function ($arg) use ($selectMock) {
140
146
->with ($ selectMock )
141
147
->willReturn ($ imagesCount );
142
148
143
- $ imageModel = $ this ->objectManager ->getObject (
144
- Image::class,
145
- [
146
- 'generator ' => $ this ->generatorMock ,
147
- 'resourceConnection ' => $ this ->resourceMock
148
- ]
149
- );
150
-
151
149
$ this ->assertSame (
152
150
$ imagesCount ,
153
- $ imageModel ->getCountAllProductImages ()
151
+ $ this -> imageModel ->getCountAllProductImages ()
154
152
);
155
153
}
156
154
@@ -185,17 +183,9 @@ function ($arg) use ($selectMock) {
185
183
->with ($ selectMock )
186
184
->willReturn ($ imagesCount );
187
185
188
- $ imageModel = $ this ->objectManager ->getObject (
189
- Image::class,
190
- [
191
- 'generator ' => $ this ->generatorMock ,
192
- 'resourceConnection ' => $ this ->resourceMock
193
- ]
194
- );
195
-
196
186
$ this ->assertSame (
197
187
$ imagesCount ,
198
- $ imageModel ->getCountUsedProductImages ()
188
+ $ this -> imageModel ->getCountUsedProductImages ()
199
189
);
200
190
}
201
191
@@ -206,21 +196,16 @@ function ($arg) use ($selectMock) {
206
196
*/
207
197
public function testGetAllProductImages (int $ imagesCount , int $ batchSize ): void
208
198
{
199
+ $ selectMock = $ this ->getVisibleImagesSelectMock ();
209
200
$ this ->connectionMock ->expects ($ this ->once ())
210
201
->method ('select ' )
211
- ->willReturn ($ this -> getVisibleImagesSelectMock () );
202
+ ->willReturn ($ selectMock );
212
203
213
204
$ batchCount = (int )ceil ($ imagesCount / $ batchSize );
214
205
$ fetchResultsCallback = $ this ->getFetchResultCallbackForBatches ($ imagesCount , $ batchSize );
215
206
$ this ->connectionMock ->expects ($ this ->exactly ($ batchCount ))
216
207
->method ('fetchAll ' )
217
208
->willReturnCallback ($ fetchResultsCallback );
218
-
219
- /** @var Select | MockObject $selectMock */
220
- $ selectMock = $ this ->getMockBuilder (Select::class)
221
- ->disableOriginalConstructor ()
222
- ->getMock ();
223
-
224
209
$ this ->generatorMock ->expects ($ this ->once ())
225
210
->method ('generate ' )
226
211
->with (
@@ -232,13 +217,11 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
232
217
$ this ->getBatchIteratorCallback ($ selectMock , $ batchCount )
233
218
);
234
219
235
- $ imageModel = $ this ->objectManager ->getObject (
236
- Image::class,
237
- [
238
- 'generator ' => $ this ->generatorMock ,
239
- 'resourceConnection ' => $ this ->resourceMock ,
240
- 'batchSize ' => $ batchSize
241
- ]
220
+ $ imageModel = new Image (
221
+ $ this ->generatorMock ,
222
+ $ this ->resourceMock ,
223
+ $ this ->metadataPoolMock ,
224
+ $ batchSize ,
242
225
);
243
226
$ resultImagesCount = iterator_to_array ($ imageModel ->getAllProductImages (), false );
244
227
$ this ->assertCount ($ imagesCount , $ resultImagesCount );
@@ -251,21 +234,16 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
251
234
*/
252
235
public function testGetUsedProductImages (int $ imagesCount , int $ batchSize ): void
253
236
{
237
+ $ selectMock = $ this ->getUsedImagesSelectMock ();
254
238
$ this ->connectionMock ->expects ($ this ->once ())
255
239
->method ('select ' )
256
- ->willReturn ($ this -> getUsedImagesSelectMock () );
240
+ ->willReturn ($ selectMock );
257
241
258
242
$ batchCount = (int )ceil ($ imagesCount / $ batchSize );
259
243
$ fetchResultsCallback = $ this ->getFetchResultCallbackForBatches ($ imagesCount , $ batchSize );
260
244
$ this ->connectionMock ->expects ($ this ->exactly ($ batchCount ))
261
245
->method ('fetchAll ' )
262
246
->willReturnCallback ($ fetchResultsCallback );
263
-
264
- /** @var Select | MockObject $selectMock */
265
- $ selectMock = $ this ->getMockBuilder (Select::class)
266
- ->disableOriginalConstructor ()
267
- ->getMock ();
268
-
269
247
$ this ->generatorMock ->expects ($ this ->once ())
270
248
->method ('generate ' )
271
249
->with (
@@ -277,16 +255,12 @@ public function testGetUsedProductImages(int $imagesCount, int $batchSize): void
277
255
$ this ->getBatchIteratorCallback ($ selectMock , $ batchCount )
278
256
);
279
257
280
- /** @var Image $imageModel */
281
- $ imageModel = $ this ->objectManager ->getObject (
282
- Image::class,
283
- [
284
- 'generator ' => $ this ->generatorMock ,
285
- 'resourceConnection ' => $ this ->resourceMock ,
286
- 'batchSize ' => $ batchSize
287
- ]
258
+ $ imageModel = new Image (
259
+ $ this ->generatorMock ,
260
+ $ this ->resourceMock ,
261
+ $ this ->metadataPoolMock ,
262
+ $ batchSize ,
288
263
);
289
-
290
264
$ resultImagesCount = iterator_to_array ($ imageModel ->getUsedProductImages (), false );
291
265
$ this ->assertCount ($ imagesCount , $ resultImagesCount );
292
266
}
@@ -296,10 +270,8 @@ public function testGetUsedProductImages(int $imagesCount, int $batchSize): void
296
270
* @param int $batchSize
297
271
* @return \Closure
298
272
*/
299
- protected function getFetchResultCallbackForBatches (
300
- int $ imagesCount ,
301
- int $ batchSize
302
- ): \Closure {
273
+ private function getFetchResultCallbackForBatches (int $ imagesCount , int $ batchSize ): \Closure
274
+ {
303
275
$ fetchResultsCallback = function () use (&$ imagesCount , $ batchSize ) {
304
276
$ batchSize =
305
277
($ imagesCount >= $ batchSize ) ? $ batchSize : $ imagesCount ;
@@ -327,10 +299,8 @@ protected function getFetchResultCallbackForBatches(
327
299
* @param int $batchCount
328
300
* @return \Closure
329
301
*/
330
- protected function getBatchIteratorCallback (
331
- MockObject $ selectMock ,
332
- int $ batchCount
333
- ): \Closure {
302
+ private function getBatchIteratorCallback (MockObject $ selectMock , int $ batchCount ): \Closure
303
+ {
334
304
$ iteratorCallback = function () use ($ batchCount , $ selectMock ): array {
335
305
$ result = [];
336
306
$ count = $ batchCount ;
0 commit comments