Skip to content

Commit a355760

Browse files
author
Dale Sikkema
committed
MAGETWO-43787: cache:clean command doesn't clear Varnish cache
- throw events to handle page caching from CLI tools
1 parent f8a4c72 commit a355760

10 files changed

+54
-18
lines changed

app/code/Magento/Backend/Console/Command/AbstractCacheTypeManageCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,28 @@
66

77
namespace Magento\Backend\Console\Command;
88

9+
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
12+
use Magento\Framework\App\Cache\Manager;
1113

1214
abstract class AbstractCacheTypeManageCommand extends AbstractCacheManageCommand
1315
{
16+
/** @var EventManagerInterface */
17+
protected $eventManager;
18+
19+
/**
20+
* @param Manager $cacheManager
21+
* @param EventManagerInterface $eventManager
22+
*/
23+
public function __construct(
24+
Manager $cacheManager,
25+
EventManagerInterface $eventManager
26+
) {
27+
$this->eventManager = $eventManager;
28+
parent::__construct($cacheManager);
29+
}
30+
1431
/**
1532
* Perform a cache management action on cache types
1633
*

app/code/Magento/Backend/Console/Command/CacheCleanCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function configure()
2929
*/
3030
protected function performAction(array $cacheTypes)
3131
{
32+
$this->eventManager->dispatch('adminhtml_cache_flush_system');
3233
$this->cacheManager->clean($cacheTypes);
3334
}
3435

app/code/Magento/Backend/Console/Command/CacheFlushCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function configure()
2929
*/
3030
protected function performAction(array $cacheTypes)
3131
{
32+
$this->eventManager->dispatch('adminhtml_cache_flush_all');
3233
$this->cacheManager->flush($cacheTypes);
3334
}
3435

app/code/Magento/Backend/Test/Unit/Console/Command/AbstractCacheCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class AbstractCacheCommandTest extends \PHPUnit_Framework_TestCase
1313
/**
1414
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
1515
*/
16-
protected $cacheManager;
16+
protected $cacheManagerMock;
1717

1818
/**
1919
* @var AbstractCacheManageCommand
@@ -22,7 +22,7 @@ abstract class AbstractCacheCommandTest extends \PHPUnit_Framework_TestCase
2222

2323
public function setUp()
2424
{
25-
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
25+
$this->cacheManagerMock = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
2626
}
2727

2828
/**

app/code/Magento/Backend/Test/Unit/Console/Command/AbstractCacheManageCommandTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@
1010

1111
abstract class AbstractCacheManageCommandTest extends AbstractCacheCommandTest
1212
{
13+
/** @var string */
14+
protected $cacheEventName;
15+
16+
/** @var \Magento\Framework\Event\ManagerInterface */
17+
protected $eventManagerMock;
18+
19+
public function setUp()
20+
{
21+
$this->eventManagerMock = $this->getMockBuilder('\Magento\Framework\Event\ManagerInterface')
22+
->disableOriginalConstructor()
23+
->getMock();
24+
$this->eventManagerMock->expects($this->any())->method('dispatch');
25+
parent::setUp();
26+
}
27+
1328
/**
1429
* @return array
1530
*/
@@ -35,7 +50,7 @@ public function testExecuteDataProvider()
3550
*/
3651
public function testExecuteInvalidCacheType()
3752
{
38-
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
53+
$this->cacheManagerMock->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
3954
$param = ['types' => ['A', 'D']];
4055
$commandTester = new CommandTester($this->command);
4156
$commandTester->execute($param);

app/code/Magento/Backend/Test/Unit/Console/Command/CacheCleanCommandTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ class CacheCleanCommandTest extends AbstractCacheManageCommandTest
1313
{
1414
public function setUp()
1515
{
16+
$this->cacheEventName = 'adminhtml_cache_flush_system';
1617
parent::setUp();
17-
$this->command = new CacheCleanCommand($this->cacheManager);
18+
$this->command = new CacheCleanCommand($this->cacheManagerMock, $this->eventManagerMock);
1819
}
1920

2021
/**
@@ -25,8 +26,8 @@ public function setUp()
2526
*/
2627
public function testExecute($param, $types, $output)
2728
{
28-
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
29-
$this->cacheManager->expects($this->once())->method('clean')->with($types);
29+
$this->cacheManagerMock->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30+
$this->cacheManagerMock->expects($this->once())->method('clean')->with($types);
3031

3132
$commandTester = new CommandTester($this->command);
3233
$commandTester->execute($param);

app/code/Magento/Backend/Test/Unit/Console/Command/CacheDisableCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CacheDisableCommandTest extends AbstractCacheSetCommandTest
1414
public function setUp()
1515
{
1616
parent::setUp();
17-
$this->command = new CacheDisableCommand($this->cacheManager);
17+
$this->command = new CacheDisableCommand($this->cacheManagerMock);
1818
}
1919

2020
/**
@@ -26,8 +26,8 @@ public function setUp()
2626
*/
2727
public function testExecute($param, $enable, $result, $output)
2828
{
29-
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30-
$this->cacheManager->expects($this->once())->method('setEnabled')->with($enable, false)->willReturn($result);
29+
$this->cacheManagerMock->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30+
$this->cacheManagerMock->expects($this->once())->method('setEnabled')->with($enable, false)->willReturn($result);
3131

3232
$commandTester = new CommandTester($this->command);
3333
$commandTester->execute($param);

app/code/Magento/Backend/Test/Unit/Console/Command/CacheEnableCommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CacheEnableCommandTest extends AbstractCacheSetCommandTest
1414
public function setUp()
1515
{
1616
parent::setUp();
17-
$this->command = new CacheEnableCommand($this->cacheManager);
17+
$this->command = new CacheEnableCommand($this->cacheManagerMock);
1818
}
1919

2020
/**
@@ -26,9 +26,9 @@ public function setUp()
2626
*/
2727
public function testExecute($param, $enable, $result, $output)
2828
{
29-
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30-
$this->cacheManager->expects($this->once())->method('setEnabled')->with($enable, true)->willReturn($result);
31-
$this->cacheManager->expects($result === [] ? $this->never() : $this->once())->method('clean')->with($enable);
29+
$this->cacheManagerMock->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30+
$this->cacheManagerMock->expects($this->once())->method('setEnabled')->with($enable, true)->willReturn($result);
31+
$this->cacheManagerMock->expects($result === [] ? $this->never() : $this->once())->method('clean')->with($enable);
3232

3333
$commandTester = new CommandTester($this->command);
3434
$commandTester->execute($param);

app/code/Magento/Backend/Test/Unit/Console/Command/CacheFlushCommandTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ class CacheFlushCommandTest extends AbstractCacheManageCommandTest
1313
{
1414
public function setUp()
1515
{
16+
$this->cacheEventName = 'adminhtml_cache_flush_all';
1617
parent::setUp();
17-
$this->command = new CacheFlushCommand($this->cacheManager);
18+
$this->command = new CacheFlushCommand($this->cacheManagerMock, $this->eventManagerMock);
1819
}
1920

2021
/**
@@ -25,8 +26,8 @@ public function setUp()
2526
*/
2627
public function testExecute($param, $types, $output)
2728
{
28-
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
29-
$this->cacheManager->expects($this->once())->method('flush')->with($types);
29+
$this->cacheManagerMock->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
30+
$this->cacheManagerMock->expects($this->once())->method('flush')->with($types);
3031

3132
$commandTester = new CommandTester($this->command);
3233
$commandTester->execute($param);

app/code/Magento/Backend/Test/Unit/Console/Command/CacheStatusCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class CacheStatusCommandTest extends AbstractCacheCommandTest
1414
public function setUp()
1515
{
1616
parent::setUp();
17-
$this->command = new CacheStatusCommand($this->cacheManager);
17+
$this->command = new CacheStatusCommand($this->cacheManagerMock);
1818
}
1919

2020
public function testExecute()
2121
{
2222
$cacheTypes = ['A' => 0, 'B' => 1, 'C' => 1];
23-
$this->cacheManager->expects($this->once())->method('getStatus')->willReturn($cacheTypes);
23+
$this->cacheManagerMock->expects($this->once())->method('getStatus')->willReturn($cacheTypes);
2424
$commandTester = new CommandTester($this->command);
2525
$commandTester->execute([]);
2626

0 commit comments

Comments
 (0)