Skip to content

Commit 6f24431

Browse files
committed
MAGETWO-56702: Around plugins refactoring: refactoring and unit test fixing/coverage
1 parent 9af2c11 commit 6f24431

File tree

22 files changed

+119
-117
lines changed

22 files changed

+119
-117
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
/**
3535
* @param \Magento\UrlRewrite\Model\StorageInterface $object
36-
* @param void $result
36+
* @param null $result
3737
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls
3838
* @return void
3939
* @SuppressWarnings(PHPMD.UnusedFormalParameter)

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,52 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin;
87

98
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
109
use Magento\CatalogUrlRewrite\Model\Category\ProductFactory;
1110
use Magento\UrlRewrite\Model\StorageInterface;
12-
use Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage;
11+
use Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage as CategoryStoragePlugin;
1312
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1413
use Magento\UrlRewrite\Model\UrlFinderInterface;
1514
use Magento\CatalogUrlRewrite\Model\Category\Product;
1615
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product as ProductResourceModel;
1716

1817
class StorageTest extends \PHPUnit_Framework_TestCase
1918
{
20-
/** @var Storage */
21-
private $model;
19+
/**
20+
* @var CategoryStoragePlugin
21+
*/
22+
private $plugin;
2223

23-
/** @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
24+
/**
25+
* @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject
26+
*/
2427
private $productFactory;
2528

26-
/** @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
/**
30+
* @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject
31+
*/
2732
private $urlFinder;
2833

29-
/** @var StorageInterface|\PHPUnit_Framework_MockObject_MockObject */
34+
/**
35+
* @var StorageInterface|\PHPUnit_Framework_MockObject_MockObject
36+
*/
3037
private $storage;
3138

32-
/** @var Product|\PHPUnit_Framework_MockObject_MockObject */
39+
/**
40+
* @var Product|\PHPUnit_Framework_MockObject_MockObject
41+
*/
3342
private $product;
3443

35-
/** @var ProductResourceModel|\PHPUnit_Framework_MockObject_MockObject */
44+
/**
45+
* @var ProductResourceModel|\PHPUnit_Framework_MockObject_MockObject
46+
*/
3647
private $productResourceModel;
3748

38-
/** @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */
49+
/**
50+
* @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject
51+
*/
3952
private $urlRewrite;
4053

4154
protected function setUp()
@@ -59,8 +72,8 @@ protected function setUp()
5972
->setMethods(['getMetadata', 'getEntityType', 'getIsAutogenerated', 'getUrlRewriteId', 'getEntityId'])
6073
->getMock();
6174

62-
$this->model = (new ObjectManager($this))->getObject(
63-
Storage::class,
75+
$this->plugin = (new ObjectManager($this))->getObject(
76+
CategoryStoragePlugin::class,
6477
[
6578
'productFactory' => $this->productFactory,
6679
'urlFinder' => $this->urlFinder
@@ -85,6 +98,6 @@ public function testAfterReplace()
8598
$this->product->expects(static::once())->method('getResource')->willReturn($this->productResourceModel);
8699
$this->productResourceModel->expects(static::once())->method('saveMultiple')->willReturnSelf();
87100

88-
$this->model->afterReplace($this->storage, null, $productUrls);
101+
$this->plugin->afterReplace($this->storage, null, $productUrls);
89102
}
90103
}

app/code/Magento/CmsUrlRewrite/Test/Unit/Plugin/Cms/Model/ResourceModel/PageTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,10 @@ class PageTest extends \PHPUnit_Framework_TestCase
3030
*/
3131
protected $cmsPageResourceMock;
3232

33-
/**
34-
* @var \Closure
35-
*/
36-
protected $closureMock;
37-
3833
protected function setUp()
3934
{
4035
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4136

42-
$this->closureMock = function () {
43-
return 'URL Rewrite Result';
44-
};
45-
4637
$this->urlPersistMock = $this->getMockBuilder(\Magento\UrlRewrite\Model\UrlPersistInterface::class)
4738
->getMockForAbstractClass();
4839

@@ -62,7 +53,7 @@ protected function setUp()
6253
);
6354
}
6455

65-
public function testAroundDeletePositive()
56+
public function testAfterDeletePositive()
6657
{
6758
$productId = 100;
6859

@@ -93,7 +84,7 @@ public function testAroundDeletePositive()
9384
);
9485
}
9586

96-
public function testAroundDeleteNegative()
87+
public function testAfterDeleteNegative()
9788
{
9889
$this->cmsPageMock->expects($this->once())
9990
->method('isDeleted')

app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Catalog\Model\ProductFactory;
1010
use Magento\ConfigurableProduct\Model\Product\Type;
1111
use Magento\Catalog\Model\Product;
12-
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
12+
use Magento\Catalog\Controller\Adminhtml\Product\Builder as CatalogProductBuilder;
1313
use Magento\Framework\App\RequestInterface;
1414

1515
class Plugin
@@ -35,18 +35,17 @@ public function __construct(ProductFactory $productFactory, Type\Configurable $c
3535
}
3636

3737
/**
38-
* @param Builder $subject
38+
* Set type and data to configurable product
39+
*
40+
* @param CatalogProductBuilder $subject
3941
* @param Product $product
4042
* @param RequestInterface $request
4143
* @return Product
4244
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4345
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
4446
*/
45-
public function afterBuild(
46-
Builder $subject,
47-
Product $product,
48-
RequestInterface $request
49-
) {
47+
public function afterBuild(CatalogProductBuilder $subject, Product $product, RequestInterface $request)
48+
{
5049
if ($request->has('attributes')) {
5150
$attributes = $request->getParam('attributes');
5251
if (!empty($attributes)) {

app/code/Magento/ConfigurableProduct/Model/Plugin/AroundProductRepositorySave.php renamed to app/code/Magento/ConfigurableProduct/Model/Plugin/ProductRepositorySave.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1616

1717
/**
18-
* Class AroundProductRepositorySave
18+
* Class ProductRepositorySave
1919
*/
20-
class AroundProductRepositorySave
20+
class ProductRepositorySave
2121
{
2222
/**
2323
* @var ProductAttributeRepositoryInterface
@@ -42,6 +42,8 @@ public function __construct(
4242
}
4343

4444
/**
45+
* Validate product links and reset configurable attributes to configurable product
46+
*
4547
* @param ProductRepositoryInterface $subject
4648
* @param ProductInterface $result
4749
* @param ProductInterface $product

app/code/Magento/ConfigurableProduct/Model/Product/Validator/Plugin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function __construct(
4848
}
4949

5050
/**
51+
* Set configurable type to product
52+
*
5153
* @param Product\Validator $subject
5254
* @param Product $product
5355
* @param RequestInterface $request
@@ -58,7 +60,7 @@ public function __construct(
5860
public function beforeValidate(
5961
\Magento\Catalog\Model\Product\Validator $subject,
6062
\Magento\Catalog\Model\Product $product,
61-
\Magento\Framework\App\RequestInterface $request,
63+
RequestInterface $request,
6264
DataObject $response
6365
) {
6466
if ($request->has('attributes')) {
@@ -81,7 +83,7 @@ public function afterValidate(
8183
\Magento\Catalog\Model\Product\Validator $subject,
8284
$result,
8385
\Magento\Catalog\Model\Product $product,
84-
\Magento\Framework\App\RequestInterface $request,
86+
RequestInterface $request,
8587
DataObject $response
8688
) {
8789
$variationProducts = (array)$request->getPost('variations-matrix');

app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ class PluginTest extends \PHPUnit_Framework_TestCase
5252
*/
5353
protected $subjectMock;
5454

55-
/**
56-
* @var \Closure
57-
*/
58-
protected $closureMock;
59-
6055
protected function setUp()
6156
{
6257
$this->productFactoryMock = $this->getMock(
@@ -124,9 +119,6 @@ protected function setUp()
124119
'',
125120
false
126121
);
127-
$this->closureMock = function () use ($product) {
128-
return $product;
129-
};
130122
$this->plugin = new \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Builder\Plugin(
131123
$this->productFactoryMock,
132124
$this->configurableTypeMock
@@ -137,7 +129,7 @@ protected function setUp()
137129
* @return void
138130
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
139131
*/
140-
public function testAroundBuild()
132+
public function testAfterBuild()
141133
{
142134
$this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(true));
143135
$valueMap = [
@@ -260,7 +252,7 @@ public function testAroundBuild()
260252
);
261253
}
262254

263-
public function testAroundBuildWhenProductNotHaveAttributeAndRequiredParameters()
255+
public function testAfterBuildWhenProductNotHaveAttributeAndRequiredParameters()
264256
{
265257
$valueMap = [
266258
['attributes', null, null],
@@ -287,7 +279,7 @@ public function testAroundBuildWhenProductNotHaveAttributeAndRequiredParameters(
287279
);
288280
}
289281

290-
public function testAroundBuildWhenAttributesAreEmpty()
282+
public function testAfterBuildWhenAttributesAreEmpty()
291283
{
292284
$valueMap = [['popup', null, false], ['product', null, 'product'], ['id', false, false]];
293285
$this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(false));
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\ProductFactory;
1313
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
14-
use Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave;
14+
use Magento\ConfigurableProduct\Model\Plugin\ProductRepositorySave;
1515
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1616
use Magento\ConfigurableProduct\Test\Unit\Model\Product\ProductExtensionAttributes;
1717
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1818

1919
/**
20-
* Class AroundProductRepositorySaveTest
20+
* Class ProductRepositorySaveTest
2121
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2222
*/
23-
class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase
23+
class ProductRepositorySaveTest extends \PHPUnit_Framework_TestCase
2424
{
2525
/**
2626
* @var ProductAttributeRepositoryInterface|MockObject
@@ -63,7 +63,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase
6363
private $option;
6464

6565
/**
66-
* @var AroundProductRepositorySave
66+
* @var ProductRepositorySave
6767
*/
6868
private $plugin;
6969

@@ -97,13 +97,13 @@ protected function setUp()
9797

9898
$this->option = $this->getMockForAbstractClass(OptionInterface::class);
9999

100-
$this->plugin = new AroundProductRepositorySave(
100+
$this->plugin = new ProductRepositorySave(
101101
$this->productAttributeRepository,
102102
$this->productFactory
103103
);
104104
}
105105

106-
public function testAroundSaveWhenProductIsSimple()
106+
public function testAfterSaveWhenProductIsSimple()
107107
{
108108
$this->product->expects(static::once())
109109
->method('getTypeId')
@@ -117,7 +117,7 @@ public function testAroundSaveWhenProductIsSimple()
117117
);
118118
}
119119

120-
public function testAroundSaveWithoutOptions()
120+
public function testAfterSaveWithoutOptions()
121121
{
122122
$this->product->expects(static::once())
123123
->method('getTypeId')
@@ -147,7 +147,7 @@ public function testAroundSaveWithoutOptions()
147147
* @expectedException \Magento\Framework\Exception\InputException
148148
* @expectedExceptionMessage Products "5" and "4" have the same set of attribute values.
149149
*/
150-
public function testAroundSaveWithLinks()
150+
public function testAfterSaveWithLinks()
151151
{
152152
$links = [4, 5];
153153
$this->product->expects(static::once())
@@ -189,7 +189,7 @@ public function testAroundSaveWithLinks()
189189
* @expectedException \Magento\Framework\Exception\InputException
190190
* @expectedExceptionMessage Product with id "4" does not contain required attribute "color".
191191
*/
192-
public function testAroundSaveWithLinksWithMissingAttribute()
192+
public function testAfterSaveWithLinksWithMissingAttribute()
193193
{
194194
$simpleProductId = 4;
195195
$links = [$simpleProductId, 5];
@@ -246,7 +246,7 @@ public function testAroundSaveWithLinksWithMissingAttribute()
246246
* @expectedException \Magento\Framework\Exception\InputException
247247
* @expectedExceptionMessage Products "5" and "4" have the same set of attribute values.
248248
*/
249-
public function testAroundSaveWithLinksWithDuplicateAttributes()
249+
public function testAfterSaveWithLinksWithDuplicateAttributes()
250250
{
251251
$links = [4, 5];
252252
$attributeCode = 'color';

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Validator/PluginTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testBeforeValidate()
119119
);
120120
}
121121

122-
public function testAroundValidateWithVariationsValid()
122+
public function testAfterValidateWithVariationsValid()
123123
{
124124
$matrix = ['products'];
125125

@@ -165,7 +165,7 @@ public function testAroundValidateWithVariationsValid()
165165
);
166166
}
167167

168-
public function testAroundValidateWithVariationsInvalid()
168+
public function testAfterValidateWithVariationsInvalid()
169169
{
170170
$matrix = ['products'];
171171

@@ -212,7 +212,7 @@ public function testAroundValidateWithVariationsInvalid()
212212
);
213213
}
214214

215-
public function testAroundValidateIfVariationsNotExist()
215+
public function testAfterValidateIfVariationsNotExist()
216216
{
217217
$this->requestMock->expects(
218218
$this->once()
@@ -233,7 +233,7 @@ public function testAroundValidateIfVariationsNotExist()
233233
);
234234
}
235235

236-
public function testAroundValidateWithVariationsAndRequiredAttributes()
236+
public function testAfterValidateWithVariationsAndRequiredAttributes()
237237
{
238238
$matrix = [
239239
['data1', 'data2', 'configurable_attribute' => ['data1']],

0 commit comments

Comments
 (0)