/** * Re-indexes the related indices for the given ActiveQueryInterface. * The result of the given ActiveQuery must consist from Searchable models of the same class. * * @param ActiveQueryInterface $activeQuery * * @return array */ public function reindexByActiveQuery(ActiveQueryInterface $activeQuery) { $indices = null; $records = $this->activeQueryChunker->chunk($activeQuery, self::CHUNK_SIZE, function ($activeRecordEntities) use(&$indices) { $records = $this->getAlgoliaRecordsFromSearchableModelArray($activeRecordEntities); // The converting ActiveRecords to Algolia ones already does the type checking // so it's safe to init indices here during the first chunk. if ($indices === null) { $indices = $this->initIndices($activeRecordEntities[0]); } return $records; }); $response = []; foreach ($indices as $index) { $response[$index->indexName] = $this->reindexAtomically($index, $records); } return $response; }
/** @test */ public function only_arrays_returned_from_callable_are_merged_into_the_results() { $dummyModel1 = m::mock(DummyModel::class)->makePartial(); $dummyModel1->id = 1; $dummyModel2 = m::mock(DummyModel::class)->makePartial(); $dummyModel2->id = 2; $activeQuery = m::mock(ActiveQueryInterface::class); $activeQueryForFirstPage = m::mock(ActiveQueryInterface::class); $activeQueryForSecondPage = m::mock(ActiveQueryInterface::class); $activeQueryForThirdPage = m::mock(ActiveQueryInterface::class); // Simulate the pagination for 2 results when there is one result per page. $activeQuery->shouldReceive('offset')->with(0)->once()->andReturn($activeQueryForFirstPage); $activeQuery->shouldReceive('offset')->with(1)->once()->andReturn($activeQueryForSecondPage); $activeQuery->shouldReceive('offset')->with(2)->once()->andReturn($activeQueryForThirdPage); $activeQueryForFirstPage->shouldReceive('limit')->with(1)->once()->andReturn($activeQueryForFirstPage); $activeQueryForSecondPage->shouldReceive('limit')->with(1)->once()->andReturn($activeQueryForSecondPage); $activeQueryForThirdPage->shouldReceive('limit')->with(1)->once()->andReturn($activeQueryForThirdPage); $activeQueryForFirstPage->shouldReceive('all')->andReturn([$dummyModel1]); $activeQueryForSecondPage->shouldReceive('all')->andReturn([$dummyModel2]); $activeQueryForThirdPage->shouldReceive('all')->andReturn([]); $activeQueryChunker = new ActiveQueryChunker(); $results = $activeQueryChunker->chunk($activeQuery, 1, function ($dummyModelChunk) { $this->assertCount(1, $dummyModelChunk); foreach ($dummyModelChunk as $dummyModel) { if ($dummyModel->id === 1) { return [$dummyModel]; } } }); $this->assertCount(1, $results); $this->assertEquals(1, $results[0]->id); }