From 6ae4dd2aaad9825d31f0e418c611f00956ce3c0b Mon Sep 17 00:00:00 2001 From: Aydin Date: Wed, 8 Oct 2014 14:43:14 +0100 Subject: [PATCH 1/3] Provide option to return empty string if attribute option not found --- .../AttributeOptionValueConverter.php | 43 +++++++++++++------ .../AttributeOptionValueConverterTest.php | 18 ++++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/Jh/DataImportMagento/ValueConverter/AttributeOptionValueConverter.php b/src/Jh/DataImportMagento/ValueConverter/AttributeOptionValueConverter.php index 427fb27..c691360 100644 --- a/src/Jh/DataImportMagento/ValueConverter/AttributeOptionValueConverter.php +++ b/src/Jh/DataImportMagento/ValueConverter/AttributeOptionValueConverter.php @@ -4,6 +4,7 @@ use Ddeboer\DataImport\ValueConverter\ValueConverterInterface; use Ddeboer\DataImport\Exception\UnexpectedValueException; +use Jh\DataImportMagento\Options\OptionsParseTrait; /** * Load the real Option Label for a given ID @@ -14,23 +15,33 @@ */ class AttributeOptionValueConverter implements ValueConverterInterface { + use OptionsParseTrait; + + /** + * @var string|null + */ + protected $attributeCode = null; + /** * @var array */ - protected $options = array(); + protected $options = [ + 'returnEmptyStringIfOptionNotExist' => false, + ]; /** - * @var string|null + * @var array */ - protected $attributeCode = null; + protected $attributeOptions = []; /** * @param array $options */ - public function __construct($attributeCode, $options = array()) + public function __construct($attributeCode, $attributeOptions = [], $options = []) { $this->attributeCode = $attributeCode; - $this->options = $options; + $this->attributeOptions = $attributeOptions; + $this->options = $this->parseOptions($this->options, $options); } /** @@ -40,16 +51,20 @@ public function __construct($attributeCode, $options = array()) */ public function convert($input) { - if (!array_key_exists($input, $this->options)) { - throw new UnexpectedValueException( - sprintf( - '"%s" does not appear to be a valid attribute option for "%s"', - $input, - $this->attributeCode - ) - ); + if (!array_key_exists($input, $this->attributeOptions)) { + if (!$this->options['returnEmptyStringIfOptionNotExist']) { + throw new UnexpectedValueException( + sprintf( + '"%s" does not appear to be a valid attribute option for "%s"', + $input, + $this->attributeCode + ) + ); + } else { + return ''; + } } //look up the real option value - return $this->options[$input]; + return $this->attributeOptions[$input]; } } diff --git a/test/DataImportMagentoTest/ValueConverter/AttributeOptionValueConverterTest.php b/test/DataImportMagentoTest/ValueConverter/AttributeOptionValueConverterTest.php index d905a1c..f632347 100644 --- a/test/DataImportMagentoTest/ValueConverter/AttributeOptionValueConverterTest.php +++ b/test/DataImportMagentoTest/ValueConverter/AttributeOptionValueConverterTest.php @@ -43,4 +43,22 @@ public function testConverterThrowsExceptionIfKeyNotExists() $this->converter->convert(6); } + + public function testConverterReturnsEmptyStringIfOptionNotFound() + { + $attributeCode = 'colour'; + $options = array( + '1' => 'Red', + '2' => 'Purple', + '3' => 'Orange', + '4' => 'Green', + ); + + $this->converter = new AttributeOptionValueConverter($attributeCode, $options, [ + 'returnEmptyStringIfOptionNotExist' => true + ]); + + $this->assertEquals('', $this->converter->convert(6)); + + } } From 28e97aa8d5ce408a9648d6e10bf19df8698576eb Mon Sep 17 00:00:00 2001 From: Aydin Date: Wed, 8 Oct 2014 14:43:36 +0100 Subject: [PATCH 2/3] Remove debug code --- src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php b/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php index 1561526..9330eee 100644 --- a/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php +++ b/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php @@ -71,7 +71,6 @@ public function setOptions(array $options) } } $this->options = $this->parseOptions($this->options, $options); - //var_dump($this->options); } /** From 5363c27e951915284d4460965a4ddc4a4fa9cf1c Mon Sep 17 00:00:00 2001 From: Aydin Date: Wed, 8 Oct 2014 14:49:06 +0100 Subject: [PATCH 3/3] Fix which allows collections to exceed the size that Magento's collection getSize method returns. So if collection getSize returns 2 but actual size is 4, all 4 records will be processed --- .../Reader/MagentoReader.php | 2 +- .../Reader/MagentoReaderTest.php | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Jh/DataImportMagento/Reader/MagentoReader.php b/src/Jh/DataImportMagento/Reader/MagentoReader.php index 0982c32..bc8c10c 100644 --- a/src/Jh/DataImportMagento/Reader/MagentoReader.php +++ b/src/Jh/DataImportMagento/Reader/MagentoReader.php @@ -98,7 +98,7 @@ public function valid() * seems to fix it but is hard to patch. This simple check should return false * if the row if null */ - return $this->current <= $this->count() && $this->data; + return $this->current <= $this->count() || $this->data; } /** diff --git a/test/DataImportMagentoTest/Reader/MagentoReaderTest.php b/test/DataImportMagentoTest/Reader/MagentoReaderTest.php index ca80581..fef6283 100644 --- a/test/DataImportMagentoTest/Reader/MagentoReaderTest.php +++ b/test/DataImportMagentoTest/Reader/MagentoReaderTest.php @@ -154,4 +154,49 @@ public function testIterator() $i++; } } + + public function testReaderReturnsAllDataIfCollectionSizeIsWrong() + { + $this->collection + ->expects($this->once()) + ->method('getSelect') + ->will($this->returnValue($this->select)); + + $this->collection + ->expects($this->once()) + ->method('getSize') + ->will($this->returnValue(1)); + + $this->reader = new MagentoReader($this->collection); + $statement = $this->getMock('\Zend_Db_Statement_Interface'); + $this->select + ->expects($this->once()) + ->method('query') + ->will($this->returnValue($statement)); + + $data = array( + array('one' => 1, 'two' => 2, 'three' => 3), + array('one' => 11, 'two' => 22, 'three' => 33), + array('one' => 111, 'two' => 222, 'three' => 333), + ); + + $statement->expects($this->at(0)) + ->method('fetch') + ->will($this->returnValue($data[0])); + + $statement->expects($this->at(1)) + ->method('fetch') + ->will($this->returnValue($data[1])); + + $statement->expects($this->at(2)) + ->method('fetch') + ->will($this->returnValue($data[2])); + + $i = 1; + foreach ($this->reader as $key => $row) { + $this->assertEquals($i, $key); + $this->assertEquals($data[$i - 1], $row); + $i++; + } + } }