Skip to content

Commit 9747217

Browse files
committed
Laravel Scout
1 parent 66d7fe8 commit 9747217

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use InvalidArgumentException;
7+
use Laravel\Scout\EngineManager;
8+
use MongoDB\Laravel\Scout\AtlasSearchEngine;
9+
10+
use function config;
11+
use function sprintf;
12+
13+
class AtlasSearchScoutServiceProvider extends ServiceProvider
14+
{
15+
public function register()
16+
{
17+
$this->app->extend(EngineManager::class, function (EngineManager $engineManager) {
18+
$engineManager->extend('atlas_search', function ($app) {
19+
$connectionName = config('scout.atlas_search.connection');
20+
$connection = $app->make('db')->connection($connectionName);
21+
22+
if (! $connection instanceof Connection) {
23+
throw new InvalidArgumentException(sprintf('The MongoDB connection for Atlas Search must be a MongoDB connection. Got "%s". Set configuration "scout.atlas_search.connection"', $connection->getDriverName()));
24+
}
25+
26+
return new AtlasSearchEngine($connection);
27+
});
28+
29+
return $engineManager;
30+
});
31+
}
32+
}

tests/Scout/AtlasSearchScoutTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Scout;
4+
5+
use Laravel\Scout\Builder;
6+
use MongoDB\Laravel\Tests\TestCase;
7+
8+
class AtlasSearchScoutTest extends TestCase
9+
{
10+
public function setUp(): void
11+
{
12+
parent::setUp();
13+
14+
Post::insert([
15+
['title' => 'First Post', 'content' => 'First Post Content'],
16+
['title' => 'Second Post', 'content' => 'Second Post Content'],
17+
['title' => 'Third Post', 'content' => 'Third Post Content'],
18+
]);
19+
}
20+
21+
public function tearDown(): void
22+
{
23+
Post::truncate();
24+
25+
parent::tearDown();
26+
}
27+
28+
public function testGetScoutModelsByIds()
29+
{
30+
$post = Post::where('title', 'First Post')->first();
31+
32+
$builder = $this->createMock(Builder::class);
33+
34+
$posts = (new Post())->getScoutModelsByIds($builder, [
35+
(string) $post->id,
36+
]);
37+
38+
$this->assertCount(1, $posts);
39+
$this->assertSame($post->title, $posts->first()->title);
40+
}
41+
}

tests/Scout/Post.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Scout;
4+
5+
use Laravel\Scout\Searchable;
6+
use MongoDB\Laravel\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
use Searchable;
11+
}

tests/ServiceProviderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Foundation\Application;
7+
use Illuminate\Foundation\ProviderRepository;
8+
use MongoDB\Laravel\MongoDBServiceProvider;
9+
use PHPUnit\Framework\TestCase;
10+
11+
use function sys_get_temp_dir;
12+
use function tempnam;
13+
14+
class ServiceProviderTest extends TestCase
15+
{
16+
public function testBusServiceProvider()
17+
{
18+
$app = new Application();
19+
$providerRepository = new ProviderRepository($app, new Filesystem(), tempnam(sys_get_temp_dir(), 'manifest') . '.php');
20+
$providerRepository->load([MongoDBServiceProvider::class]);
21+
}
22+
}

0 commit comments

Comments
 (0)