Skip to content

Commit 5f8f441

Browse files
author
Yaroslav Onischenko
committed
Merge remote-tracking branch 'origin/MAGETWO-51072' into develop
2 parents 40aba3f + 57f5371 commit 5f8f441

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Plugin\Catalog;
7+
8+
class CacheInvalidate
9+
{
10+
/**
11+
* @var \Magento\Framework\App\Cache\TypeListInterface
12+
*/
13+
private $typeList;
14+
15+
/**
16+
* @var \Magento\Swatches\Helper\Data
17+
*/
18+
private $swatchHelper;
19+
20+
/**
21+
* @param \Magento\Framework\App\Cache\TypeListInterface $typeList
22+
* @param \Magento\Swatches\Helper\Data $swatchHelper
23+
*/
24+
public function __construct(
25+
\Magento\Framework\App\Cache\TypeListInterface $typeList,
26+
\Magento\Swatches\Helper\Data $swatchHelper
27+
) {
28+
$this->typeList = $typeList;
29+
$this->swatchHelper = $swatchHelper;
30+
}
31+
32+
/**
33+
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject
34+
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $result
35+
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute
36+
*/
37+
public function afterSave(
38+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject,
39+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $result
40+
) {
41+
if ($this->swatchHelper->isSwatchAttribute($subject)) {
42+
$this->typeList->invalidate('block_html');
43+
$this->typeList->invalidate('collections');
44+
$this->typeList->invalidate('full_page');
45+
}
46+
return $result;
47+
}
48+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Test\Unit\Plugin\Catalog;
7+
8+
class CacheInvalidateTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Framework\App\Cache\TypeListInterface | \PHPUnit_Framework_MockObject_MockObject
12+
*/
13+
private $typeList;
14+
15+
/**
16+
* @var \Magento\Swatches\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $swatchHelper;
19+
20+
/**
21+
* @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute | \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $attribute;
24+
25+
/**
26+
* @var \Magento\Swatches\Plugin\Catalog\CacheInvalidate
27+
*/
28+
private $cacheInvalidate;
29+
30+
protected function setUp()
31+
{
32+
$this->typeList = $this->getMock(
33+
'\Magento\Framework\App\Cache\TypeListInterface',
34+
[],
35+
[],
36+
'',
37+
false
38+
);
39+
$this->swatchHelper = $this->getMock(
40+
'\Magento\Swatches\Helper\Data',
41+
[],
42+
[],
43+
'',
44+
false
45+
);
46+
$this->attribute = $this->getMock(
47+
'\Magento\Catalog\Model\ResourceModel\Eav\Attribute',
48+
[],
49+
[],
50+
'',
51+
false
52+
);
53+
54+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
55+
$this->cacheInvalidate = $objectManager->getObject(
56+
'Magento\Swatches\Plugin\Catalog\CacheInvalidate',
57+
[
58+
'typeList' => $this->typeList,
59+
'swatchHelper' => $this->swatchHelper
60+
]
61+
);
62+
}
63+
64+
public function testAfterSaveSwatch()
65+
{
66+
$this->swatchHelper->expects($this->atLeastOnce())->method('isSwatchAttribute')->with($this->attribute)
67+
->willReturn(true);
68+
$this->typeList->expects($this->at(0))->method('invalidate')->with('block_html');
69+
$this->typeList->expects($this->at(1))->method('invalidate')->with('collections');
70+
$this->typeList->expects($this->at(2))->method('invalidate')->with('full_page');
71+
$this->assertSame($this->attribute, $this->cacheInvalidate->afterSave($this->attribute, $this->attribute));
72+
}
73+
74+
public function testAfterSaveNotSwatch()
75+
{
76+
$this->swatchHelper->expects($this->atLeastOnce())->method('isSwatchAttribute')->with($this->attribute)
77+
->willReturn(false);
78+
$this->typeList->expects($this->never())->method('invalidate');
79+
$this->assertSame($this->attribute, $this->cacheInvalidate->afterSave($this->attribute, $this->attribute));
80+
}
81+
}

app/code/Magento/Swatches/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424
<argument name="attributesList" xsi:type="object">Magento\Swatches\Model\AttributesList</argument>
2525
</arguments>
2626
</type>
27+
<type name="\Magento\Catalog\Model\ResourceModel\Eav\Attribute">
28+
<plugin name="invalidate_caches_after_attribute_save" type="Magento\Swatches\Plugin\Catalog\CacheInvalidate" />
29+
</type>
2730
</config>

0 commit comments

Comments
 (0)