Skip to content

Commit

Permalink
Merge pull request #324 from utopia-php/empty-search
Browse files Browse the repository at this point in the history
Empty fulltext search
  • Loading branch information
abnegate authored Sep 28, 2023
2 parents f2626ac + f01fcb3 commit 58d33c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ protected function getFulltextValue(string $value): string
$value = preg_replace('/\s+/', ' ', $value); // Remove multiple whitespaces
$value = trim($value);

if (empty($value)) {
return '';
}

if ($exact) {
$value = '"' . $value . '"';
} else {
Expand Down
29 changes: 25 additions & 4 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1290,10 +1290,7 @@ public function testFulltextIndexWithInteger(): void
static::getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']);
}

/**
* @depends testCreateDocument
*/
public function testListDocumentSearch(Document $document): void
public function testListDocumentSearch(): void
{
$fulltextSupport = $this->getDatabase()->getAdapter()->getSupportForFulltextIndex();
if (!$fulltextSupport) {
Expand Down Expand Up @@ -1328,6 +1325,30 @@ public function testListDocumentSearch(Document $document): void
$this->assertEquals(1, count($documents));
}

public function testEmptySearch(): void
{
$fulltextSupport = $this->getDatabase()->getAdapter()->getSupportForFulltextIndex();
if (!$fulltextSupport) {
$this->expectNotToPerformAssertions();
return;
}

$documents = static::getDatabase()->find('documents', [
Query::search('string', ''),
]);
$this->assertEquals(0, count($documents));

$documents = static::getDatabase()->find('documents', [
Query::search('string', '*'),
]);
$this->assertEquals(0, count($documents));

$documents = static::getDatabase()->find('documents', [
Query::search('string', '<>'),
]);
$this->assertEquals(0, count($documents));
}

/**
* @depends testGetDocument
*/
Expand Down

0 comments on commit 58d33c8

Please sign in to comment.