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/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/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php b/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php index bc9bdc1..a8b1cd4 100644 --- a/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php +++ b/src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php @@ -72,7 +72,6 @@ public function setOptions(array $options) } } $this->options = $this->parseOptions($this->options, $options); - //var_dump($this->options); } /** 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++; + } + } } 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)); + + } }