Skip to content

Commit 37654bd

Browse files
committed
AC-15635: PHPUnit 12 Upgrade | Optimize code
1 parent fc4899d commit 37654bd

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

app/code/Magento/ProductVideo/Test/Unit/Block/Product/View/GalleryTest.php

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
use Magento\Catalog\Block\Product\Context;
1111
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface;
13+
use Magento\Catalog\Model\Product\Image\UrlBuilder;
1214
use Magento\Catalog\Model\Product\Type\AbstractType;
1315
use Magento\Framework\DataObject;
1416
use Magento\Framework\Json\EncoderInterface;
1517
use Magento\Framework\Registry;
1618
use Magento\Framework\Stdlib\ArrayUtils;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1720
use Magento\ProductVideo\Block\Product\View\Gallery;
1821
use Magento\ProductVideo\Helper\Media;
1922
use PHPUnit\Framework\MockObject\MockObject;
@@ -47,7 +50,17 @@ class GalleryTest extends TestCase
4750
protected $coreRegistry;
4851

4952
/**
50-
* @var Gallery|MockObject
53+
* @var ImagesConfigFactoryInterface|MockObject
54+
*/
55+
protected $imagesConfigFactoryMock;
56+
57+
/**
58+
* @var UrlBuilder|MockObject
59+
*/
60+
protected $urlBuilderMock;
61+
62+
/**
63+
* @var Gallery
5164
*/
5265
protected $gallery;
5366

@@ -66,14 +79,36 @@ protected function setUp(): void
6679
$this->mediaHelperMock = $this->createMock(Media::class);
6780
$this->jsonEncoderMock = $this->createMock(EncoderInterface::class);
6881
$this->coreRegistry = $this->createMock(Registry::class);
69-
$this->contextMock->method('getRegistry')->willReturn($this->coreRegistry);
82+
$this->contextMock->expects($this->once())->method('getRegistry')->willReturn($this->coreRegistry);
83+
84+
$this->imagesConfigFactoryMock = $this->createMock(ImagesConfigFactoryInterface::class);
85+
$this->urlBuilderMock = $this->createMock(UrlBuilder::class);
7086

7187
$this->productModelMock = $this->createMock(Product::class);
7288

73-
$this->gallery = $this->getMockBuilder(Gallery::class)
74-
->onlyMethods(['getMediaGalleryDataJson', 'getVideoSettingsJson'])
75-
->disableOriginalConstructor()
76-
->getMock();
89+
$objectManager = new ObjectManager($this);
90+
91+
$objects = [
92+
[
93+
ImagesConfigFactoryInterface::class,
94+
$this->imagesConfigFactoryMock
95+
],
96+
[
97+
UrlBuilder::class,
98+
$this->urlBuilderMock
99+
]
100+
];
101+
$objectManager->prepareObjectManager($objects);
102+
103+
$this->gallery = $objectManager->getObject(
104+
Gallery::class,
105+
[
106+
'context' => $this->contextMock,
107+
'arrayUtils' => $this->arrayUtilsMock,
108+
'mediaHelper' => $this->mediaHelperMock,
109+
'jsonEncoder' => $this->jsonEncoderMock,
110+
]
111+
);
77112
}
78113

79114
/**
@@ -101,11 +136,11 @@ public function testGetMediaGalleryDataJson()
101136
];
102137
$mediaGalleryData->setData($data);
103138

104-
$this->coreRegistry->method('registry')->willReturn($this->productModelMock);
139+
$this->coreRegistry->expects($this->any())->method('registry')->willReturn($this->productModelMock);
105140
$typeInstance = $this->createMock(AbstractType::class);
106-
$typeInstance->method('getStoreFilter')->willReturn('_cache_instance_store_filter');
107-
$this->productModelMock->method('getTypeInstance')->willReturn($typeInstance);
108-
$this->productModelMock->method('getMediaGalleryImages')->willReturn(
141+
$typeInstance->expects($this->any())->method('getStoreFilter')->willReturn('_cache_instance_store_filter');
142+
$this->productModelMock->expects($this->any())->method('getTypeInstance')->willReturn($typeInstance);
143+
$this->productModelMock->expects($this->any())->method('getMediaGalleryImages')->willReturn(
109144
[$mediaGalleryData]
110145
);
111146
$this->gallery->getMediaGalleryDataJson();
@@ -117,11 +152,11 @@ public function testGetMediaGalleryDataJson()
117152
public function testGetMediaEmptyGalleryDataJson()
118153
{
119154
$mediaGalleryData = [];
120-
$this->coreRegistry->method('registry')->willReturn($this->productModelMock);
155+
$this->coreRegistry->expects($this->any())->method('registry')->willReturn($this->productModelMock);
121156
$typeInstance = $this->createMock(AbstractType::class);
122157
$typeInstance->expects($this->any())->method('getStoreFilter')->willReturn('_cache_instance_store_filter');
123-
$this->productModelMock->method('getTypeInstance')->willReturn($typeInstance);
124-
$this->productModelMock->method('getMediaGalleryImages')->willReturn($mediaGalleryData);
158+
$this->productModelMock->expects($this->any())->method('getTypeInstance')->willReturn($typeInstance);
159+
$this->productModelMock->expects($this->any())->method('getMediaGalleryImages')->willReturn($mediaGalleryData);
125160
$this->gallery->getMediaGalleryDataJson();
126161
}
127162

@@ -130,9 +165,9 @@ public function testGetMediaEmptyGalleryDataJson()
130165
*/
131166
public function testGetVideoSettingsJson()
132167
{
133-
$this->mediaHelperMock->method('getPlayIfBaseAttribute')->willReturn(1);
134-
$this->mediaHelperMock->method('getShowRelatedAttribute')->willReturn(0);
135-
$this->mediaHelperMock->method('getVideoAutoRestartAttribute')->willReturn(0);
168+
$this->mediaHelperMock->expects($this->once())->method('getPlayIfBaseAttribute')->willReturn(1);
169+
$this->mediaHelperMock->expects($this->once())->method('getShowRelatedAttribute')->willReturn(0);
170+
$this->mediaHelperMock->expects($this->once())->method('getVideoAutoRestartAttribute')->willReturn(0);
136171
$this->gallery->getVideoSettingsJson();
137172
}
138173
}

0 commit comments

Comments
 (0)