/** * Test for scan iterator. */ public function testIterator() { $rawData = ['hits' => ['total' => 2, 'hits' => [['_id' => 'foo']]]]; $repository = $this->getMockBuilder('ONGR\\ElasticsearchBundle\\ORM\\Repository')->disableOriginalConstructor()->getMock(); $repository->expects($this->once())->method('scan')->with('test_id', '5m', Repository::RESULTS_RAW)->willReturn(['_scroll_id' => 'updated_id', 'hits' => ['total' => 2, 'hits' => [['_id' => 'bar']]]]); $iterator = new RawResultScanIterator($rawData); $iterator->setRepository($repository)->setScrollId('test_id')->setScrollDuration('5m'); $this->assertCount(2, $iterator); $iterator->rewind(); $data = []; $expectedData = [['_id' => 'foo'], ['_id' => 'bar']]; foreach ($iterator as $key => $document) { $data[$key] = $document; } $this->assertEquals($expectedData, $data); }
/** * Parses raw result. * * @param array $raw * @param string $resultsType * @param string $scrollDuration * * @return DocumentIterator|DocumentScanIterator|RawResultIterator|array * * @throws \Exception */ private function parseResult($raw, $resultsType, $scrollDuration) { switch ($resultsType) { case self::RESULTS_OBJECT: if (isset($raw['_scroll_id'])) { $iterator = new DocumentScanIterator($raw, $this->getManager()->getTypesMapping(), $this->getManager()->getBundlesMapping()); $iterator->setRepository($this)->setScrollDuration($scrollDuration)->setScrollId($raw['_scroll_id']); return $iterator; } return new DocumentIterator($raw, $this->getManager()->getTypesMapping(), $this->getManager()->getBundlesMapping()); case self::RESULTS_ARRAY: return $this->convertToNormalizedArray($raw); case self::RESULTS_RAW: return $raw; case self::RESULTS_RAW_ITERATOR: if (isset($raw['_scroll_id'])) { $iterator = new RawResultScanIterator($raw); $iterator->setRepository($this)->setScrollDuration($scrollDuration)->setScrollId($raw['_scroll_id']); return $iterator; } return new RawResultIterator($raw); default: throw new \Exception('Wrong results type selected'); } }