Skip to content

Commit

Permalink
Merge pull request #253 from yocmen/yocmen/modify-import-query
Browse files Browse the repository at this point in the history
Feature: Add the makeAllSearchableUsing API to the default import source
  • Loading branch information
matchish authored Jul 26, 2023
2 parents 0c1b798 + 8a179c9 commit 1f4ce15
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Searchable/DefaultImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function model()

private function newQuery(): Builder
{
$query = $this->model()->newQuery();
$query = $this->className::makeAllSearchableUsing($this->model()->newQuery());
$softDelete = $this->className::usesSoftDelete() && config('scout.soft_delete', false);
$query
->when($softDelete, function ($query) {
Expand Down
45 changes: 41 additions & 4 deletions tests/Feature/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Book;
use App\BookWithCustomKey;
use App\Post;
use App\Product;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -156,16 +157,20 @@ public function test_progress_report(): void
$output = explode("\n", $output->fetch());
$this->assertEquals(
trans('scout::import.start', ['searchable' => Product::class]),
trim($output[0]));
trim($output[0])
);
$this->assertEquals(
'[OK] '.trans('scout::import.done', ['searchable' => Product::class]),
trim($output[14]));
trim($output[14])
);
$this->assertEquals(
trans('scout::import.start', ['searchable' => Book::class]),
trim($output[16]));
trim($output[16])
);
$this->assertEquals(
'[OK] '.trans('scout::import.done', ['searchable' => Book::class]),
trim($output[30]));
trim($output[30])
);
}

public function test_progress_report_in_queue(): void
Expand Down Expand Up @@ -268,4 +273,36 @@ public function test_chained_queue_timeout_configuration_with_empty_string(): vo
return $job->timeout === null;
});
}

public function test_makeAllSearchableUsing_method_is_called_in_the_product_model(): void
{
$dispatcher = Post::getEventDispatcher();
Post::unsetEventDispatcher();

factory(Post::class)->states('draft')->create();
factory(Post::class)->states('draft')->create();
factory(Post::class)->states('draft')->create();
factory(Post::class)->states('published')->create();

Post::setEventDispatcher($dispatcher);

// Call the makeAllSearchableUsing method on the Product model
Artisan::call('scout:import', ['searchable' => [Post::class]]);

$params = [
'index' => (new Post())->searchableAs(),
'body' => [
'query' => [
'match_all' => new stdClass(),
],
],
];

$response = $this->elasticsearch->search($params);

// Assert that only the published posts are searchable
// bacause in the Post model we have defined the makeAllSearchableUsing method
// which returns only the published posts.
$this->assertEquals(1, $response['hits']['total']['value']);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Searchable/SearchableListFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function test_only_load_seachable_classes()

$searchable = $factory->make();

// There are 4 searchable models: Book, BookWithCustomKey, Product and Ticket
$this->assertCount(4, $searchable);
// There are 5 searchable models: Book, BookWithCustomKey, Product, Ticket and Post
$this->assertCount(5, $searchable);
}

public function test_find_searchable_trait_within_trait()
Expand Down
16 changes: 16 additions & 0 deletions tests/laravel/app/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Post extends Model
{
use Searchable;

protected function makeAllSearchableUsing($query)
{
return $query->where('status', 'published');
}
}
27 changes: 27 additions & 0 deletions tests/laravel/database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
return [
'title' => $faker->text,
'body' => $faker->text,
'status' => 'draft',
'date' => $faker->date(),
];
});

$factory->state(Post::class, 'draft', function () {
return [
'status' => 'draft',
];
});

$factory->state(Post::class, 'published', function () {
return [
'status' => 'published',
];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

final class CreatePostTable extends Migration
{
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->string('status');
$table->dateTime('date');
$table->softDeletes();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('posts');
}
}

0 comments on commit 1f4ce15

Please sign in to comment.