Skip to content

Commit

Permalink
Add workaround for issue 1405 where the area is not set during cli co…
Browse files Browse the repository at this point in the history
…mmands

Reference magento/magento2#1405
  • Loading branch information
Vinai committed Jul 14, 2015
1 parent 5ce3b62 commit 7677c20
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 15 deletions.
23 changes: 22 additions & 1 deletion src/Setup/EavOptionSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Eav\Api\Data\AttributeOptionInterface as AttributeOption;
use Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory as AttributeOptionLabelFactory;
use Magento\Eav\Api\Data\AttributeOptionLabelInterface as AttributeOptionLabel;
use Magento\Framework\App\State as AppState;

class EavOptionSetup
{
Expand Down Expand Up @@ -44,16 +45,23 @@ class EavOptionSetup
*/
private $attributeOptionLabelFactory;

/**
* @var AppState
*/
private $appState;

public function __construct(
AttributeRepository $attributeRepository,
AttributeOptionManagementService $attributeOptionManagementService,
AttributeOptionFactory $attributeOptionFactory,
AttributeOptionLabelFactory $attributeOptionLabelFactory
AttributeOptionLabelFactory $attributeOptionLabelFactory,
AppState $appState
) {
$this->attributeRepository = $attributeRepository;
$this->attrOptionManagementService = $attributeOptionManagementService;
$this->attributeOptionFactory = $attributeOptionFactory;
$this->attributeOptionLabelFactory = $attributeOptionLabelFactory;
$this->appState = $appState;
}

/**
Expand Down Expand Up @@ -180,6 +188,7 @@ private function getDefaultOptionLabelsAsStrings()
*/
private function addAttributeOption($defaultLabel, array $storeScopeLabels)
{
$this->workaroundIssue1405();
$this->attrOptionManagementService->add(
$this->attribute->getEntityTypeId(),
$this->attribute->getAttributeCode(),
Expand Down Expand Up @@ -236,4 +245,16 @@ private function getMaxAttributeSortOrder()
return max($max, $option->getSortOrder());
}, 0);
}

/**
* Reference https://github.com/magento/magento2/issues/1405
* Remove this method once the issue is resolved that calling an Api Interface
* method from a Module Install triggers Exception that Area Code is not set
*/
private function workaroundIssue1405()
{
if (!$this->appState->getAreaCode()) {
$this->appState->setAreaCode('adminhtml');
}
}
}
104 changes: 90 additions & 14 deletions src/Test/EavOptionSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Api\Data\AttributeOptionLabelInterface;
use Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use VinaiKopp\EavOptionSetup\Setup\EavOptionSetup;

/**
Expand Down Expand Up @@ -42,6 +45,11 @@ class EavOptionSetupTest extends \PHPUnit_Framework_TestCase
*/
private $mockAttributeOptionLabelFactory;

/**
* @var AppState|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockAppState;

/**
* @param string $testLabel
* @param int $testSortOrder
Expand Down Expand Up @@ -78,14 +86,30 @@ protected function setUp()
{
$this->mockAttributeRepository = $this->getMock(AttributeRepositoryInterface::class);
$this->mockAttributeOptionManagementService = $this->getMock(AttributeOptionManagementInterface::class);
$this->mockAttributeOptionFactory = $this->getMock(AttributeOptionInterfaceFactory::class, ['create']);
$this->mockAttributeOptionLabelFactory = $this->getMock(AttributeOptionLabelInterfaceFactory::class, ['create']);

$this->mockAttributeOptionFactory = $this->getMock(
AttributeOptionInterfaceFactory::class,
['create'],
[],
'',
false
);

$this->mockAttributeOptionLabelFactory = $this->getMock(
AttributeOptionLabelInterfaceFactory::class,
['create'],
[],
'',
false
);

$this->mockAppState = $this->getMock(AppState::class, [], [], '', false);

$this->optionSetup = new EavOptionSetup(
$this->mockAttributeRepository,
$this->mockAttributeOptionManagementService,
$this->mockAttributeOptionFactory,
$this->mockAttributeOptionLabelFactory
$this->mockAttributeOptionLabelFactory,
$this->mockAppState
);
}

Expand All @@ -95,7 +119,7 @@ protected function setUp()
public function itShouldThrowIfAdminLabelIsSpecifiedAsStoreScopeLabel()
{
$this->setExpectedException(\RuntimeException::class);

$this->optionSetup->addAttributeOptionIfNotExistsWithStoreLabels(
'entity_type',
'attribute_code',
Expand Down Expand Up @@ -137,7 +161,7 @@ public function itShouldThrowAnExceptionIfTheAttributeIsNotKnown()
* @test
* @dataProvider unexpectedReturnValueProvider
*/
public function itShouldThrowAnExceptionIfTheRepositoryReturnsAUnexpectedResult($returnValue)
public function itShouldThrowAnExceptionIfTheRepositoryReturnsAnUnexpectedResult($returnValue)
{
$this->setExpectedException(\RuntimeException::class);
$this->mockAttributeRepository->method('get')->willReturn($returnValue);
Expand All @@ -160,11 +184,11 @@ public function unexpectedReturnValueProvider()
/**
* @test
*/
public function itShouldNotAddKnownAttributes()
public function itShouldNotAddKnownAttributeOptions()
{
$mockAttribute = $this->getMock(AttributeInterface::class);
$mockAttribute->method('getAttributeId')->willReturn(111);

$this->mockAttributeRepository->method('get')->willReturn($mockAttribute);

$this->mockAttributeOptionManagementService->method('getItems')
Expand All @@ -173,19 +197,19 @@ public function itShouldNotAddKnownAttributes()
$this->createMockOptionLabel('Option 2', 200),
$this->createMockOptionLabel('Option 3', 300),
]);

$this->mockAttributeOptionManagementService->expects($this->never())->method('add');

$this->optionSetup->addAttributeOptionIfNotExists('entity_code', 'attribute_code', 'Option 2');
}

/**
* @test
*/
public function itShouldAddKnownAttributes()
public function itShouldAddUnknownAttributeOptions()
{
$this->setSpecifiedAttributeExistsFixture();

$this->mockAttributeOptionFactory->method('create')->willReturn(
$this->getMock(AttributeOptionInterface::class)
);
Expand All @@ -198,7 +222,7 @@ public function itShouldAddKnownAttributes()
]);

$this->mockAttributeOptionManagementService->expects($this->once())->method('add');

$this->optionSetup->addAttributeOptionIfNotExists('entity_code', 'attribute_code', 'Option 4');
}

Expand All @@ -210,7 +234,7 @@ public function itShouldAddStoreLabelInstances()
$this->setSpecifiedAttributeExistsFixture();

$this->expectNewOptionWithStoreLabelToBeCreated();

$this->mockAttributeOptionLabelFactory->method('create')->willReturn(
$this->getMock(AttributeOptionLabelInterface::class)
);
Expand All @@ -232,4 +256,56 @@ public function itShouldAddStoreLabelInstances()
[$testStoreId => 'Option 4 Store 1 Label']
);
}

/**
* @test
*/
public function itShouldTryToSetTheAdminScopeAsAWorkaroundForIssue1405()
{
$this->setSpecifiedAttributeExistsFixture();

$this->mockAttributeOptionFactory->method('create')->willReturn(
$this->getMock(AttributeOptionInterface::class)
);

$this->mockAttributeOptionManagementService->method('getItems')
->willReturn([
$this->createMockOptionLabel('Option 1', 100),
$this->createMockOptionLabel('Option 2', 200),
$this->createMockOptionLabel('Option 3', 300),
]);

$this->mockAppState->expects($this->once())->method('setAreaCode');

$this->optionSetup->addAttributeOptionIfNotExists('entity_code', 'attribute_code', 'Option 4');

}

/**
* @test
*/
public function itShouldTryToSetTheAdminScopeAsAWorkaroundForIssue1405EvenIfAnAreaIsAlreadySet()
{
$this->setSpecifiedAttributeExistsFixture();

$this->mockAttributeOptionFactory->method('create')->willReturn(
$this->getMock(AttributeOptionInterface::class)
);

$this->mockAttributeOptionManagementService->method('getItems')
->willReturn([
$this->createMockOptionLabel('Option 1', 100),
$this->createMockOptionLabel('Option 2', 200),
$this->createMockOptionLabel('Option 3', 300),
]);

$this->mockAppState->expects($this->once())->method('setAreaCode')->willThrowException(
new LocalizedException($this->getMock(Phrase::class, [], ['Test Exception']))
);

$this->mockAttributeOptionManagementService->expects($this->once())->method('add');

$this->optionSetup->addAttributeOptionIfNotExists('entity_code', 'attribute_code', 'Option 4');

}
}

0 comments on commit 7677c20

Please sign in to comment.