From 7b3e50d3162ccc2f6dc2837c68affbd7ef93b1f4 Mon Sep 17 00:00:00 2001 From: pmishev Date: Wed, 14 Oct 2020 13:43:22 +0100 Subject: [PATCH] ObjectIterator::current() to return null on empty or null iterators, instead of giving an error --- Result/ObjectIterator.php | 2 +- .../Acme/BarBundle/Document/Product.php | 3 +- .../Result/DocumentIteratorTest.php | 50 +++++++++++++++---- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Result/ObjectIterator.php b/Result/ObjectIterator.php index 85f5a6d..c5f4892 100644 --- a/Result/ObjectIterator.php +++ b/Result/ObjectIterator.php @@ -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; } /** diff --git a/Tests/App/fixture/Acme/BarBundle/Document/Product.php b/Tests/App/fixture/Acme/BarBundle/Document/Product.php index 54c1c04..14c84a8 100644 --- a/Tests/App/fixture/Acme/BarBundle/Document/Product.php +++ b/Tests/App/fixture/Acme/BarBundle/Document/Product.php @@ -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. @@ -47,7 +48,7 @@ class Product extends AbstractDocument public $category; /** - * @var ObjCategory[] + * @var ObjCategory[]|ObjectIterator * @ES\Property(type="object", name="related_categories", multiple=true, objectName="AcmeBarBundle:ObjCategory") */ public $relatedCategories; diff --git a/Tests/Functional/Result/DocumentIteratorTest.php b/Tests/Functional/Result/DocumentIteratorTest.php index e3c24bb..81bd002 100644 --- a/Tests/Functional/Result/DocumentIteratorTest.php +++ b/Tests/Functional/Result/DocumentIteratorTest.php @@ -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', @@ -67,7 +67,7 @@ protected function getDataArray() ], [ '_id' => '54321', - ] + ], ], ], ]; @@ -127,7 +127,7 @@ public function testManualIteration() $expected = [ 'Foo Product', 'Bar Product', - '3rd Product' + '3rd Product', ]; while ($iterator->valid()) { $this->assertEquals($i, $iterator->key()); @@ -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 */ @@ -193,10 +223,10 @@ public function testSuggestions() 'text' => ['prodcut foot'], 'term' => [ 'size' => 3, - 'field' => 'title' - ] - ] - ] + 'field' => 'title', + ], + ], + ], ], Finder::RESULTS_OBJECT); $suggestions = $iterator->getSuggestions();