Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Console Tool Usage for Cache and Index Operations #1440

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

abstract class AbstractCacheManageCommand extends AbstractCacheCommand
{
Expand All @@ -17,11 +16,6 @@ abstract class AbstractCacheManageCommand extends AbstractCacheCommand
*/
const INPUT_KEY_TYPES = 'types';

/**
* Input key all
*/
const INPUT_KEY_ALL = 'all';

/**
* {@inheritdoc}
*/
Expand All @@ -32,16 +26,9 @@ protected function configure()
InputArgument::IS_ARRAY,
'List of cache types, space separated. If omitted, all caches will be affected'
);
$this->addOption(
self::INPUT_KEY_ALL,
null,
InputOption::VALUE_NONE,
'All cache types'
);
parent::configure();
}


/**
* Get requested cache types
*
Expand All @@ -56,7 +43,7 @@ protected function getRequestedTypes(InputInterface $input)
$requestedTypes = array_filter(array_map('trim', $requestedTypes), 'strlen');
}
if (empty($requestedTypes)) {
return [];
return $this->cacheManager->getAvailableTypes();
} else {
$availableTypes = $this->cacheManager->getAvailableTypes();
$unsupportedTypes = array_diff($requestedTypes, $availableTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ abstract protected function isEnable();
protected function execute(InputInterface $input, OutputInterface $output)
{
$isEnable = $this->isEnable();
if ($input->getOption(self::INPUT_KEY_ALL)) {
$types = $this->cacheManager->getAvailableTypes();
} else {
$types = $this->getRequestedTypes($input);
}
$types = $this->getRequestedTypes($input);
$changedTypes = $this->cacheManager->setEnabled($types, $isEnable);
if ($changedTypes) {
$output->writeln('Changed cache status:');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ abstract protected function getDisplayMessage();
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption(self::INPUT_KEY_ALL)) {
$types = $this->cacheManager->getAvailableTypes();
} else {
$types = $this->getRequestedTypes($input);
}
$types = $this->getRequestedTypes($input);
$this->performAction($types);
$output->writeln($this->getDisplayMessage());
$output->writeln(join(PHP_EOL, $types));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Test\Unit\Console\Command;

use Magento\Backend\Console\Command\AbstractCacheManageCommand;

abstract class AbstractCacheCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cacheManager;

/**
* @var AbstractCacheManageCommand
*/
protected $command;

public function setUp()
{
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
}

/**
* Formats expected output for testExecute data providers
*
* @param array $types
* @return string
*/
abstract function getExpectedExecutionOutput(array $types);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Test\Unit\Console\Command;

use Symfony\Component\Console\Tester\CommandTester;

abstract class AbstractCacheManageCommandTest extends AbstractCacheCommandTest
{
/**
* @return array
*/
public function testExecuteDataProvider()
{
return [
'implicit all' => [
[],
['A', 'B', 'C'],
$this->getExpectedExecutionOutput(['A', 'B', 'C']),
],
'specified types' => [
['types' => ['A', 'B']],
['A', 'B'],
$this->getExpectedExecutionOutput(['A', 'B']),
],
];
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The following requested cache types are not supported:
*/
public function testExecuteInvalidCacheType()
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$param = ['types' => ['A', 'D']];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Test\Unit\Console\Command;

abstract class AbstractCacheSetCommandTest extends AbstractCacheManageCommandTest
{
/**
* @return array
*/
public function testExecuteDataProvider()
{
return [
'implicit all' => [
[],
['A', 'B', 'C'],
['A', 'B', 'C'],
$this->getExpectedExecutionOutput(['A', 'B', 'C']),
],
'specified types' => [
['types' => ['A', 'B']],
['A', 'B'],
['A', 'B'],
$this->getExpectedExecutionOutput(['A', 'B']),
],
'no changes' => [
['types' => ['A', 'B']],
['A', 'B'],
[],
$this->getExpectedExecutionOutput([]),
],
];
}

/**
* Formats expected output of cache status change
*
* @param array $changes
* @param bool $enabled
* @return string
*/
public function getExpectedChangeOutput(array $changes, $enabled)
{
if ($changes) {
$output = 'Changed cache status:' . PHP_EOL;
foreach ($changes as $type) {
$output .= sprintf('%30s: %d -> %d', $type, $enabled === false, $enabled === true) . PHP_EOL;
}
} else {
$output = 'There is nothing to change in cache status' . PHP_EOL;
}
return $output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,39 @@
use Magento\Backend\Console\Command\CacheCleanCommand;
use Symfony\Component\Console\Tester\CommandTester;

class CacheCleanCommandTest extends \PHPUnit_Framework_TestCase
class CacheCleanCommandTest extends AbstractCacheManageCommandTest
{
/**
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
private $cacheManager;

/**
* @var CacheCleanCommand
*/
private $command;

public function setUp()
{
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
parent::setUp();
$this->command = new CacheCleanCommand($this->cacheManager);
}

public function testExecute()
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager->expects($this->once())->method('clean')->with(['A', 'B']);
$param = ['types' => ['A', 'B']];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);
$expect = 'Cleaned cache types:' . PHP_EOL . 'A' . PHP_EOL . 'B' . PHP_EOL;
$this->assertEquals($expect, $commandTester->getDisplay());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The following requested cache types are not supported:
* @param array $param
* @param array $types
* @param string $output
* @dataProvider testExecuteDataProvider
*/
public function testExecuteInvalidCacheType()
public function testExecute($param, $types, $output)
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$param = ['types' => ['A', 'D']];
$this->cacheManager->expects($this->once())->method('clean')->with($types);

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

$this->assertEquals($output, $commandTester->getDisplay());
}

public function testExecuteAllCacheType()
/**
* Get expected output based on set of types operated on
*
* @param array $types
* @return string
*/
public function getExpectedExecutionOutput(array $types)
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager->expects($this->once())->method('clean')->with(['A', 'B', 'C']);
$param = ['--all' => true];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);
$expect = 'Cleaned cache types:' . PHP_EOL . 'A' . PHP_EOL . 'B' . PHP_EOL . 'C' . PHP_EOL;
$this->assertEquals($expect, $commandTester->getDisplay());
return 'Cleaned cache types:' . PHP_EOL . implode(PHP_EOL, $types) . PHP_EOL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,37 @@
use Magento\Backend\Console\Command\CacheDisableCommand;
use Symfony\Component\Console\Tester\CommandTester;

class CacheDisableCommandTest extends \PHPUnit_Framework_TestCase
class CacheDisableCommandTest extends AbstractCacheSetCommandTest
{
/**
* @var \Magento\Framework\App\Cache\Manager|\PHPUnit_Framework_MockObject_MockObject
*/
private $cacheManager;

/**
* @var CacheDisableCommand
*/
private $command;

public function setUp()
{
$this->cacheManager = $this->getMock('Magento\Framework\App\Cache\Manager', [], [], '', false);
parent::setUp();
$this->command = new CacheDisableCommand($this->cacheManager);
}

public function testExecute()
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager
->expects($this->once())
->method('setEnabled')
->with(['A', 'B'], false)
->willReturn(['A', 'B']);
$param = ['types' => ['A', 'B']];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);

$expect = 'Changed cache status:' . PHP_EOL;
foreach (['A', 'B'] as $cacheType) {
$expect .= sprintf('%30s: %d -> %d', $cacheType, true, false) . PHP_EOL;
}

$this->assertEquals($expect, $commandTester->getDisplay());
}

public function testExecuteAll()
/**
* @param array $param
* @param array $enable
* @param array $result
* @param string $output
* @dataProvider testExecuteDataProvider
*/
public function testExecute($param, $enable, $result, $output)
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager
->expects($this->once())
->method('setEnabled')
->with(['A', 'B', 'C'], false)
->willReturn(['A', 'B', 'C']);
$param = ['--all' => true];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);

$expect = 'Changed cache status:' . PHP_EOL;
foreach (['A', 'B', 'C'] as $cacheType) {
$expect .= sprintf('%30s: %d -> %d', $cacheType, true, false) . PHP_EOL;
}
$this->assertEquals($expect, $commandTester->getDisplay());
}
$this->cacheManager->expects($this->once())->method('setEnabled')->with($enable, false)->willReturn($result);

public function testExecuteNoChanges()
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$this->cacheManager
->expects($this->once())
->method('setEnabled')
->with(['A', 'B'], false)
->willReturn([]);
$this->cacheManager->expects($this->never())->method('clean');
$param = ['types' => ['A', 'B']];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);

$expect = 'There is nothing to change in cache status' . PHP_EOL;
$this->assertEquals($expect, $commandTester->getDisplay());
$this->assertEquals($output, $commandTester->getDisplay());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The following requested cache types are not supported:
* {@inheritdoc}
*/
public function testExecuteInvalidCacheType()
public function getExpectedExecutionOutput(array $changes)
{
$this->cacheManager->expects($this->once())->method('getAvailableTypes')->willReturn(['A', 'B', 'C']);
$param = ['types' => ['A', 'D']];
$commandTester = new CommandTester($this->command);
$commandTester->execute($param);
return $this->getExpectedChangeOutput($changes, false);
}
}
Loading