11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2018 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
1414use Magento \Framework \DB \Query \BatchIteratorInterface ;
1515use Magento \Framework \DB \Query \Generator ;
1616use Magento \Framework \DB \Select ;
17- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
18+ use Magento \Framework \EntityManager \MetadataPool ;
1819use PHPUnit \Framework \MockObject \MockObject ;
1920use PHPUnit \Framework \TestCase ;
2021
2122class ImageTest extends TestCase
2223{
23- /**
24- * @var ObjectManager
25- */
26- protected $ objectManager ;
27-
2824 /**
2925 * @var AdapterInterface | MockObject
3026 */
31- protected $ connectionMock ;
27+ private $ connectionMock ;
3228
3329 /**
3430 * @var Generator | MockObject
3531 */
36- protected $ generatorMock ;
32+ private $ generatorMock ;
3733
3834 /**
3935 * @var ResourceConnection | MockObject
4036 */
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 ;
4248
4349 protected function setUp (): void
4450 {
45- $ this ->objectManager =
46- new ObjectManager ($ this );
47- $ this ->connectionMock = $ this ->getMockForAbstractClass (AdapterInterface::class);
51+ $ this ->connectionMock = $ this ->createMock (AdapterInterface::class);
4852 $ this ->resourceMock = $ this ->createMock (ResourceConnection::class);
4953 $ this ->resourceMock ->method ('getConnection ' )
5054 ->willReturn ($ this ->connectionMock );
5155 $ this ->resourceMock ->method ('getTableName ' )
5256 ->willReturnArgument (0 );
5357 $ 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+ );
5468 }
5569
5670 /**
5771 * @return MockObject
5872 */
59- protected function getVisibleImagesSelectMock (): MockObject
73+ private function getVisibleImagesSelectMock (): MockObject
6074 {
61- $ selectMock = $ this ->getMockBuilder (Select::class)
62- ->disableOriginalConstructor ()
63- ->getMock ();
75+ $ selectMock = $ this ->createMock (Select::class);
6476 $ selectMock ->expects ($ this ->once ())
6577 ->method ('distinct ' )
6678 ->willReturnSelf ();
6779 $ selectMock ->expects ($ this ->once ())
6880 ->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 ();
7383 $ selectMock ->expects ($ this ->once ())
7484 ->method ('where ' )
7585 ->with ('disabled = 0 ' )
@@ -81,30 +91,26 @@ protected function getVisibleImagesSelectMock(): MockObject
8191 /**
8292 * @return MockObject
8393 */
84- protected function getUsedImagesSelectMock (): MockObject
94+ private function getUsedImagesSelectMock (): MockObject
8595 {
86- $ selectMock = $ this ->getMockBuilder (Select::class)
87- ->disableOriginalConstructor ()
88- ->getMock ();
96+ $ selectMock = $ this ->createMock (Select::class);
8997 $ selectMock ->expects ($ this ->once ())
9098 ->method ('distinct ' )
9199 ->willReturnSelf ();
92100 $ selectMock ->expects ($ this ->once ())
93101 ->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 ())
99105 ->method ('joinInner ' )
100- ->with (
101- ['image_value ' => Gallery::GALLERY_VALUE_TABLE ],
102- 'images.value_id = image_value.value_id '
103- )->willReturnSelf ();
106+ ->willReturnSelf ();
104107 $ selectMock ->expects ($ this ->once ())
105108 ->method ('where ' )
106109 ->with ('images.disabled = 0 AND image_value.disabled = 0 ' )
107110 ->willReturnSelf ();
111+ $ selectMock ->expects ($ this ->once ())
112+ ->method ('group ' )
113+ ->willReturnSelf ();
108114
109115 return $ selectMock ;
110116 }
@@ -140,17 +146,9 @@ function ($arg) use ($selectMock) {
140146 ->with ($ selectMock )
141147 ->willReturn ($ imagesCount );
142148
143- $ imageModel = $ this ->objectManager ->getObject (
144- Image::class,
145- [
146- 'generator ' => $ this ->generatorMock ,
147- 'resourceConnection ' => $ this ->resourceMock
148- ]
149- );
150-
151149 $ this ->assertSame (
152150 $ imagesCount ,
153- $ imageModel ->getCountAllProductImages ()
151+ $ this -> imageModel ->getCountAllProductImages ()
154152 );
155153 }
156154
@@ -185,17 +183,9 @@ function ($arg) use ($selectMock) {
185183 ->with ($ selectMock )
186184 ->willReturn ($ imagesCount );
187185
188- $ imageModel = $ this ->objectManager ->getObject (
189- Image::class,
190- [
191- 'generator ' => $ this ->generatorMock ,
192- 'resourceConnection ' => $ this ->resourceMock
193- ]
194- );
195-
196186 $ this ->assertSame (
197187 $ imagesCount ,
198- $ imageModel ->getCountUsedProductImages ()
188+ $ this -> imageModel ->getCountUsedProductImages ()
199189 );
200190 }
201191
@@ -206,21 +196,16 @@ function ($arg) use ($selectMock) {
206196 */
207197 public function testGetAllProductImages (int $ imagesCount , int $ batchSize ): void
208198 {
199+ $ selectMock = $ this ->getVisibleImagesSelectMock ();
209200 $ this ->connectionMock ->expects ($ this ->once ())
210201 ->method ('select ' )
211- ->willReturn ($ this -> getVisibleImagesSelectMock () );
202+ ->willReturn ($ selectMock );
212203
213204 $ batchCount = (int )ceil ($ imagesCount / $ batchSize );
214205 $ fetchResultsCallback = $ this ->getFetchResultCallbackForBatches ($ imagesCount , $ batchSize );
215206 $ this ->connectionMock ->expects ($ this ->exactly ($ batchCount ))
216207 ->method ('fetchAll ' )
217208 ->willReturnCallback ($ fetchResultsCallback );
218-
219- /** @var Select | MockObject $selectMock */
220- $ selectMock = $ this ->getMockBuilder (Select::class)
221- ->disableOriginalConstructor ()
222- ->getMock ();
223-
224209 $ this ->generatorMock ->expects ($ this ->once ())
225210 ->method ('generate ' )
226211 ->with (
@@ -232,13 +217,11 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
232217 $ this ->getBatchIteratorCallback ($ selectMock , $ batchCount )
233218 );
234219
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 ,
242225 );
243226 $ resultImagesCount = iterator_to_array ($ imageModel ->getAllProductImages (), false );
244227 $ this ->assertCount ($ imagesCount , $ resultImagesCount );
@@ -251,21 +234,16 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
251234 */
252235 public function testGetUsedProductImages (int $ imagesCount , int $ batchSize ): void
253236 {
237+ $ selectMock = $ this ->getUsedImagesSelectMock ();
254238 $ this ->connectionMock ->expects ($ this ->once ())
255239 ->method ('select ' )
256- ->willReturn ($ this -> getUsedImagesSelectMock () );
240+ ->willReturn ($ selectMock );
257241
258242 $ batchCount = (int )ceil ($ imagesCount / $ batchSize );
259243 $ fetchResultsCallback = $ this ->getFetchResultCallbackForBatches ($ imagesCount , $ batchSize );
260244 $ this ->connectionMock ->expects ($ this ->exactly ($ batchCount ))
261245 ->method ('fetchAll ' )
262246 ->willReturnCallback ($ fetchResultsCallback );
263-
264- /** @var Select | MockObject $selectMock */
265- $ selectMock = $ this ->getMockBuilder (Select::class)
266- ->disableOriginalConstructor ()
267- ->getMock ();
268-
269247 $ this ->generatorMock ->expects ($ this ->once ())
270248 ->method ('generate ' )
271249 ->with (
@@ -277,16 +255,12 @@ public function testGetUsedProductImages(int $imagesCount, int $batchSize): void
277255 $ this ->getBatchIteratorCallback ($ selectMock , $ batchCount )
278256 );
279257
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 ,
288263 );
289-
290264 $ resultImagesCount = iterator_to_array ($ imageModel ->getUsedProductImages (), false );
291265 $ this ->assertCount ($ imagesCount , $ resultImagesCount );
292266 }
@@ -296,10 +270,8 @@ public function testGetUsedProductImages(int $imagesCount, int $batchSize): void
296270 * @param int $batchSize
297271 * @return \Closure
298272 */
299- protected function getFetchResultCallbackForBatches (
300- int $ imagesCount ,
301- int $ batchSize
302- ): \Closure {
273+ private function getFetchResultCallbackForBatches (int $ imagesCount , int $ batchSize ): \Closure
274+ {
303275 $ fetchResultsCallback = function () use (&$ imagesCount , $ batchSize ) {
304276 $ batchSize =
305277 ($ imagesCount >= $ batchSize ) ? $ batchSize : $ imagesCount ;
@@ -327,10 +299,8 @@ protected function getFetchResultCallbackForBatches(
327299 * @param int $batchCount
328300 * @return \Closure
329301 */
330- protected function getBatchIteratorCallback (
331- MockObject $ selectMock ,
332- int $ batchCount
333- ): \Closure {
302+ private function getBatchIteratorCallback (MockObject $ selectMock , int $ batchCount ): \Closure
303+ {
334304 $ iteratorCallback = function () use ($ batchCount , $ selectMock ): array {
335305 $ result = [];
336306 $ count = $ batchCount ;
0 commit comments