Skip to content

Commit

Permalink
ObjectIterator::current() to return null on empty or null iterators, …
Browse files Browse the repository at this point in the history
…instead of giving an error
  • Loading branch information
pmishev committed Oct 14, 2020
1 parent cf3d53f commit 7b3e50d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Result/ObjectIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function count()
*/
public function current()
{
return $this->convertToObject($this->objects[$this->key()]);
return isset($this->objects[$this->key()]) ? $this->convertToObject($this->objects[$this->key()]) : null;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Tests/App/fixture/Acme/BarBundle/Document/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sineflow\ElasticsearchBundle\Annotation as ES;
use Sineflow\ElasticsearchBundle\Document\AbstractDocument;
use Sineflow\ElasticsearchBundle\Document\MLProperty;
use Sineflow\ElasticsearchBundle\Result\ObjectIterator;

/**
* Product document for testing.
Expand Down Expand Up @@ -47,7 +48,7 @@ class Product extends AbstractDocument
public $category;

/**
* @var ObjCategory[]
* @var ObjCategory[]|ObjectIterator<ObjCategory>
* @ES\Property(type="object", name="related_categories", multiple=true, objectName="AcmeBarBundle:ObjCategory")
*/
public $relatedCategories;
Expand Down
50 changes: 40 additions & 10 deletions Tests/Functional/Result/DocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ protected function getDataArray()
'title' => 'Bar',
'tags' => [
['tagname' => 'first tag'],
['tagname' => 'second tag']
]
['tagname' => 'second tag'],
],
],
'related_categories' => [
[
'title' => 'Acme',
'tags' => [
['tagname' => 'tutu']
]
['tagname' => 'tutu'],
],
],
[
'title' => 'Doodle',
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function getDataArray()
],
[
'_id' => '54321',
]
],
],
],
];
Expand Down Expand Up @@ -127,7 +127,7 @@ public function testManualIteration()
$expected = [
'Foo Product',
'Bar Product',
'3rd Product'
'3rd Product',
];
while ($iterator->valid()) {
$this->assertEquals($i, $iterator->key());
Expand All @@ -152,6 +152,36 @@ public function testCurrentWithEmptyIterator()
$this->assertNull($iterator->current());
}

/**
* Make sure null is returned when field doesn't exist or is empty and ObjectIterator otherwise
*/
public function testNestedObjectIterator()
{
/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository();
/** @var DocumentIterator $iterator */
$products = $repo->find(['query' => ['match_all' => (object) []]], Finder::RESULTS_OBJECT);

$this->assertCount(4, $products);
/** @var Product $product */
foreach ($products as $product) {
$this->assertContains($product->id, ['1', '2', '3', '54321']);
switch ($product->id) {
case '54321':
$this->assertNull($product->relatedCategories);
break;
case '3':
$this->assertInstanceOf(ObjectIterator::class, $product->relatedCategories);
$this->assertNull($product->relatedCategories->current());
break;
default:
$this->assertInstanceOf(ObjectIterator::class, $product->relatedCategories);
$this->assertCount(2, $product->relatedCategories);
$this->assertInstanceOf(ObjCategory::class, $product->relatedCategories->current());
}
}
}

/**
* Test that aggregations are returned
*/
Expand Down Expand Up @@ -193,10 +223,10 @@ public function testSuggestions()
'text' => ['prodcut foot'],
'term' => [
'size' => 3,
'field' => 'title'
]
]
]
'field' => 'title',
],
],
],
], Finder::RESULTS_OBJECT);

$suggestions = $iterator->getSuggestions();
Expand Down

0 comments on commit 7b3e50d

Please sign in to comment.