Skip to content
Snippets Groups Projects
Commit ee89e3cb authored by Safwan Khan's avatar Safwan Khan
Browse files

MAGETWO-36068: Move dev/shell/log.php

- Changes based on QA feedback.
parent 9751afc6
Branches
No related merge requests found
......@@ -30,7 +30,8 @@ class LogCleanCommand extends AbstractLogCommand
self::INPUT_KEY_DAYS,
null,
InputOption::VALUE_REQUIRED,
'Save log for specified number of days (minimum 1 day)'
'Save log for specified number of days',
'1'
),
];
$this->setName('log:clean')
......@@ -41,19 +42,26 @@ class LogCleanCommand extends AbstractLogCommand
/**
* {@inheritdoc}
* @throws \InvalidArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$errorMsg = 'Invalid value for option "' . self::INPUT_KEY_DAYS . '"';
$days = $input->getOption(self::INPUT_KEY_DAYS);
if ($days > 0) {
/** @var \Magento\Framework\App\Config\MutableScopeConfigInterface $mutableConfig */
$mutableConfig = $this->objectManager->create('Magento\Framework\App\Config\MutableScopeConfigInterface');
$mutableConfig->setValue(
\Magento\Log\Model\Log::XML_LOG_CLEAN_DAYS,
$days,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if (!is_numeric($days) || (strpos($days,'.') !== false)) {
throw new \InvalidArgumentException($errorMsg);
}
$days = (int) $days;
if ($days <= 0) {
throw new \InvalidArgumentException($errorMsg);
}
/** @var \Magento\Framework\App\Config\MutableScopeConfigInterface $mutableConfig */
$mutableConfig = $this->objectManager->create('Magento\Framework\App\Config\MutableScopeConfigInterface');
$mutableConfig->setValue(
\Magento\Log\Model\Log::XML_LOG_CLEAN_DAYS,
$days,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
/** @var \Magento\Log\Model\LogFactory $logFactory */
$logFactory = $this->objectManager->create('Magento\Log\Model\LogFactory');
/** @var \Magento\Log\Model\Log $model */
......
......@@ -7,9 +7,28 @@ namespace Magento\Log\Test\Unit\Console\Command;
use Magento\Log\Console\Command\LogCleanCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Magento\Framework\App\ObjectManager;
class LogCleanCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $objectManager;
/**
* @var CommandTester
*/
private $commandTester;
public function setUp()
{
$this->objectManager = $this->getMock('Magento\Framework\App\ObjectManager', [], [], '', false);
$objectManagerFactory = $this->getMock('Magento\Framework\App\ObjectManagerFactory', [], [], '', false);
$objectManagerFactory->expects($this->once())->method('create')->willReturn($this->objectManager);
$this->commandTester = new CommandTester(new LogCleanCommand($objectManagerFactory));
}
public function testExecute()
{
$mutableConfig = $this->getMock('Magento\Framework\App\Config\MutableScopeConfigInterface', [], [], '', false);
......@@ -26,16 +45,41 @@ class LogCleanCommandTest extends \PHPUnit_Framework_TestCase
$logFactory,
],
];
$objectManager = $this->getMock('Magento\Framework\App\ObjectManager', [], [], '', false);
$objectManager->expects($this->exactly(2))->method('create')->will($this->returnValueMap($returnValueMap));
$this->objectManager
->expects($this->exactly(2))->method('create')
->will($this->returnValueMap($returnValueMap));
$mutableConfig->expects($this->once())->method('setValue');
$log = $this->getMock('Magento\Log\Model\Log', [], [], '', false);
$logFactory->expects($this->once())->method('create')->willReturn($log);
$log->expects($this->once())->method('clean');
$objectManagerFactory = $this->getMock('Magento\Framework\App\ObjectManagerFactory', [], [], '', false);
$objectManagerFactory->expects($this->once())->method('create')->willReturn($objectManager);
$commandTester = new CommandTester(new LogCleanCommand($objectManagerFactory));
$commandTester->execute(['--days' => '1']);
$this->assertEquals('Log cleaned.' . PHP_EOL, $commandTester->getDisplay());
$this->commandTester->execute(['--days' => '1']);
$this->assertEquals('Log cleaned.' . PHP_EOL, $this->commandTester->getDisplay());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid value for option "days"
*/
public function testExecuteInvalidNegativeDays()
{
$this->commandTester->execute(['--days' => '-1']);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid value for option "days"
*/
public function testExecuteInvalidFractionDays()
{
$this->commandTester->execute(['--days' => '5.5']);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid value for option "days"
*/
public function testExecuteInvalidTexyDays()
{
$this->commandTester->execute(['--days' => 'test']);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment