public function testReload() { /** @var Product[]|Collection $c */ $c = Collection::make([$this->existed[0], $this->existed[1], $this->notFilled]); $c->reload(); $this->assertCount(3, $c); foreach ($c as $m) { $this->assertEquals($m->getOriginal(), $m->getAttributes()); } $c = Collection::make([$this->existed[0], $this->existed[1], $this->notExisted]); $c->reload(); $this->assertCount(2, $c); foreach ($c as $m) { $this->assertEquals($m->getOriginal(), $m->getAttributes()); } }
/** * Get models by query hits. * * @param QueryHit[] $hits * @return Collection */ public function models($hits) { $models = []; $groupedIdsAsKeys = $this->groupedSearchableIdsAsKeys($hits); foreach ($hits as $hit) { $model = $this->model($hit); $id = $model->{$model->getKeyName()}; $searchableIds = array_get($groupedIdsAsKeys, get_class($model), []); if (isset($searchableIds[$id])) { $models[] = $model; } } return Collection::make($models); }
public function testPaginate() { $models = m::mock(LazyCollection::make([])); $models->shouldReceive('slice')->with(0, 2)->andReturn($sliced = m::mock(LazyCollection::make([]))); $sliced->shouldReceive('reload')->andReturn(Collection::make([1, 2])); $query = $this->constructor->query('test'); $this->runner->shouldReceive('models')->with($this->query)->andReturn($models); $expected = new Paginator([1, 2], 3, 2); $actual = $query->paginate(2); $this->assertEquals($expected, $actual); $models->shouldReceive('slice')->with(2, 2)->andReturn($sliced); $this->runner->shouldReceive('models')->with($this->query)->andReturn($models); $actual = $query->paginate(2, 2); $this->assertEquals($expected, $actual); }