public function testIfWillProperlyRemapSanitizer() { $model = new ModelWithSimpleTree(); $model->parentId = new MongoId(); $meta = ManganelMeta::create($model)->field('parentId'); $this->assertTrue(!empty($meta->sanitizer), 'That has sanitizer explicitly set'); $data = SearchArray::fromModel($model); $this->assertInternalType('string', $data['parentId']); $fromArray = SearchArray::toModel($data); $this->assertInternalType('string', $fromArray->parentId); }
public function testIfCanStoreSimpleDocument() { $model = new SimpleModel(); $model->_id = new MongoId(); $model->title = 'Connecticut is a state'; $mnl = Manganel::fly(); $client = $mnl->getClient(); $params = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id, 'body' => SearchArray::fromModel($model)]; $client->index($params); $get = $params; unset($get['body']); $found = $client->get($get)['_source']; $this->assertSame($model->title, $found['title']); }
public function testIfWillProperlyHandleMongoDate() { $model = new ModelWithDate(); $model->createdAt = new MongoDate(); $arr = SearchArray::fromModel($model); codecept_debug($arr['createdAt']); $this->assertInternalType('int', $arr['createdAt']); $fromArray = SearchArray::toModel($arr); /* @var $fromArray ModelWithDate */ codecept_debug(date('c', $model->createdAt->sec)); $this->assertInstanceOf(MongoDate::class, $model->createdAt); $this->assertInstanceOf(MongoDate::class, $fromArray->createdAt); $this->assertSame((int) $model->createdAt->sec, (int) $fromArray->createdAt->sec); }
public function testIfCanStoreNestedDocument() { $model = new NestedModel(); $model->_id = new MongoId(); $model->username = '******'; $image = new Image(); $image->_id = new MongoId(); $image->filename = 'my-photo.jpg'; $model->avatar = $image; $mnl = Manganel::fly(); $client = $mnl->getClient(); $params = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id, 'body' => SearchArray::fromModel($model)]; codecept_debug($params); $client->index($params); $get = $params; unset($get['body']); $found = $client->get($get)['_source']; codecept_debug($found); $this->assertSame($model->username, $found['username']); }
protected function fetchData() { $criteria = $this->configureFetch(); /** * TODO Refactor this into SearchFinder class */ $qb = new QueryBuilder(); if ($criteria instanceof SearchCriteria) { $models = $criteria->getModels(); if (!empty($models)) { $qb->add($models); } } $model = $this->getModel(); if (!Event::handled($model, FinderInterface::EventBeforeFind)) { return []; } $modelCriteria = $model->getDbCriteria(); $criteria->mergeWith($modelCriteria); if (!empty($model)) { $qb->add($model); } $qb->setCriteria($criteria); $rawResults = $qb->search($criteria->getSearch()); $results = []; foreach ($rawResults as $data) { $model = SearchArray::toModel($data['_source']); if ($model instanceof IndexAwareInterface) { $model->setIndex($data['_index']); } if ($model instanceof ScoreAwareInterface) { $model->setScore($data['_score']); } $results[] = $model; } return $results; }
public function get($id = null) { if (!$this->isIndexable) { return; } $params = $id ? ['id' => (string) $id] : []; $data = $this->getClient()->get($this->getParams($params))['_source']; return SearchArray::toModel($data); }