Skip to content

Commit fc4899d

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

File tree

5 files changed

+107
-73
lines changed

5 files changed

+107
-73
lines changed

app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/CategoryTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
use Magento\Framework\DB\Adapter\AdapterInterface;
1515
use Magento\Framework\Indexer\IndexerInterface;
1616
use Magento\Framework\Indexer\IndexerRegistry;
17-
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1817
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1918
use PHPUnit\Framework\MockObject\MockObject;
2019
use PHPUnit\Framework\TestCase;
2120

2221
class CategoryTest extends TestCase
2322
{
24-
use MockCreationTrait;
2523
/**
2624
* @var MockObject|IndexerInterface
2725
*/

app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Filter/CategoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Category;
1111
use Magento\Catalog\Model\Layer;
12+
use Magento\Catalog\Model\Layer\Filter\DataProvider\Category as CategoryDataProvider;
1213
use Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory;
1314
use Magento\Catalog\Model\Layer\Filter\Item;
1415
use Magento\Catalog\Model\Layer\Filter\Item\DataBuilder;
@@ -51,7 +52,7 @@ class CategoryTest extends TestCase
5152
private $layer;
5253

5354
/**
54-
* @var \Magento\Catalog\Model\Layer\Filter\DataProvider\Category|MockObject
55+
* @var CategoryDataProvider|MockObject
5556
*/
5657
private $dataProvider;
5758

@@ -83,7 +84,7 @@ protected function setUp(): void
8384
);
8485

8586
$this->dataProvider = $this->createPartialMock(
86-
\Magento\Catalog\Model\Layer\Filter\DataProvider\Category::class,
87+
CategoryDataProvider::class,
8788
['setCategoryId', 'getCategory']
8889
);
8990

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenCategoriesProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;
99

1010
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
1112
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
1213
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1314
use Magento\Framework\DB\Adapter\AdapterInterface;
@@ -70,7 +71,7 @@ protected function setUp(): void
7071
['where', 'deleteFromSelect', 'from']
7172
);
7273
$this->connection = $this->createMock(AdapterInterface::class);
73-
$categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
74+
$categoryResource = $this->createMock(CategoryResource::class);
7475
$this->category->method('getResource')->willReturn($categoryResource);
7576
$categoryResource->method('getConnection')->willReturn($this->connection);
7677
$this->connection->method('select')->willReturn($this->select);
Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<?php
22
/**
3-
* Copyright 2015 Adobe
4-
* All Rights Reserved.
5-
*/
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
5+
*/
66
declare(strict_types=1);
7-
7+
88
namespace Magento\ProductVideo\Test\Unit\Block\Adminhtml\Product\Edit;
9-
9+
1010
use Magento\Backend\Block\Template\Context;
11-
use Magento\Backend\Block\Widget\Form\Element\ElementCreator;
12-
use Magento\Directory\Helper\Data as DirectoryHelper;
11+
use Magento\Framework\App\ObjectManager as AppObjectManager;
1312
use Magento\Framework\Data\FormFactory;
1413
use Magento\Framework\Json\EncoderInterface;
15-
use Magento\Framework\Json\Helper\Data as JsonHelper;
1614
use Magento\Framework\Math\Random;
1715
use Magento\Framework\Registry;
1816
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -21,102 +19,109 @@
2119
use Magento\ProductVideo\Helper\Media;
2220
use PHPUnit\Framework\MockObject\MockObject;
2321
use PHPUnit\Framework\TestCase;
24-
25-
/**
26-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27-
*/
22+
2823
class NewVideoTest extends TestCase
2924
{
3025
/**
3126
* @var Context|MockObject
3227
*/
3328
protected $contextMock;
34-
29+
3530
/**
3631
* @var MockObject|UrlInterface
3732
*/
3833
protected $urlBuilder;
39-
34+
4035
/**
4136
* @var Random|MockObject
4237
*/
4338
protected $mathRandom;
44-
39+
4540
/**
4641
* @var Registry|MockObject
4742
*/
4843
protected $registryMock;
49-
44+
5045
/**
5146
* @var FormFactory|MockObject
5247
*/
5348
protected $formFactoryMock;
54-
49+
5550
/**
5651
* @var EncoderInterface|MockObject
5752
*/
5853
protected $jsonEncoderMock;
59-
54+
6055
/**
6156
* @var Media|MockObject
6257
*/
6358
protected $mediaHelper;
64-
65-
/**
66-
* @var ElementCreator|MockObject
67-
*/
68-
protected $elementCreatorMock;
69-
70-
/**
71-
* @var JsonHelper|MockObject
72-
*/
73-
protected $jsonHelperMock;
74-
75-
/**
76-
* @var DirectoryHelper|MockObject
77-
*/
78-
protected $directoryHelperMock;
79-
59+
8060
/**
81-
* @var NewVideo|MockObject
61+
* @var NewVideo
8262
*/
8363
protected $block;
84-
64+
8565
protected function setUp(): void
8666
{
67+
$objectManagerMock = $this->createMock(AppObjectManager::class);
68+
AppObjectManager::setInstance($objectManagerMock);
69+
8770
$this->contextMock = $this->createMock(Context::class);
8871
$this->mediaHelper = $this->createMock(Media::class);
8972
$this->mathRandom = $this->createMock(Random::class);
9073
$this->urlBuilder = $this->createMock(UrlInterface::class);
91-
$this->contextMock->method('getMathRandom')->willReturn($this->mathRandom);
92-
$this->contextMock->method('getUrlBuilder')->willReturn($this->urlBuilder);
74+
$this->contextMock->expects($this->any())->method('getMathRandom')->willReturn($this->mathRandom);
75+
$this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
9376
$this->registryMock = $this->createMock(Registry::class);
9477
$this->formFactoryMock = $this->createMock(FormFactory::class);
9578
$this->jsonEncoderMock = $this->createMock(EncoderInterface::class);
96-
$this->elementCreatorMock = $this->createMock(ElementCreator::class);
97-
$this->jsonHelperMock = $this->createMock(JsonHelper::class);
98-
$this->directoryHelperMock = $this->createMock(DirectoryHelper::class);
99-
100-
$this->block = $this->getMockBuilder(NewVideo::class)
101-
->onlyMethods(['getHtmlId', 'getWidgetOptions'])
102-
->disableOriginalConstructor()
103-
->getMock();
79+
80+
$objectManager = new ObjectManager($this);
81+
82+
$this->block = $objectManager->getObject(
83+
NewVideo::class,
84+
[
85+
'context' => $this->contextMock,
86+
'mediaHelper' => $this->mediaHelper,
87+
'urlBuilder' => $this->urlBuilder,
88+
'jsonEncoder' => $this->jsonEncoderMock,
89+
'registry' => $this->registryMock,
90+
'formFactory' => $this->formFactoryMock,
91+
]
92+
);
10493
}
105-
94+
10695
public function testGetHtmlId()
10796
{
108-
$expectedId = 'id_test123';
109-
$this->block->method('getHtmlId')->willReturn($expectedId);
97+
$this->mathRandom->expects($this->any())->method('getUniqueHash')->with('id_')->willReturn('id_' . rand());
11098
$result = $this->block->getHtmlId();
111-
$this->assertEquals($expectedId, $result);
99+
$this->assertNotNull($result);
112100
}
113-
101+
114102
public function testGetWidgetOptions()
115103
{
116-
$expectedOptions = '{"saveVideoUrl":"test_url","saveRemoteVideoUrl":"test_remote_url","htmlId":"test_id",'
117-
. '"youTubeApiKey":null,"videoSelector":"#media_gallery_content"}';
118-
$this->block->method('getWidgetOptions')->willReturn($expectedOptions);
104+
$rand = rand();
105+
$this->mathRandom->expects($this->any())->method('getUniqueHash')->with('id_')->willReturn('id_' . $rand);
106+
$saveVideoUrl = 'http://host/index.php/admin/catalog/product_gallery/upload/key/';
107+
$saveRemoteVideoUrl = 'http://host/index.php/admin/product_video/product_gallery/retrieveImage/';
108+
$this->urlBuilder->expects($this->exactly(2))->method('getUrl')->willReturnOnConsecutiveCalls(
109+
$saveVideoUrl,
110+
$saveRemoteVideoUrl
111+
);
112+
$value = [
113+
'saveVideoUrl' => $saveVideoUrl,
114+
'saveRemoteVideoUrl' => $saveRemoteVideoUrl,
115+
'htmlId' => 'id_' . $rand,
116+
'youTubeApiKey' => null,
117+
'videoSelector' => '#media_gallery_content'
118+
];
119+
$this->jsonEncoderMock->expects($this->once())->method('encode')->with(
120+
$value
121+
)->willReturn(
122+
json_encode($value)
123+
);
119124
$result = $this->block->getWidgetOptions();
120-
$this->assertEquals($expectedOptions, $result);
125+
$this->assertNotNull($result);
121126
}
122-
}
127+
}

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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use Magento\Catalog\Block\Product\Context;
1111
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Type\AbstractType;
13+
use Magento\Framework\DataObject;
1214
use Magento\Framework\Json\EncoderInterface;
1315
use Magento\Framework\Registry;
1416
use Magento\Framework\Stdlib\ArrayUtils;
@@ -79,31 +81,58 @@ protected function setUp(): void
7981
*/
8082
public function testGetMediaGalleryDataJson()
8183
{
82-
$expectedJson = '[{"media_type":"external-video","video_url":"http://example.com/video.mp4"}]';
83-
$this->gallery->method('getMediaGalleryDataJson')->willReturn($expectedJson);
84-
$result = $this->gallery->getMediaGalleryDataJson();
85-
$this->assertEquals($expectedJson, $result);
84+
$mediaGalleryData = new DataObject();
85+
$data = [
86+
[
87+
'media_type' => 'external-video',
88+
'video_url' => 'http://magento.ce/media/catalog/product/9/b/9br6ujuthnc.jpg',
89+
'is_base' => true,
90+
],
91+
[
92+
'media_type' => 'external-video',
93+
'video_url' => 'https://www.youtube.com/watch?v=QRYX7GIvdLE',
94+
'is_base' => false,
95+
],
96+
[
97+
'media_type' => '',
98+
'video_url' => '',
99+
'is_base' => null,
100+
]
101+
];
102+
$mediaGalleryData->setData($data);
103+
104+
$this->coreRegistry->method('registry')->willReturn($this->productModelMock);
105+
$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(
109+
[$mediaGalleryData]
110+
);
111+
$this->gallery->getMediaGalleryDataJson();
86112
}
87113

88114
/**
89115
* Test getMediaEmptyGalleryDataJson()
90116
*/
91117
public function testGetMediaEmptyGalleryDataJson()
92118
{
93-
$expectedJson = '[]';
94-
$this->gallery->method('getMediaGalleryDataJson')->willReturn($expectedJson);
95-
$result = $this->gallery->getMediaGalleryDataJson();
96-
$this->assertEquals($expectedJson, $result);
119+
$mediaGalleryData = [];
120+
$this->coreRegistry->method('registry')->willReturn($this->productModelMock);
121+
$typeInstance = $this->createMock(AbstractType::class);
122+
$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);
125+
$this->gallery->getMediaGalleryDataJson();
97126
}
98127

99128
/**
100129
* Test getVideoSettingsJson
101130
*/
102131
public function testGetVideoSettingsJson()
103132
{
104-
$expectedJson = '{"playIfBase":1,"showRelated":0,"videoAutoRestart":0}';
105-
$this->gallery->method('getVideoSettingsJson')->willReturn($expectedJson);
106-
$result = $this->gallery->getVideoSettingsJson();
107-
$this->assertEquals($expectedJson, $result);
133+
$this->mediaHelperMock->method('getPlayIfBaseAttribute')->willReturn(1);
134+
$this->mediaHelperMock->method('getShowRelatedAttribute')->willReturn(0);
135+
$this->mediaHelperMock->method('getVideoAutoRestartAttribute')->willReturn(0);
136+
$this->gallery->getVideoSettingsJson();
108137
}
109138
}

0 commit comments

Comments
 (0)