public function testModels() { // Asserts for product model list($cKey, $cValue) = $this->config->classUidPair(new Product()); list($pKey, $pValue) = $this->config->primaryKeyPair(new Product()); $this->productRepoMock->shouldReceive('searchableIds')->andReturn([1, 2, 3])->byDefault(); $hitMock = m::mock('ZendSearch\\Lucene\\Search\\QueryHit'); $hitMock->{$cKey} = $cValue; $hitMock->{$pKey} = 1; $this->assertEquals([1], $this->config->models([$hitMock])->lists('id')->all()); $hitMock->{$pKey} = 5; $this->assertEquals([], $this->config->models([$hitMock])->lists('id')->all()); // Asserts for dummy model list($cKey, $cValue) = $this->config->classUidPair(new DummyModel()); list($pKey, $pValue) = $this->config->primaryKeyPair(new DummyModel()); $builderMock = m::mock(Builder::class); $this->dummyMock->shouldReceive('newQuery')->andReturn($builderMock); $builderMock->shouldReceive('lists')->with('pk')->andReturn([1, 2, 3])->byDefault(); $hitMock = m::mock('ZendSearch\\Lucene\\Search\\QueryHit'); $hitMock->{$cKey} = $cValue; $hitMock->{$pKey} = 3; $this->assertEquals([3], $this->config->models([$hitMock])->lists('pk')->all()); $hitMock->{$pKey} = 10; $this->assertEquals([], $this->config->models([$hitMock])->lists('pk')->all()); }
/** * Update document in index for model * * @param Model $model */ public function update(Model $model) { // Remove any existing documents for model. $this->delete($model); // Create new document for model. $doc = new Document(); list($name, $value) = $this->config->privateKeyPair($model); // Add private key. $doc->addField(Field::keyword($name, $value)); // Add model's class UID. list($name, $value) = $this->config->classUidPair($model); // Add class uid for identification of model's class. $doc->addField(Field::Keyword($name, $value)); $fields = $this->config->fields($model); // Add fields to document to be indexed (but not stored). foreach ($fields as $field) { $doc->addField(Field::unStored(trim($field), strip_tags(trim($model->{trim($field)})))); } $optionalAttributes = $this->config->optionalAttributes($model); // Add optional attributes to document to be indexed (but not stored). foreach ($optionalAttributes as $fieldName => $fieldValue) { $doc->addField(Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)))); } // Add document to index. $this->index()->addDocument($doc); }
public function setUp() { parent::setUp(); $this->model = new DummyModel(); $this->model->id = 1; $this->model->name = 'test name'; $this->connection = m::mock('Nqxcode\\LuceneSearch\\Index\\Connection'); $this->connection->shouldReceive('getIndexPath'); $this->config = m::mock('Nqxcode\\LuceneSearch\\Model\\Config'); $this->config->shouldReceive('primaryKeyPair')->with($this->model)->andReturn(['primary_key', 1]); $this->config->shouldReceive('classUidPair')->with($this->model)->andReturn(['class_uid', '12345']); $this->config->shouldReceive('fields')->with($this->model)->andReturn(['name' => ['boost' => 1]]); $this->config->shouldReceive('optionalAttributes')->andReturn(['optional_attribute1' => ['boost' => 1, 'value' => 'optional value']]); $this->config->shouldReceive('boost')->andReturn(1); }
/** * Update document in index for model * * @param Model $model */ public function update(Model $model) { // Remove any existing documents for model. $this->delete($model); // Create new document for model. $doc = new Document(); list($name, $value) = $this->config->primaryKeyPair($model); // Add private key. $doc->addField(Field::keyword($name, $value)); // Add model's class UID. list($name, $value) = $this->config->classUidPair($model); // Add class uid for identification of model's class. $doc->addField(Field::Keyword($name, $value)); // Get base fields. $fields = $this->config->fields($model); // Add fields to document to be indexed (but not stored). foreach ($fields as $fieldName => $options) { $fieldValue = $model->{trim($fieldName)}; $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))); $field->boost = array_get($options, 'boost'); $doc->addField($field); } // Get dynamic fields. $optionalAttributes = $this->config->optionalAttributes($model); // Add optional attributes to document to be indexed (but not stored). foreach ($optionalAttributes as $fieldName => $options) { $fieldValue = array_get($options, "value"); $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))); $field->boost = array_get($options, "boost"); $doc->addField($field); } // Set boost for model. $doc->boost = $this->config->boost($model); // Add document to index. $this->index()->addDocument($doc); }
public function testPrivateKeyForIncorrectModel() { $message = "Configuration doesn't exist for model of class '" . get_class($this->unknownRepoMock) . "'."; $this->setExpectedException('\\InvalidArgumentException', $message); $this->config->privateKeyPair($this->unknownRepoMock); }