Skip to content

Commit

Permalink
Merge pull request #14 from WeareJH/hotfix/collection-count
Browse files Browse the repository at this point in the history
Hotfix/collection count
  • Loading branch information
AydinHassan committed Oct 8, 2014
2 parents 3a7ef18 + 5363c27 commit 5868f97
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Jh/DataImportMagento/Reader/MagentoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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];
}
}
1 change: 0 additions & 1 deletion src/Jh/DataImportMagento/Writer/InventoryUpdateWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function setOptions(array $options)
}
}
$this->options = $this->parseOptions($this->options, $options);
//var_dump($this->options);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions test/DataImportMagentoTest/Reader/MagentoReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}
}

0 comments on commit 5868f97

Please sign in to comment.