Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-snowshoe committed Sep 9, 2023
1 parent 916644e commit 506bdae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
31 changes: 14 additions & 17 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function __construct($id = null)
$this->id = $id ?? uniqid(true);
}

protected function addFieldsToProperties($properties): array
{
/** @var FieldInterface $field */
foreach (get_object_vars($this) as $field) {
if ($field instanceof FieldInterface && !is_null($field->getValue())) {
$properties[] = $field->getName();
$properties[] = $field->getValue();
}
}
return $properties;
}

public function getHashDefinition(array $prefixes = null): array
{
$id = $this->getId();
Expand All @@ -45,15 +57,7 @@ public function getHashDefinition(array $prefixes = null): array
$properties[] = 'REPLACE';
}

/** @var FieldInterface $field */
foreach (get_object_vars($this) as $field) {
if ($field instanceof FieldInterface && !is_null($field->getValue())) {
$properties[] = $field->getName();
$properties[] = $field->getValue();
}
}

return $properties;
return $this->addFieldsToProperties($properties);
}

public function getDefinition(): array
Expand Down Expand Up @@ -91,14 +95,7 @@ public function getDefinition(): array

$properties[] = 'FIELDS';

/** @var FieldInterface $field */
foreach (get_object_vars($this) as $field) {
if ($field instanceof FieldInterface && !is_null($field->getValue())) {
$properties[] = $field->getName();
$properties[] = $field->getValue();
}
}
return $properties;
return $this->addFieldsToProperties($properties);
}

public function getId(): string
Expand Down
10 changes: 4 additions & 6 deletions tests/RediSearch/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ public function testSetStopWordsOnCreateIndex()
$this->assertEquals(1, $result->getCount());
}

public function testWithPrefix()
public function testShouldNotSearchEveryIndexWhenAPrefixIsSpecified()
{
$expectedFirstResult = 'Jack';
$firstPrefix = 'Foo';
Expand All @@ -757,16 +757,14 @@ public function testWithPrefix()
$this->assertEquals($expectedFirstResult, $firstResult->getDocuments()[0]->name);
}

public function testWithoutPrefix()
public function testShouldSearchEveryIndexWhenAPrefixIsNotSpecified()
{
$expectedDocuments = 1;
$expectedName = 'Jack';
$firstIndex = (new Index($this->redisClient, 'first'))
->addTextField('name');
$firstIndex = (new Index($this->redisClient, 'first'))->addTextField('name');
$firstIndex->create();
$firstIndex->add([new TextField('name', $expectedName)]);
$secondIndex = (new Index($this->redisClient, 'second'))
->addTextField('name');
$secondIndex = (new Index($this->redisClient, 'second'))->addTextField('name');
$secondIndex->create();

$firstResult = $firstIndex->search($expectedName);
Expand Down

0 comments on commit 506bdae

Please sign in to comment.