Skip to content

Commit

Permalink
MAGETWO-39841: [GitHub] Running 'log:clean' through cli results in an…
Browse files Browse the repository at this point in the history
… error: 'Area code not set' #1442

- updated tests
  • Loading branch information
Olga Kopylova committed Jul 15, 2015
1 parent 588bb40 commit 126c4aa
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class IndexerCommandCommonTestSetup extends \PHPUnit_Framework_TestCase
*/
protected $indexerFactory;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
*/
protected $stateMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|CollectionFactory
*/
Expand All @@ -33,16 +38,13 @@ protected function setUp()
{
$this->objectManagerFactory = $this->getMock('Magento\Framework\App\ObjectManagerFactory', [], [], '', false);
$this->objectManager = $this->getMockForAbstractClass('Magento\Framework\ObjectManagerInterface');
$this->objectManagerFactory->expects($this->any())->method('create')->willReturn($this->objectManager);

//TODO: temporary fix unit
$stateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml')->willReturnSelf();

$this->objectManager->expects($this->once())
$this->stateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$this->objectManager->expects($this->any())
->method('get')
->with('Magento\Framework\App\State')
->willReturn($stateMock);
$this->objectManagerFactory->expects($this->once())->method('create')->willReturn($this->objectManager);
->willReturn($this->stateMock);

$this->collectionFactory = $this->getMockBuilder('Magento\Indexer\Model\Indexer\CollectionFactory')
->disableOriginalConstructor()
Expand All @@ -54,13 +56,11 @@ protected function setUp()
->getMock();

$this->objectManager
->expects($this->exactly(2))
->expects($this->any())
->method('create')
->will($this->returnValueMap([
['Magento\Indexer\Model\Indexer\CollectionFactory', [], $this->collectionFactory],
['Magento\Indexer\Model\IndexerFactory', [], $this->indexerFactory],
]));

$this->objectManagerFactory->expects($this->once())->method('create')->willReturn($this->objectManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class IndexerInfoCommandTest extends IndexerCommandCommonTestSetup
*/
private $command;

protected function setUp()
{
parent::setUp();
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
}

public function testExecute()
{
$collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false);
Expand All @@ -26,7 +32,6 @@ public function testExecute()
$collection->expects($this->once())->method('getItems')->willReturn([$indexerOne]);

$this->collectionFactory->expects($this->once())->method('create')->will($this->returnValue($collection));
$this->indexerFactory->expects($this->never())->method('create');
$this->command = new IndexerInfoCommand($this->objectManagerFactory);
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class IndexerReindexCommandTest extends IndexerCommandCommonTestSetup

public function testGetOptions()
{
$this->stateMock->expects($this->never())->method('setAreaCode');
$this->command = new IndexerReindexCommand($this->objectManagerFactory);
$optionsList = $this->command->getInputList();
$this->assertSame(2, sizeof($optionsList));
Expand All @@ -28,6 +29,7 @@ public function testGetOptions()

public function testExecuteAll()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false);
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
Expand All @@ -44,6 +46,7 @@ public function testExecuteAll()

public function testExecuteWithIndex()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$indexerOne->expects($this->once())->method('reindexAll');
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
Expand All @@ -66,6 +69,7 @@ public function testExecuteWithIndex()

public function testExecuteWithLocalizedException()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$localizedException = new \Magento\Framework\Exception\LocalizedException(__('Some Exception Message'));
$indexerOne->expects($this->once())->method('reindexAll')->will($this->throwException($localizedException));
Expand All @@ -80,6 +84,7 @@ public function testExecuteWithLocalizedException()

public function testExecuteWithException()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$exception = new \Exception();
$indexerOne->expects($this->once())->method('reindexAll')->will($this->throwException($exception));
Expand All @@ -95,6 +100,7 @@ public function testExecuteWithException()

public function testExecuteWithExceptionInLoad()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$exception = new \Exception();
$indexerOne->expects($this->once())->method('load')->will($this->throwException($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class IndexerSetModeCommandTest extends IndexerCommandCommonTestSetup

public function testGetOptions()
{
$this->stateMock->expects($this->never())->method('setAreaCode')->with('adminmhtml');
$this->command = new IndexerSetModeCommand($this->objectManagerFactory);
$optionsList = $this->command->getInputList();
$this->assertSame(3, sizeof($optionsList));
Expand All @@ -36,6 +37,7 @@ public function testGetOptions()
*/
public function testExecuteInvalidArgument()
{
$this->stateMock->expects($this->never())->method('setAreaCode')->with('adminmhtml');
$this->command = new IndexerSetModeCommand($this->objectManagerFactory);
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);
Expand All @@ -47,13 +49,15 @@ public function testExecuteInvalidArgument()
*/
public function testExecuteInvalidMode()
{
$this->stateMock->expects($this->never())->method('setAreaCode')->with('adminmhtml');
$this->command = new IndexerSetModeCommand($this->objectManagerFactory);
$commandTester = new CommandTester($this->command);
$commandTester->execute(['mode' => 'wrong_mode']);
}

public function testExecuteAll()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false);
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);

Expand Down Expand Up @@ -87,6 +91,7 @@ public function testExecuteAll()
*/
public function testExecuteWithIndex($isScheduled, $previous, $current, $mode, $expectedValue)
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
$indexerOne->expects($this->once())->method('load')->with('id_indexerOne')->willReturn($indexerOne);
Expand Down Expand Up @@ -147,6 +152,7 @@ public function executeWithIndexDataProvider()

public function testExecuteWithLocalizedException()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$localizedException = new \Magento\Framework\Exception\LocalizedException(__('Some Exception Message'));
$indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($localizedException));
Expand All @@ -161,6 +167,7 @@ public function testExecuteWithLocalizedException()

public function testExecuteWithException()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$exception = new \Exception();
$indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class IndexerShowModeCommandTest extends IndexerCommandCommonTestSetup

public function testGetOptions()
{
$this->stateMock->expects($this->never())->method('setAreaCode')->with('adminmhtml');
$this->command = new IndexerShowModeCommand($this->objectManagerFactory);
$optionsList = $this->command->getInputList();
$this->assertSame(2, sizeof($optionsList));
Expand All @@ -28,7 +29,6 @@ public function testGetOptions()

public function testExecuteAll()
{

$collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false);
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class IndexerStatusCommandTest extends IndexerCommandCommonTestSetup

public function testExecuteAll()
{
$this->stateMock->expects($this->once())->method('setAreaCode')->with('adminmhtml');
$collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false);
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Log\Test\Unit\Console\Command;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Log\Console\Command\LogCleanCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Magento\Framework\App\ObjectManager;
Expand All @@ -23,10 +24,17 @@ class LogCleanCommandTest extends \PHPUnit_Framework_TestCase

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));
$this->objectManager = $this->getMockForAbstractClass('Magento\Framework\ObjectManagerInterface');
$configLoader = $this->getMock('Magento\Framework\App\ObjectManager\ConfigLoader', [], [], '', false);
$state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$configLoader->expects($this->once())
->method('load')
->with(FrontNameResolver::AREA_CODE)
->will($this->returnValue([]));
$state->expects($this->once())
->method('setAreaCode')
->with(FrontNameResolver::AREA_CODE);
$this->commandTester = new CommandTester(new LogCleanCommand($this->objectManager, $configLoader, $state));
}

public function testExecute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@
*/
namespace Magento\Log\Test\Unit\Console\Command;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Log\Console\Command\LogStatusCommand;
use Symfony\Component\Console\Tester\CommandTester;

class LogStatusCommandTest extends \PHPUnit_Framework_TestCase
{
public function testExecute()
{
$objectManagerFactory = $this->getMock('Magento\Framework\App\ObjectManagerFactory', [], [], '', false);
$objectManager = $this->getMock('Magento\Framework\App\ObjectManager', [], [], '', false);
$resourceFactory = $this->getMockBuilder('Magento\Log\Model\Resource\ShellFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$objectManager->expects($this->once())->method('create')->willReturn($resourceFactory);
$resource = $this->getMock('Magento\Log\Model\Resource\Shell', [], [], '', false);
$resourceFactory->expects($this->once())->method('create')->willReturn($resource);
$resource->expects($this->once())->method('getTablesInfo')->willReturn(
[['name' => 'log_customer', 'rows' => '1', 'data_length' => '16384', 'index_length' => '1024']]
);
$objectManagerFactory->expects($this->once())->method('create')->willReturn($objectManager);
$commandTester = new CommandTester(new LogStatusCommand($objectManagerFactory));
$objectManager = $this->getMockForAbstractClass('Magento\Framework\ObjectManagerInterface');
$objectManager->expects($this->once())->method('create')->willReturn($resourceFactory);
$configLoader = $this->getMock('Magento\Framework\App\ObjectManager\ConfigLoader', [], [], '', false);
$state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$configLoader->expects($this->once())
->method('load')
->with(FrontNameResolver::AREA_CODE)
->will($this->returnValue([]));
$state->expects($this->once())
->method('setAreaCode')
->with(FrontNameResolver::AREA_CODE);
$commandTester = new CommandTester(new LogStatusCommand($objectManager, $configLoader, $state));

$commandTester->execute([]);
$expectedMsg = '-----------------------------------+------------+------------+------------+' . PHP_EOL
. 'Table Name | Rows | Data Size | Index Size |' . PHP_EOL
Expand Down

0 comments on commit 126c4aa

Please sign in to comment.