Skip to content

Commit

Permalink
Merge pull request #31 from WeareJH/fix-attribute-cache
Browse files Browse the repository at this point in the history
Fix attribute cache
  • Loading branch information
AydinHassan committed Sep 4, 2015
2 parents 011d54a + df9a9ed commit 0c892da
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Jh/DataImportMagento/Service/AttributeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(
*/
public function getAttrCodeCreateIfNotExist($entityType, $attrCode, $attrValue)
{
$attrValue = strtolower($attrValue);
if (isset($this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue])) {
return $this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue];
}
Expand All @@ -70,10 +71,14 @@ public function getAttrCodeCreateIfNotExist($entityType, $attrCode, $attrValue)
$options = $attributeOptionsModel->getAllOptions(false);

foreach ($options as $option) {
if (strtolower($option['label']) == strtolower($attrValue)) {
$this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue] = $option['value'];
return $option['value'];
}
$optionId = strtolower($option['value']);
$optionValue = strtolower($option['label']);

$this->cachedAttributeOptionsValues[$entityType][$attrCode][$optionValue] = $optionId;
}

if (isset($this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue])) {
return $this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue];
}

//not found - create it
Expand All @@ -86,10 +91,9 @@ public function getAttrCodeCreateIfNotExist($entityType, $attrCode, $attrValue)

$attributeOptionsModel = clone $this->eavAttrSrcModel;
$attributeOptionsModel->setAttribute($attribute);
$id = $attributeOptionsModel->getOptionId(strtolower($attrValue));
$id = $attributeOptionsModel->getOptionId($attrValue);

$this->cachedAttributeOptionsValues[$entityType][$attrCode][$attrValue] = $id;

return $id;
}
}
168 changes: 168 additions & 0 deletions test/DataImportMagentoTest/Service/AttributeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,72 @@ public function testGetAttributeCreatesAttributeOptionIfItDoesNotExist()
$this->assertEquals($ret, 'code3');
}

public function testGetAttributeCreatesAttributeOptionIfItDoesNotExistAndCachesIt()
{
$attribute = $this->getMock('\Mage_Eav_Model_Entity_Attribute_Abstract');

$options = array(
array('label' => 'option1', 'value' => 'code1'),
array('label' => 'option2', 'value' => 'code2'),
);

$this->attrModel
->expects($this->once())
->method('getIdByCode')
->with('catalog_product', 'code3')
->will($this->returnValue(1));

$this->attrModel
->expects($this->once())
->method('load')
->with(1)
->will($this->returnValue($attribute));

$this->attrSrcModel
->expects($this->exactly(2))
->method('setAttribute')
->with($attribute);

$this->attrSrcModel
->expects($this->once())
->method('getAllOptions')
->with(false)
->will($this->returnValue($options));

$this->attrSrcModel
->expects($this->once())
->method('getOptionId')
->with('option3')
->will($this->returnValue('code3'));

$data = array(
'value' => array(
'option' => array('option3', 'option3')
)
);

$attribute
->expects($this->once())
->method('usesSource')
->will($this->returnValue(true));

$attribute
->expects($this->once())
->method('setData')
->with('option', $data);

$attribute
->expects($this->once())
->method('save');

$ret = $this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'code3', 'option3');
$this->assertEquals($ret, 'code3');

//pls load from cache
$ret = $this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'code3', 'option3');
$this->assertEquals($ret, 'code3');
}

public function testGetAttributeReturnsIdIfItExists()
{
$attribute = $this->getMock('\Mage_Eav_Model_Entity_Attribute_Abstract');
Expand Down Expand Up @@ -185,4 +251,106 @@ public function testGetAttributeThrowsExceptionIfAttributeDoesNotExist()

$this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'not_here', 'some_value');
}

public function testAllAttributeOptionsAreCached()
{
$attribute = $this->getMock('\Mage_Eav_Model_Entity_Attribute_Abstract');

$options = array(
array('label' => 'option1', 'value' => 'code1'),
array('label' => 'option2', 'value' => 'code2'),
);

$this->attrModel
->expects($this->once())
->method('getIdByCode')
->with('catalog_product', 'code2')
->will($this->returnValue(1));

$this->attrModel
->expects($this->once())
->method('load')
->with(1)
->will($this->returnValue($attribute));

$this->attrSrcModel
->expects($this->once())
->method('setAttribute')
->with($attribute);

$this->attrSrcModel
->expects($this->once())
->method('getAllOptions')
->with(false)
->will($this->returnValue($options));

$attribute
->expects($this->once())
->method('usesSource')
->will($this->returnValue(true));

$attribute
->expects($this->never())
->method('setData');

$attribute
->expects($this->never())
->method('save');

$ret = $this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'code2', 'option2');
$this->assertEquals($ret, 'code2');

//retrieve via cache hopefully
$ret = $this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'code2', 'option2');
$this->assertEquals($ret, 'code2');
}

public function testGetAttributeValueIdIsNotCaseSensitive()
{
$attribute = $this->getMock('\Mage_Eav_Model_Entity_Attribute_Abstract');

$options = array(
array('label' => 'option1', 'value' => 'code1'),
array('label' => 'option2', 'value' => 'code2'),
);

$this->attrModel
->expects($this->once())
->method('getIdByCode')
->with('catalog_product', 'code2')
->will($this->returnValue(1));

$this->attrModel
->expects($this->once())
->method('load')
->with(1)
->will($this->returnValue($attribute));

$this->attrSrcModel
->expects($this->once())
->method('setAttribute')
->with($attribute);

$this->attrSrcModel
->expects($this->once())
->method('getAllOptions')
->with(false)
->will($this->returnValue($options));

$attribute
->expects($this->once())
->method('usesSource')
->will($this->returnValue(true));

$attribute
->expects($this->never())
->method('setData');

$attribute
->expects($this->never())
->method('save');

$ret = $this->attributeService->getAttrCodeCreateIfNotExist('catalog_product', 'code2', 'OPTION2');
$this->assertEquals($ret, 'code2');
}
}

0 comments on commit 0c892da

Please sign in to comment.