Skip to content

Commit 3ac816d

Browse files
committed
0.1.0-rc.1 (#570)
1 parent 39b7332 commit 3ac816d

File tree

9 files changed

+53
-83
lines changed

9 files changed

+53
-83
lines changed

.github/workflows/mysql_tests.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.github/workflows/postgres_tests.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/DatabaseEngine.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\LazyCollection;
89
use Laravel\Scout\Builder;
910
use Laravel\Scout\Engines\Engine;
@@ -50,14 +51,22 @@ public function update($models)
5051
return;
5152
}
5253

53-
$indexes = collect($searchableData)->map(function ($data, $field) use ($model) {
54-
return [
55-
'key' => $model->getScoutKey(),
56-
'index' => $model->searchableAs(),
57-
'field' => $field,
58-
'content' => $data,
59-
];
60-
});
54+
$indexes = collect($searchableData)
55+
->filter(function ($data) {
56+
return ! is_null($data);
57+
})
58+
->map(function ($data, $field) use ($model) {
59+
if (is_iterable($data)) {
60+
$data = implode(' , ', Arr::flatten($data));
61+
}
62+
63+
return [
64+
'key' => $model->getScoutKey(),
65+
'index' => $model->searchableAs(),
66+
'field' => $field,
67+
'content' => $data,
68+
];
69+
});
6170

6271
SearchIndex::insert($indexes->values()->all());
6372

tests/Stubs/Post.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public function searchableAs()
3434
*/
3535
public function toSearchableArray()
3636
{
37-
$array = $this->toArray();
38-
39-
// Customize the data array...
40-
41-
return Arr::only($array, ['title', 'body']);
37+
return [
38+
'id' => $this->id,
39+
'title' => $this->title,
40+
'body' => $this->body,
41+
'array' => ['one', 'two', 'three' => ['four', 'five', 'six']],
42+
];
4243
}
4344
}

tests/TestCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Lunar\ScoutDatabaseEngine\Tests;
44

5-
use Lunar\ScoutDatabaseEngine\ScoutDatabaseServiceProvider;
65
use Laravel\Scout\ScoutServiceProvider;
6+
use Lunar\ScoutDatabaseEngine\ScoutDatabaseServiceProvider;
77

88
class TestCase extends \Orchestra\Testbench\TestCase
99
{
@@ -36,7 +36,6 @@ protected function defineEnvironment($app)
3636
* Get package providers.
3737
*
3838
* @param \Illuminate\Foundation\Application $app
39-
*
4039
* @return array<int, string>
4140
*/
4241
protected function getPackageProviders($app)

tests/Unit/IndexerTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Lunar\ScoutDatabaseEngine\Tests\Unit;
44

5-
use Lunar\ScoutDatabaseEngine\SearchIndex;
6-
use Lunar\ScoutDatabaseEngine\Tests\TestCase;
7-
use Lunar\ScoutDatabaseEngine\Tests\Stubs\Post;
85
use Illuminate\Foundation\Testing\RefreshDatabase;
96
use Illuminate\Support\Facades\Artisan;
7+
use Lunar\ScoutDatabaseEngine\SearchIndex;
8+
use Lunar\ScoutDatabaseEngine\Tests\Stubs\Post;
9+
use Lunar\ScoutDatabaseEngine\Tests\TestCase;
1010

1111
class IndexerTest extends TestCase
1212
{
@@ -22,7 +22,7 @@ public function can_index_a_post()
2222

2323
$this->assertInstanceOf(Post::class, $post);
2424

25-
$this->assertDatabaseCount('search_index', 2);
25+
$this->assertDatabaseCount('search_index', 4);
2626

2727
$this->assertDatabaseHas('search_index', [
2828
'key' => $post->getScoutKey(),
@@ -32,6 +32,26 @@ public function can_index_a_post()
3232
]);
3333
}
3434

35+
/** @test */
36+
public function can_index_a_post_with_nulls()
37+
{
38+
$post = new Post();
39+
$post->title = 'Example Post';
40+
$post->body = null;
41+
$post->save();
42+
43+
$this->assertInstanceOf(Post::class, $post);
44+
45+
$this->assertDatabaseCount('search_index', 3);
46+
47+
$this->assertDatabaseMissing('search_index', [
48+
'key' => $post->getScoutKey(),
49+
'index' => $post->searchableAs(),
50+
'field' => 'body',
51+
'content' => '',
52+
]);
53+
}
54+
3555
/** @test */
3656
public function deletes_outdated_index_data()
3757
{
@@ -101,7 +121,7 @@ public function can_flush_data()
101121
$post->save();
102122

103123
// 3 models x 2 fields = 6
104-
$this->assertDatabaseCount('search_index', 6);
124+
$this->assertDatabaseCount('search_index', 12);
105125

106126
Artisan::call('scout:flush "Lunar\\\ScoutDatabaseEngine\\\Tests\\\Stubs\\\Post"');
107127

tests/Unit/SearchIndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Lunar\ScoutDatabaseEngine\Tests\Unit;
44

5+
use Illuminate\Foundation\Testing\RefreshDatabase;
56
use Lunar\ScoutDatabaseEngine\SearchIndex;
67
use Lunar\ScoutDatabaseEngine\Tests\TestCase;
7-
use Illuminate\Foundation\Testing\RefreshDatabase;
88

99
class SearchIndexTest extends TestCase
1010
{

tests/Unit/SearchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Lunar\ScoutDatabaseEngine\Tests\Unit;
44

5+
use Illuminate\Foundation\Testing\DatabaseMigrations;
56
use Lunar\ScoutDatabaseEngine\SearchIndex;
6-
use Lunar\ScoutDatabaseEngine\Tests\TestCase;
77
use Lunar\ScoutDatabaseEngine\Tests\Stubs\Post;
8-
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use Lunar\ScoutDatabaseEngine\Tests\TestCase;
99

1010
class SearchTest extends TestCase
1111
{

tests/database/migrations/2022_08_31_000000_create_posts_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function up()
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->id();
1818
$table->string('title');
19-
$table->text('body');
19+
$table->text('body')->nullable();
2020
$table->timestamps();
2121
});
2222
}

0 commit comments

Comments
 (0)