Skip to content

Commit

Permalink
adding filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Sep 30, 2020
1 parent a8a4c30 commit 3555f54
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 15 deletions.
16 changes: 7 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
"laravel"
],
"license": "MIT",
"authors": [
{
"name": "TNT Studio",
"email": "[email protected]"
}
],
"authors": [{
"name": "TNT Studio",
"email": "[email protected]"
}],
"require": {
"php": ">=7.1",
"teamtnt/tntsearch": "2.*",
Expand All @@ -26,8 +24,8 @@
"illuminate/support": "~5.4|^6.0|^7.0|^8.0"
},
"require-dev": {
"mockery/mockery": "~0.9|^1.3.1",
"phpunit/phpunit": "~5.0|^9.3"
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.0|^9.3"
},
"autoload": {
"psr-4": {
Expand All @@ -54,4 +52,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
Expand Down
52 changes: 52 additions & 0 deletions src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class TNTSearchEngine extends Engine
{

private $filters;
/**
* @var TNTSearch
*/
Expand Down Expand Up @@ -161,6 +163,9 @@ protected function performSearch(Builder $builder, array $options = [])
$options
);
}

$builder->query = $this->applyFilters('query_expansion', $builder->query, get_class($builder->model));

if (isset($this->tnt->config['searchBoolean']) ? $this->tnt->config['searchBoolean'] : false) {
$res = $this->tnt->searchBoolean($builder->query, $limit);
event(new SearchPerformed($builder, $res, true));
Expand Down Expand Up @@ -408,4 +413,51 @@ public function flush($model)
unlink($pathToIndex);
}
}

/**
* Adds a filter
*
* @param string
* @param callback
* @return void
*/
public function addFilter($name, $callback)
{
if (!is_callable($callback, true)) {
throw new InvalidArgumentException(sprintf('Filter is an invalid callback: %s.', print_r($callback, true)));
}
$this->filters[$name][] = $callback;
}

/**
* Returns an array of filters
*
* @param string
* @return array
*/
public function getFilters($name)
{
return isset($this->filters[$name]) ? $this->filters[$name] : [];
}

/**
* Returns a string on which a filter is applied
*
* @param string
* @param string
* @return string
*/
public function applyFilters($name, $result, $model)
{
foreach ($this->getFilters($name) as $callback) {
// prevent fatal errors, do your own warning or
// exception here as you need it.
if (!is_callable($callback)) {
continue;
}

$result = call_user_func($callback, $result, $model);
}
return $result;
}
}
36 changes: 31 additions & 5 deletions tests/TNTSearchEngineTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?php

use Illuminate\Database\Eloquent\Collection;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use TeamTNT\Scout\Engines\TNTSearchEngine;
use TeamTNT\TNTSearch\TNTSearch;

class TNTSearchEngineTest extends PHPUnit_Framework_TestCase
class TNTSearchEngineTest extends TestCase
{
public function tearDown()

protected function tearDown(): void
{
Mockery::close();
m::close();
}

public function test_update_adds_objects_to_index()
{
$client = Mockery::mock('TeamTNT\TNTSearch\TNTSearch');
$client = m::mock('TeamTNT\TNTSearch\TNTSearch');
$client->shouldReceive('createIndex')
->with('table.index')
->andReturn($index = Mockery::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer'));
->andReturn($index = m::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer'));
$index->shouldReceive('setDatabaseHandle');
$index->shouldReceive('setPrimaryKey');
$index->shouldReceive('query');
Expand All @@ -32,6 +36,28 @@ public function test_update_adds_objects_to_index()
$engine = new TNTSearchEngine($client);
$engine->update(Collection::make([new TNTSearchEngineTestModel()]));
}

public function testApplyingFilters()
{
$tnt = new TNTSearch;
$engine = new TeamTNT\Scout\Engines\TNTSearchEngine($tnt);

$engine->addFilter("query_expansion", function ($query, $model) {
if ($query == "test" && $model == "TeamTNT\TNTSearch\TNTSearch") {
return "modified-".$query;
}
return $query;

});

$query = $engine->applyFilters('query_expansion', "test", TNTSearch::class);
$query2 = $engine->applyFilters('query_expansion', "test", Collection::class);
$query3 = $engine->applyFilters('query_expansion', "test2", TNTSearch::class);

$this->assertTrue($query == "modified-test");
$this->assertTrue($query2 == "test");
$this->assertTrue($query3 == "test2");
}
}

class TNTSearchEngineTestModel
Expand Down

0 comments on commit 3555f54

Please sign in to comment.