Skip to content

Commit 6752b5f

Browse files
ENGCOM-6510: [Weee] Cover Weee Plugin by Unit Test #26127
2 parents 5802635 + 3f45fae commit 6752b5f

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Weee\Test\Unit\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
9+
10+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Weee\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper\ProcessTaxAttribute;
15+
use PHPUnit\Framework\MockObject\Matcher\InvokedCount as InvokedCountMatcher;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ProcessTaxAttributeTest extends TestCase
20+
{
21+
/**
22+
* Weee frontend input
23+
*/
24+
private const WEEE_FRONTEND_INPUT = 'weee';
25+
26+
/**
27+
* Text frontend input
28+
*/
29+
private const TEXT_FRONTEND_INPUT = 'text';
30+
31+
/**
32+
* Stub weee attribute code
33+
*/
34+
private const STUB_WEEE_ATTRIBUTE_CODE = 'weee_1';
35+
36+
/**
37+
* Stub weee attribute value
38+
*/
39+
private const STUB_WEEE_ATTRIBUTE_VALUE = 1122;
40+
41+
/**
42+
* @var ProcessTaxAttribute
43+
*/
44+
private $plugin;
45+
46+
/**
47+
* @var Helper|MockObject
48+
*/
49+
private $subjectMock;
50+
51+
/**
52+
* @var Product|MockObject
53+
*/
54+
private $productMock;
55+
56+
/**
57+
* @var Product|MockObject
58+
*/
59+
private $resultMock;
60+
61+
/**
62+
* Prepare environment for test
63+
*/
64+
protected function setUp()
65+
{
66+
$this->subjectMock = $this->createMock(Helper::class);
67+
$this->resultMock = $this->createMock(Product::class);
68+
$this->productMock = $this->createMock(Product::class);
69+
70+
$objectManager = new ObjectManager($this);
71+
$this->plugin = $objectManager->getObject(ProcessTaxAttribute::class);
72+
}
73+
74+
/**
75+
* Test afterInitializeFromData when attributes are empty
76+
*/
77+
public function testAfterInitializeFromDataWhenAttributesAreEmpty()
78+
{
79+
$this->resultMock->expects($this->any())->method('getAttributes')
80+
->willReturn([]);
81+
82+
$this->resultMock->expects($this->never())->method('setData');
83+
84+
$this->plugin->afterInitializeFromData($this->subjectMock, $this->resultMock, $this->productMock, []);
85+
}
86+
87+
/**
88+
* Test afterInitializeFromData when attributes do not include weee frontend input
89+
*/
90+
public function testAfterInitializeFromDataWhenAttributesDoNotIncludeWeee()
91+
{
92+
/** @var AbstractAttribute|MockObject $attributeMock */
93+
$attributeMock = $this->createMock(AbstractAttribute::class);
94+
95+
$attributeMock->expects($this->any())->method('getFrontendInput')
96+
->willReturn(self::TEXT_FRONTEND_INPUT);
97+
98+
$this->resultMock->expects($this->any())->method('getAttributes')
99+
->willReturn([$attributeMock]);
100+
101+
$this->resultMock->expects($this->never())->method('setData');
102+
103+
$this->plugin->afterInitializeFromData($this->subjectMock, $this->resultMock, $this->productMock, []);
104+
}
105+
106+
/**
107+
* Test afterInitializeFromData when attributes include weee
108+
*
109+
* @param array $productData
110+
* @param InvokedCountMatcher $expected
111+
* @dataProvider afterInitializeFromDataWhenAttributesIncludeWeeeDataProvider
112+
*/
113+
public function testAfterInitializeFromDataWhenAttributesIncludeWeee($productData, $expected)
114+
{
115+
/** @var AbstractAttribute|MockObject $attributeMock */
116+
$attributeMock = $this->createMock(AbstractAttribute::class);
117+
118+
$attributeMock->expects($this->any())->method('getFrontendInput')
119+
->willReturn(self::WEEE_FRONTEND_INPUT);
120+
$attributeMock->expects($this->any())->method('getAttributeCode')
121+
->willReturn(self::STUB_WEEE_ATTRIBUTE_CODE);
122+
$this->resultMock->expects($this->any())->method('getAttributes')
123+
->willReturn([$attributeMock]);
124+
125+
$this->resultMock->expects($expected)->method('setData')
126+
->with(self::STUB_WEEE_ATTRIBUTE_CODE, [])
127+
->willReturnSelf();
128+
129+
$this->plugin->afterInitializeFromData(
130+
$this->subjectMock,
131+
$this->resultMock,
132+
$this->productMock,
133+
$productData
134+
);
135+
}
136+
137+
/**
138+
* ProductData data provider for testAfterInitializeFromDataWhenAttributesIncludeWeee
139+
*
140+
* @return array
141+
*/
142+
public function afterInitializeFromDataWhenAttributesIncludeWeeeDataProvider()
143+
{
144+
return [
145+
'Product data includes wee' => [
146+
[
147+
self::STUB_WEEE_ATTRIBUTE_CODE => self::STUB_WEEE_ATTRIBUTE_VALUE
148+
],
149+
$this->never()
150+
],
151+
'Product data does not include wee' => [
152+
[],
153+
$this->once()
154+
]
155+
];
156+
}
157+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Weee\Test\Unit\Plugin\Ui\DataProvider;
10+
11+
use Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider;
12+
use Magento\Framework\App\Config;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Weee\Model\Config as WeeeConfig;
15+
use Magento\Weee\Plugin\Ui\DataProvider\WeeeSettings;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class WeeeSettingsTest extends TestCase
20+
{
21+
/**
22+
* Stub settings fpt display product list
23+
*/
24+
private const STUB_FPT_DISPLAY_PRODUCT_LIST = '1';
25+
26+
/**
27+
* @var WeeeSettings
28+
*/
29+
private $plugin;
30+
31+
/**
32+
* @var DataProvider|MockObject
33+
*/
34+
protected $subjectMock;
35+
36+
/**
37+
* @var Config|MockObject
38+
*/
39+
private $configMock;
40+
41+
/**
42+
* Prepare environment for test
43+
*/
44+
protected function setUp()
45+
{
46+
$this->configMock = $this->createMock(Config::class);
47+
$this->subjectMock = $this->createMock(DataProvider::class);
48+
49+
$objectManager = new ObjectManager($this);
50+
$this->plugin = $objectManager->getObject(
51+
WeeeSettings::class,
52+
[
53+
'config' => $this->configMock
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Test plugin afterGetData
60+
*/
61+
public function testAfterGetDataWhenConfigIsYesResultIsEmpty()
62+
{
63+
$this->configMock->expects($this->any())->method('getValue')
64+
->with(WeeeConfig::XML_PATH_FPT_DISPLAY_PRODUCT_LIST)
65+
->willReturn(self::STUB_FPT_DISPLAY_PRODUCT_LIST);
66+
67+
$this->assertEquals(
68+
[
69+
'displayWeee' => self::STUB_FPT_DISPLAY_PRODUCT_LIST
70+
],
71+
$this->plugin->afterGetData($this->subjectMock, [])
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)