2
2
3
3
namespace MongoDB \Laravel \Tests \Scout ;
4
4
5
+ use Illuminate \Database \Eloquent \Factories \Sequence ;
6
+ use Illuminate \Support \LazyCollection ;
5
7
use MongoDB \Laravel \Tests \Models \SqlUser ;
6
8
use MongoDB \Laravel \Tests \TestCase ;
9
+ use Orchestra \Testbench \Factories \UserFactory ;
7
10
8
- use function class_exists ;
9
11
use function Orchestra \Testbench \artisan ;
12
+ use function range ;
10
13
use function sleep ;
11
14
12
15
class ScoutIntegrationTest extends TestCase
13
16
{
14
- use SearchableTests {
15
- defineScoutDatabaseMigrations as baseDefineScoutDatabaseMigrations;
16
- }
17
+ protected static string $ userModel = SqlUser::class;
17
18
18
19
protected function defineEnvironment ($ app )
19
20
{
20
- $ this ->defineScoutEnvironment ($ app );
21
+ $ app ['config ' ]->set ('scout.driver ' , 'mongodb ' );
22
+ $ app ['config ' ]->set ('scout.prefix ' , 'scout_ ' );
21
23
}
22
24
23
- /**
24
- * Define database migrations.
25
- *
26
- * @return void
27
- */
28
- protected function defineDatabaseMigrations ()
25
+ protected function defineDatabaseMigrations (): void
29
26
{
30
- $ this ->defineScoutDatabaseMigrations ();
31
- }
27
+ SqlUser::executeSchema ();
32
28
33
- protected function defineScoutDatabaseMigrations ()
34
- {
35
- $ this ->baseDefineScoutDatabaseMigrations ();
29
+ $ collect = LazyCollection::make (function () {
30
+ yield ['name ' => 'Laravel Framework ' ];
36
31
37
- $ this ->importScoutIndexFrom (SqlUser::class);
38
- }
32
+ foreach (range (2 , 10 ) as $ key ) {
33
+ yield ['name ' => 'Example ' . $ key ];
34
+ }
39
35
40
- protected function importScoutIndexFrom ($ model = null )
41
- {
42
- if (class_exists ($ model )) {
43
- self ::assertSame (0 , artisan ($ this , 'scout:index ' , ['name ' => $ model ]));
44
- }
36
+ yield ['name ' => 'Larry Casper ' , 'email_verified_at ' => null ];
37
+ yield ['name ' => 'Reta Larkin ' ];
45
38
46
- self ::assertSame (0 , artisan ($ this , 'scout:import ' , ['model ' => $ model ]));
39
+ foreach (range (13 , 19 ) as $ key ) {
40
+ yield ['name ' => 'Example ' . $ key ];
41
+ }
47
42
48
- sleep (1 );
49
- }
43
+ yield ['name ' => 'Prof. Larry Prosacco DVM ' , 'email_verified_at ' => null ];
44
+
45
+ foreach (range (21 , 38 ) as $ key ) {
46
+ yield ['name ' => 'Example ' . $ key , 'email_verified_at ' => null ];
47
+ }
48
+
49
+ yield ['name ' => 'Linkwood Larkin ' , 'email_verified_at ' => null ];
50
+ yield ['name ' => 'Otis Larson MD ' ];
51
+ yield ['name ' => 'Gudrun Larkin ' ];
52
+ yield ['name ' => 'Dax Larkin ' ];
53
+ yield ['name ' => 'Dana Larson Sr. ' ];
54
+ yield ['name ' => 'Amos Larson Sr. ' ];
55
+ });
56
+
57
+ UserFactory::new ()
58
+ ->times (44 )
59
+ ->state (new Sequence (...$ collect ->all ()))
60
+ ->create ();
50
61
51
- protected function tearDown (): void
52
- {
53
62
self ::assertSame (0 , artisan ($ this , 'scout:delete-index ' , ['name ' => SqlUser::class]));
63
+ self ::assertSame (0 , artisan ($ this , 'scout:index ' , ['name ' => SqlUser::class]));
64
+ self ::assertSame (0 , artisan ($ this , 'scout:import ' , ['model ' => SqlUser::class]));
54
65
55
- parent :: tearDown ( );
66
+ sleep ( 5 );
56
67
}
57
68
58
69
public function testItCanUseBasicSearch ()
59
70
{
60
- $ results = $ this -> itCanUseBasicSearch ();
71
+ $ results = SqlUser:: search ( ' lar ' )-> take ( 10 )-> get ();
61
72
62
73
$ this ->assertSame ([
63
74
11 => 'Larry Casper ' ,
@@ -70,7 +81,9 @@ public function testItCanUseBasicSearch()
70
81
71
82
public function testItCanUseBasicSearchWithQueryCallback ()
72
83
{
73
- $ results = $ this ->itCanUseBasicSearchWithQueryCallback ();
84
+ $ results = SqlUser::search ('lar ' )->take (10 )->query (function ($ query ) {
85
+ return $ query ->whereNotNull ('email_verified_at ' );
86
+ })->get ();
74
87
75
88
$ this ->assertSame ([
76
89
42 => 'Dax Larkin ' ,
@@ -81,7 +94,7 @@ public function testItCanUseBasicSearchWithQueryCallback()
81
94
82
95
public function testItCanUseBasicSearchToFetchKeys ()
83
96
{
84
- $ results = $ this -> itCanUseBasicSearchToFetchKeys ();
97
+ $ results = SqlUser:: search ( ' lar ' )-> take ( 10 )-> keys ();
85
98
86
99
$ this ->assertSame ([
87
100
11 ,
@@ -94,7 +107,9 @@ public function testItCanUseBasicSearchToFetchKeys()
94
107
95
108
public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys ()
96
109
{
97
- $ results = $ this ->itCanUseBasicSearchWithQueryCallbackToFetchKeys ();
110
+ $ results = SqlUser::search ('lar ' )->take (10 )->query (function ($ query ) {
111
+ return $ query ->whereNotNull ('email_verified_at ' );
112
+ })->keys ();
98
113
99
114
$ this ->assertSame ([
100
115
11 ,
@@ -105,17 +120,10 @@ public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys()
105
120
], $ results ->all ());
106
121
}
107
122
108
- public function testItReturnSameKeysWithQueryCallback ()
109
- {
110
- $ this ->assertSame (
111
- $ this ->itCanUseBasicSearchToFetchKeys ()->all (),
112
- $ this ->itCanUseBasicSearchWithQueryCallbackToFetchKeys ()->all (),
113
- );
114
- }
115
-
116
123
public function testItCanUsePaginatedSearch ()
117
124
{
118
- [$ page1 , $ page2 ] = $ this ->itCanUsePaginatedSearch ();
125
+ $ page1 = SqlUser::search ('lar ' )->take (10 )->paginate (3 , 'page ' , 1 );
126
+ $ page2 = SqlUser::search ('lar ' )->take (10 )->paginate (3 , 'page ' , 2 );
119
127
120
128
$ this ->assertSame ([
121
129
11 => 'Larry Casper ' ,
@@ -131,7 +139,12 @@ public function testItCanUsePaginatedSearch()
131
139
132
140
public function testItCanUsePaginatedSearchWithQueryCallback ()
133
141
{
134
- [$ page1 , $ page2 ] = $ this ->itCanUsePaginatedSearchWithQueryCallback ();
142
+ $ queryCallback = function ($ query ) {
143
+ return $ query ->whereNotNull ('email_verified_at ' );
144
+ };
145
+
146
+ $ page1 = SqlUser::search ('lar ' )->take (10 )->query ($ queryCallback )->paginate (3 , 'page ' , 1 );
147
+ $ page2 = SqlUser::search ('lar ' )->take (10 )->query ($ queryCallback )->paginate (3 , 'page ' , 2 );
135
148
136
149
$ this ->assertSame ([
137
150
42 => 'Dax Larkin ' ,
@@ -142,14 +155,4 @@ public function testItCanUsePaginatedSearchWithQueryCallback()
142
155
44 => 'Amos Larson Sr. ' ,
143
156
], $ page2 ->pluck ('name ' , 'id ' )->all ());
144
157
}
145
-
146
- protected static function scoutDriver (): string
147
- {
148
- return 'mongodb ' ;
149
- }
150
-
151
- protected function getUserModel (): string
152
- {
153
- return SqlUser::class;
154
- }
155
158
}
0 commit comments