diff --git a/app/code/Magento/Log/Console/Command/LogCleanCommand.php b/app/code/Magento/Log/Console/Command/LogCleanCommand.php index 76900912d8dcadb9dacb942fd2d69de5149c445b..8934199373fffc7f9ad19f6cbbd10baa0a4225b2 100644 --- a/app/code/Magento/Log/Console/Command/LogCleanCommand.php +++ b/app/code/Magento/Log/Console/Command/LogCleanCommand.php @@ -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 */ diff --git a/app/code/Magento/Log/Test/Unit/Console/Command/LogCleanCommandTest.php b/app/code/Magento/Log/Test/Unit/Console/Command/LogCleanCommandTest.php index 1e5ce8f05e6c314c3b88c816817f951c679cdbec..2b742e8ceeba227aed5f26655bdf62f86969f07b 100644 --- a/app/code/Magento/Log/Test/Unit/Console/Command/LogCleanCommandTest.php +++ b/app/code/Magento/Log/Test/Unit/Console/Command/LogCleanCommandTest.php @@ -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']); } }