22
33namespace MongoDB \Laravel \Tests \Scout ;
44
5+ use ArrayIterator ;
56use Closure ;
67use DateTimeImmutable ;
78use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
89use Illuminate \Support \Collection as LaravelCollection ;
910use Illuminate \Support \LazyCollection ;
1011use Laravel \Scout \Builder ;
1112use Laravel \Scout \Jobs \RemoveFromSearch ;
13+ use LogicException ;
1214use Mockery as m ;
15+ use MongoDB \BSON \Document ;
1316use MongoDB \BSON \UTCDateTime ;
1417use MongoDB \Collection ;
1518use MongoDB \Database ;
@@ -31,6 +34,82 @@ class ScoutEngineTest extends TestCase
3134{
3235 private const EXPECTED_TYPEMAP = ['root ' => 'object ' , 'document ' => 'bson ' , 'array ' => 'bson ' ];
3336
37+ public function testCreateIndexInvalidDefinition (): void
38+ {
39+ $ database = m::mock (Database::class);
40+ $ engine = new ScoutEngine ($ database , false , ['collection_invalid ' => ['foo ' => 'bar ' ]]);
41+
42+ $ this ->expectException (LogicException::class);
43+ $ this ->expectExceptionMessage ('Invalid search index definition for collection "collection_invalid", the "mappings" key is required. ' );
44+ $ engine ->createIndex ('collection_invalid ' );
45+ }
46+
47+ public function testCreateIndex (): void
48+ {
49+ $ collectionName = 'collection_custom ' ;
50+ $ expectedDefinition = [
51+ 'mappings ' => [
52+ 'dynamic ' => true ,
53+ ],
54+ ];
55+
56+ $ database = m::mock (Database::class);
57+ $ collection = m::mock (Collection::class);
58+ $ database ->shouldReceive ('createCollection ' )
59+ ->once ()
60+ ->with ($ collectionName );
61+ $ database ->shouldReceive ('selectCollection ' )
62+ ->with ($ collectionName )
63+ ->andReturn ($ collection );
64+ $ collection ->shouldReceive ('createSearchIndex ' )
65+ ->once ()
66+ ->with ($ expectedDefinition , ['name ' => 'scout ' ]);
67+ $ collection ->shouldReceive ('listSearchIndexes ' )
68+ ->once ()
69+ ->with (['name ' => 'scout ' , 'typeMap ' => ['root ' => 'bson ' ]])
70+ ->andReturn (new ArrayIterator ([Document::fromPHP (['name ' => 'scout ' , 'status ' => 'READY ' ])]));
71+
72+ $ engine = new ScoutEngine ($ database , false , []);
73+ $ engine ->createIndex ($ collectionName );
74+ }
75+
76+ public function testCreateIndexCustomDefinition (): void
77+ {
78+ $ collectionName = 'collection_custom ' ;
79+ $ expectedDefinition = [
80+ 'mappings ' => [
81+ [
82+ 'analyzer ' => 'lucene.standard ' ,
83+ 'fields ' => [
84+ [
85+ 'name ' => 'wildcard ' ,
86+ 'type ' => 'string ' ,
87+ ],
88+ ],
89+ ],
90+ ],
91+ ];
92+
93+ $ database = m::mock (Database::class);
94+ $ collection = m::mock (Collection::class);
95+ $ database ->shouldReceive ('createCollection ' )
96+ ->once ()
97+ ->with ($ collectionName );
98+ $ database ->shouldReceive ('selectCollection ' )
99+ ->with ($ collectionName )
100+ ->andReturn ($ collection );
101+ $ collection ->shouldReceive ('createSearchIndex ' )
102+ ->once ()
103+ ->with ($ expectedDefinition , ['name ' => 'scout ' ]);
104+ $ collection ->shouldReceive ('listSearchIndexes ' )
105+ ->once ()
106+ ->with (['name ' => 'scout ' , 'typeMap ' => ['root ' => 'bson ' ]])
107+ ->andReturn (new ArrayIterator ([Document::fromPHP (['name ' => 'scout ' , 'status ' => 'READY ' ])]));
108+
109+ $ engine = new ScoutEngine ($ database , false , [$ collectionName => $ expectedDefinition ]);
110+ $ engine ->createIndex ($ collectionName );
111+ }
112+
34113 /** @param callable(): Builder $builder */
35114 #[DataProvider('provideSearchPipelines ' )]
36115 public function testSearch (Closure $ builder , array $ expectedPipeline ): void
0 commit comments