Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Searchable Tests
  • Loading branch information
Arayik authored and Arayik committed Feb 22, 2023
commit d5f6029d232d2fbc523754dfadb0410928291402
163 changes: 163 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Default Search Engine
|--------------------------------------------------------------------------
|
| This option controls the default search connection that gets used while
| using Laravel Scout. This connection is used when syncing all models
| to the search service. You should adjust this based on your needs.
|
| Supported: "algolia", "meilisearch", "database", "collection", "null"
|
*/

'driver' => env('SCOUT_DRIVER', 'typesense'),

/*
|--------------------------------------------------------------------------
| Index Prefix
|--------------------------------------------------------------------------
|
| Here you may specify a prefix that will be applied to all search index
| names used by Scout. This prefix may be useful if you have multiple
| "tenants" or applications sharing the same search infrastructure.
|
*/

'prefix' => env('SCOUT_PREFIX', ''),

/*
|--------------------------------------------------------------------------
| Queue Data Syncing
|--------------------------------------------------------------------------
|
| This option allows you to control if the operations that sync your data
| with your search engines are queued. When this is set to "true" then
| all automatic data syncing will get queued for better performance.
|
*/

'queue' => env('SCOUT_QUEUE', false),

/*
|--------------------------------------------------------------------------
| Database Transactions
|--------------------------------------------------------------------------
|
| This configuration option determines if your data will only be synced
| with your search indexes after every open database transaction has
| been committed, thus preventing any discarded data from syncing.
|
*/

'after_commit' => false,

/*
|--------------------------------------------------------------------------
| Chunk Sizes
|--------------------------------------------------------------------------
|
| These options allow you to control the maximum chunk size when you are
| mass importing data into the search engine. This allows you to fine
| tune each of these chunk sizes based on the power of the servers.
|
*/

'chunk' => [
'searchable' => 500,
'unsearchable' => 500,
],

/*
|--------------------------------------------------------------------------
| Soft Deletes
|--------------------------------------------------------------------------
|
| This option allows to control whether to keep soft deleted records in
| the search indexes. Maintaining soft deleted records can be useful
| if your application still needs to search for the records later.
|
*/

'soft_delete' => false,

/*
|--------------------------------------------------------------------------
| Identify User
|--------------------------------------------------------------------------
|
| This option allows you to control whether to notify the search engine
| of the user performing the search. This is sometimes useful if the
| engine supports any analytics based on this application's users.
|
| Supported engines: "algolia"
|
*/

'identify' => env('SCOUT_IDENTIFY', false),

/*
|--------------------------------------------------------------------------
| Algolia Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Algolia settings. Algolia is a cloud hosted
| search engine which works great with Scout out of the box. Just plug
| in your application ID and admin API key to get started searching.
|
*/

'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
],

/*
|--------------------------------------------------------------------------
| MeiliSearch Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your MeiliSearch settings. MeiliSearch is an open
| source search engine with minimal configuration. Below, you can state
| the host and key information for your own MeiliSearch installation.
|
| See: https://docs.meilisearch.com/guides/advanced_guides/configuration.html
|
*/

'meilisearch' => [
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
'key' => env('MEILISEARCH_KEY', null),
'index-settings' => [
// 'users' => [
// 'filterableAttributes'=> ['id', 'name', 'email'],
// ],
],
],

'typesense' => [
'api_key' => 'xyz',
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'path' => '',
'protocol' => 'http',
],
],
'nearest_node' => [
'host' => 'localhost',
'port' => '8108',
'path' => '',
'protocol' => 'http',
],
'connection_timeout_seconds' => 2,
'healthcheck_interval_seconds' => 30,
'num_retries' => 3,
'retry_interval_seconds' => 1,
],
];
16 changes: 0 additions & 16 deletions tests/Feature/ExampleTest.php

This file was deleted.

50 changes: 50 additions & 0 deletions tests/Feature/SearchableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Typesense\LaravelTypesense\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Typesense\LaravelTypesense\Tests\Fixtures\SearchableUserModel;
use Typesense\LaravelTypesense\Tests\TestCase;

class SearchableTest extends TestCase
{
use RefreshDatabase;

protected function defineDatabaseMigrations()
{
$this->setUpFaker();
$this->loadLaravelMigrations();

SearchableUserModel::create([
'name' => 'Laravel Typsense',
'email' => '[email protected]',
'password' => bcrypt('password'),
]);
SearchableUserModel::create([
'name' => 'Laravel Typsense',
'email' => '[email protected]',
'password' => bcrypt('password'),
]);
}

public function testSearchByEmail()
{
$models = SearchableUserModel::search('[email protected]')->get();

$this->assertCount(1, $models);
}

public function testSearchByName()
{
$models = SearchableUserModel::search('Laravel Typsense')->get();

$this->assertCount(2, $models);
}

public function testSearchByWrongQueryParam()
{
$models = SearchableUserModel::search('[email protected]')->get();

$this->assertCount(0, $models);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use Laravel\Scout\Searchable;
use Typesense\LaravelTypesense\Interfaces\TypesenseDocument;

class UserModel extends Model implements TypesenseDocument
class SearchableUserModel extends Model implements TypesenseDocument
{
use Searchable;

protected $fillable = ['email', 'name', 'password'];

public $timestamps = false;

protected $table = 'users';

public function toSearchableArray(): array
Expand Down
48 changes: 9 additions & 39 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Typesense\LaravelTypesense\Tests;

use Laravel\Scout\EngineManager;
use Illuminate\Foundation\Testing\WithFaker;
use Orchestra\Testbench\TestCase as Orchestra;
use Typesense\LaravelTypesense\Tests\Fixtures\UserModel;
use Typesense\LaravelTypesense\TypesenseServiceProvider;

abstract class TestCase extends Orchestra
Expand All @@ -13,52 +13,22 @@ abstract class TestCase extends Orchestra

protected function getPackageProviders($app)
{
$app->singleton(EngineManager::class, function ($app) {
return new EngineManager($app);
});

return [TypesenseServiceProvider::class];
}

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'typesense');
$app->make('config')->set('scout.typesense',
[
'api_key' => 'xyz',
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'path' => '',
'protocol' => 'http',
],
],
'nearest_node' => [
'host' => 'localhost',
'port' => '8108',
'path' => '',
'protocol' => 'http',
],
'connection_timeout_seconds' => 2,
'healthcheck_interval_seconds' => 30,
'num_retries' => 3,
'retry_interval_seconds' => 1,
]
);
$this->mergeConfigFrom($app, __DIR__.'/../config/scout.php', 'scout');
}

protected function defineDatabaseMigrations()
private function mergeConfigFrom($app, $path, $key)
{
$this->setUpFaker();
$this->loadLaravelMigrations();

UserModel::create([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'asd'
]);
$config = $app['config']->get($key, []);

UserModel::create([
'name' => 'Abigail Otwell',
'email' => '[email protected]',
'password' => 'asd'
]);
$app['config']->set($key, array_merge(require $path, $config));
}
}