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']); }
public function index() { if (!$this->isIndexable) { return; } // NOTE: Transformer must ensure that _id is string, not MongoId $body = SearchArray::fromModel($this->model); if (array_key_exists('_id', $body)) { $config = Mangan::fromModel($this->model)->sanitizersMap; if (!array_key_exists(SearchArray::class, $config)) { throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class)); } else { throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model))); } } // In some cases $value *might* still be mongoId type, // see https://github.com/Maslosoft/Addendum/issues/43 $func = function ($value) { if ($value instanceof MongoId) { return (string) $value; } return $value; }; $filtered = filter_var($body, \FILTER_CALLBACK, ['options' => $func]); // Create proper elastic search request array $params = ['body' => $filtered]; try { $this->getClient()->index($this->getParams($params)); } catch (BadRequest400Exception $e) { // Throw previous exception, // as it holds more meaningfull information $previous = $e->getPrevious(); $message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage()); throw new BadRequest400Exception($message); } }