Skip to content

Commit 962274a

Browse files
committed
Fix static test
1 parent 784e733 commit 962274a

File tree

1 file changed

+114
-53
lines changed
  • app/code/Magento/ConfigurableProduct/Test/Unit/Plugin/Model/ResourceModel

1 file changed

+114
-53
lines changed

app/code/Magento/ConfigurableProduct/Test/Unit/Plugin/Model/ResourceModel/ProductTest.php

Lines changed: 114 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,72 @@
77
namespace Magento\ConfigurableProduct\Test\Unit\Plugin\Model\ResourceModel;
88

99
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Catalog\Model\Product as ModelProduct;
1011
use Magento\Catalog\Model\Product\Type;
1112
use Magento\Catalog\Model\ProductAttributeSearchResults;
1213
use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
14+
use Magento\Catalog\Model\ResourceModel\Product as ResourceModelProduct;
1315
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1416
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute as ConfigurableAttribute;
17+
use Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product as PluginResourceModelProduct;
1518
use Magento\Framework\Api\ExtensionAttributesInterface;
1619
use Magento\Framework\Api\FilterBuilder;
1720
use Magento\Framework\Api\SearchCriteria;
1821
use Magento\Framework\Api\SearchCriteriaBuilder;
22+
use Magento\Framework\Exception\NoSuchEntityException;
1923
use Magento\Framework\Indexer\ActionInterface;
24+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
2027

2128
/**
2229
* Unit test and integration test for plugin
2330
*
2431
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2532
*/
26-
class ProductTest extends \PHPUnit\Framework\TestCase
33+
class ProductTest extends TestCase
2734
{
2835
/**
29-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
36+
* @var PluginResourceModelProduct
3037
*/
31-
private $objectManager;
38+
private $model;
3239

3340
/**
34-
* @var Configurable|\PHPUnit_Framework_MockObject_MockObject
41+
* @var ObjectManagerHelper
3542
*/
36-
private $configurableMock;
43+
private $objectManagerHelper;
3744

3845
/**
39-
* @var ActionInterface|\PHPUnit_Framework_MockObject_MockObject
46+
* @var Configurable|MockObject
4047
*/
41-
private $actionMock;
48+
private $configurableMock;
4249

4350
/**
44-
* @var \Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product
51+
* @var ActionInterface|MockObject
4552
*/
46-
private $model;
53+
private $actionMock;
4754

4855
/**
49-
* @var ProductAttributeRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
56+
* @var ProductAttributeRepositoryInterface|MockObject
5057
*/
5158
private $productAttributeRepositoryMock;
5259

5360
/**
54-
* @var SearchCriteriaBuilder|\PHPUnit\Framework\MockObject\MockObject
61+
* @var SearchCriteriaBuilder|MockObject
5562
*/
5663
private $searchCriteriaBuilderMock;
5764

5865
/**
59-
* @var FilterBuilder|\PHPUnit\Framework\MockObject\MockObject
66+
* @var FilterBuilder|MockObject
6067
*/
6168
private $filterBuilderMock;
6269

70+
/**
71+
* @inheritDoc
72+
*/
6373
public function setUp()
6474
{
65-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
75+
$this->objectManagerHelper = new ObjectManagerHelper($this);
6676
$this->configurableMock = $this->createMock(Configurable::class);
6777
$this->actionMock = $this->createMock(ActionInterface::class);
6878
$this->productAttributeRepositoryMock = $this->getMockBuilder(ProductAttributeRepositoryInterface::class)
@@ -78,8 +88,8 @@ public function setUp()
7888
['setField', 'setConditionType', 'setValue', 'create']
7989
);
8090

81-
$this->model = $this->objectManager->getObject(
82-
\Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product::class,
91+
$this->model = $this->objectManagerHelper->getObject(
92+
PluginResourceModelProduct::class,
8393
[
8494
'configurable' => $this->configurableMock,
8595
'productIndexer' => $this->actionMock,
@@ -90,13 +100,17 @@ public function setUp()
90100
);
91101
}
92102

93-
public function testBeforeSaveConfigurable()
103+
/**
104+
* @return void
105+
* @throws NoSuchEntityException
106+
*/
107+
public function testBeforeSaveConfigurable():void
94108
{
95-
/** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
96-
$subject = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
97-
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $object */
109+
/** @var ResourceModelProduct|MockObject $subject */
110+
$subject = $this->createMock(ResourceModelProduct::class);
111+
/** @var ModelProduct|MockObject $object */
98112
$object = $this->createPartialMock(
99-
\Magento\Catalog\Model\Product::class,
113+
ModelProduct::class,
100114
[
101115
'getTypeId',
102116
'getTypeInstance',
@@ -105,7 +119,7 @@ public function testBeforeSaveConfigurable()
105119
]
106120
);
107121
$type = $this->createPartialMock(
108-
\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class,
122+
Configurable::class,
109123
['getSetAttributes']
110124
);
111125

@@ -117,81 +131,128 @@ public function testBeforeSaveConfigurable()
117131
ConfigurableAttribute::class,
118132
['getAttributeId']
119133
);
120-
$extensionAttributes->expects($this->exactly(2))->method('getConfigurableProductOptions')
134+
$extensionAttributes->expects($this->exactly(2))
135+
->method('getConfigurableProductOptions')
121136
->willReturn([$option]);
122-
$object->expects($this->once())->method('getExtensionAttributes')
137+
$object->expects($this->once())
138+
->method('getExtensionAttributes')
123139
->willReturn($extensionAttributes);
124140

125-
$this->filterBuilderMock->expects($this->atLeastOnce())->method('setField')->willReturnSelf();
126-
$this->filterBuilderMock->expects($this->atLeastOnce())->method('setValue')->willReturnSelf();
127-
$this->filterBuilderMock->expects($this->atLeastOnce())->method('setConditionType')->willReturnSelf();
128-
$this->filterBuilderMock->expects($this->atLeastOnce())->method('create')->willReturnSelf();
141+
$this->filterBuilderMock->expects($this->atLeastOnce())
142+
->method('setField')
143+
->willReturnSelf();
144+
$this->filterBuilderMock->expects($this->atLeastOnce())
145+
->method('setValue')
146+
->willReturnSelf();
147+
$this->filterBuilderMock->expects($this->atLeastOnce())
148+
->method('setConditionType')
149+
->willReturnSelf();
150+
$this->filterBuilderMock->expects($this->atLeastOnce())
151+
->method('create')
152+
->willReturnSelf();
129153
$searchCriteria = $this->createMock(SearchCriteria::class);
130-
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteria);
154+
$this->searchCriteriaBuilderMock->expects($this->once())
155+
->method('create')
156+
->willReturn($searchCriteria);
131157

132158
$searchResultMockClass = $this->createPartialMock(
133159
ProductAttributeSearchResults::class,
134160
['getItems']
135161
);
136162
$this->productAttributeRepositoryMock->expects($this->once())
137-
->method('getList')->with($searchCriteria)->willReturn($searchResultMockClass);
163+
->method('getList')
164+
->with($searchCriteria)
165+
->willReturn($searchResultMockClass);
138166
$optionAttribute = $this->createPartialMock(
139167
EavAttribute::class,
140168
['getAttributeCode']
141169
);
142-
$searchResultMockClass->expects($this->once())->method('getItems')->willReturn([$optionAttribute]);
143-
$type->expects($this->once())->method('getSetAttributes')->with($object);
170+
$searchResultMockClass->expects($this->once())
171+
->method('getItems')
172+
->willReturn([$optionAttribute]);
173+
$type->expects($this->once())
174+
->method('getSetAttributes')
175+
->with($object);
144176

145-
$object->expects($this->once())->method('getTypeId')->will($this->returnValue(Configurable::TYPE_CODE));
146-
$object->expects($this->once())->method('getTypeInstance')->will($this->returnValue($type));
147-
$object->expects($this->once())->method('setData');
148-
$option->expects($this->once())->method('getAttributeId');
149-
$optionAttribute->expects($this->once())->method('getAttributeCode');
177+
$object->expects($this->once())
178+
->method('getTypeId')
179+
->will($this->returnValue(Configurable::TYPE_CODE));
180+
$object->expects($this->once())
181+
->method('getTypeInstance')
182+
->will($this->returnValue($type));
183+
$object->expects($this->once())
184+
->method('setData');
185+
$option->expects($this->once())
186+
->method('getAttributeId');
187+
$optionAttribute->expects($this->once())
188+
->method('getAttributeCode');
150189

151190
$this->model->beforeSave(
152191
$subject,
153192
$object
154193
);
155194
}
156195

157-
public function testBeforeSaveSimple()
196+
/**
197+
* @return void
198+
* @throws NoSuchEntityException
199+
*/
200+
public function testBeforeSaveSimple():void
158201
{
159-
/** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
160-
$subject = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
161-
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $object */
162-
$object = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getTypeId', 'getTypeInstance']);
163-
$object->expects($this->once())->method('getTypeId')->will($this->returnValue(Type::TYPE_SIMPLE));
164-
$object->expects($this->never())->method('getTypeInstance');
202+
/** @var ResourceModelProduct|MockObject $subject */
203+
$subject = $this->createMock(ResourceModelProduct::class);
204+
/** @var ModelProduct|MockObject $object */
205+
$object = $this->createPartialMock(
206+
ModelProduct::class,
207+
[
208+
'getTypeId',
209+
'getTypeInstance'
210+
]
211+
);
212+
$object->expects($this->once())
213+
->method('getTypeId')
214+
->will($this->returnValue(Type::TYPE_SIMPLE));
215+
$object->expects($this->never())
216+
->method('getTypeInstance');
165217

166218
$this->model->beforeSave(
167219
$subject,
168220
$object
169221
);
170222
}
171223

172-
public function testAroundDelete()
224+
/**
225+
* @return void
226+
*/
227+
public function testAroundDelete():void
173228
{
174229
$productId = '1';
175230
$parentConfigId = ['2'];
176-
/** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
177-
$subject = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
178-
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */
231+
/** @var ResourceModelProduct|MockObject $subject */
232+
$subject = $this->createMock(ResourceModelProduct::class);
233+
/** @var ModelProduct|MockObject $product */
179234
$product = $this->createPartialMock(
180-
\Magento\Catalog\Model\Product::class,
235+
ModelProduct::class,
181236
['getId', 'delete']
182237
);
183-
$product->expects($this->once())->method('getId')->willReturn($productId);
184-
$product->expects($this->once())->method('delete')->willReturn(true);
238+
$product->expects($this->once())
239+
->method('getId')
240+
->willReturn($productId);
241+
$product->expects($this->once())
242+
->method('delete')
243+
->willReturn(true);
185244
$this->configurableMock->expects($this->once())
186245
->method('getParentIdsByChild')
187246
->with($productId)
188247
->willReturn($parentConfigId);
189-
$this->actionMock->expects($this->once())->method('executeList')->with($parentConfigId);
248+
$this->actionMock->expects($this->once())
249+
->method('executeList')
250+
->with($parentConfigId);
190251

191252
$return = $this->model->aroundDelete(
192253
$subject,
193-
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $prod */
194-
function (\Magento\Catalog\Model\Product $prod) use ($subject) {
254+
/** @var ModelProduct|MockObject $prod */
255+
function (ModelProduct $prod) use ($subject) {
195256
$prod->delete();
196257
return $subject;
197258
},

0 commit comments

Comments
 (0)