Skip to content

Commit 4afc618

Browse files
committed
Fix integration tests
1 parent 4647cf0 commit 4afc618

File tree

6 files changed

+53
-55
lines changed

6 files changed

+53
-55
lines changed

src/Scout/ScoutEngine.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
234234
],
235235
];
236236

237-
if ($builder->limit) {
238-
$pipeline[] = ['$limit' => $builder->limit];
239-
}
240-
241237
if ($offset) {
242238
$pipeline[] = ['$skip' => $offset];
243239
}
244240

241+
if ($builder->limit) {
242+
$pipeline[] = ['$limit' => $builder->limit];
243+
}
244+
245245
$options = [
246246
'allowDiskUse' => true,
247247
'typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array'],

tests/Models/SqlUser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Schema\Blueprint;
1212
use Illuminate\Database\Schema\SQLiteBuilder;
1313
use Illuminate\Support\Facades\Schema;
14+
use Laravel\Scout\Searchable;
1415
use MongoDB\Laravel\Eloquent\HybridRelations;
1516
use MongoDB\Laravel\Relations\MorphToMany;
1617

@@ -19,6 +20,7 @@
1920
class SqlUser extends EloquentModel
2021
{
2122
use HybridRelations;
23+
use Searchable;
2224

2325
protected $connection = 'sqlite';
2426
protected $table = 'users';
@@ -66,6 +68,10 @@ public static function executeSchema(): void
6668
$schema->create('users', function (Blueprint $table) {
6769
$table->increments('id');
6870
$table->string('name');
71+
$table->string('email')->nullable();
72+
$table->date('email_verified_at')->nullable();
73+
$table->string('password')->nullable();
74+
$table->string('remember_token')->nullable();
6975
$table->timestamps();
7076
});
7177

tests/Scout/ScoutEngineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ public function testPaginate()
360360
],
361361
],
362362
[
363-
'$limit' => 5,
363+
'$skip' => 10,
364364
],
365365
[
366-
'$skip' => 10,
366+
'$limit' => 5,
367367
],
368368
], $args[0]);
369369

tests/Scout/ScoutIntegrationTest.php

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

33
namespace MongoDB\Laravel\Tests\Scout;
44

5-
use MongoDB\Laravel\Tests\Models\User;
5+
use MongoDB\Laravel\Tests\Models\SqlUser;
66
use MongoDB\Laravel\Tests\TestCase;
77

88
use function class_exists;
@@ -34,7 +34,7 @@ protected function defineScoutDatabaseMigrations()
3434
{
3535
$this->baseDefineScoutDatabaseMigrations();
3636

37-
$this->importScoutIndexFrom(User::class);
37+
$this->importScoutIndexFrom(SqlUser::class);
3838
}
3939

4040
protected function importScoutIndexFrom($model = null)
@@ -50,7 +50,7 @@ protected function importScoutIndexFrom($model = null)
5050

5151
protected function tearDown(): void
5252
{
53-
self::assertSame(0, artisan($this, 'scout:delete-index', ['name' => User::class]));
53+
self::assertSame(0, artisan($this, 'scout:delete-index', ['name' => SqlUser::class]));
5454

5555
parent::tearDown();
5656
}
@@ -60,23 +60,19 @@ public function testItCanUseBasicSearch()
6060
$results = $this->itCanUseBasicSearch();
6161

6262
$this->assertSame([
63-
'Larry Casper',
64-
'Dax Larkin',
65-
'Prof. Larry Prosacco DVM',
66-
'Amos Larson Sr.',
67-
'Dana Larson Sr.',
68-
], $results->pluck('name')->all());
63+
11 => 'Larry Casper',
64+
42 => 'Dax Larkin',
65+
20 => 'Prof. Larry Prosacco DVM',
66+
43 => 'Dana Larson Sr.',
67+
44 => 'Amos Larson Sr.',
68+
], $results->pluck('name', 'id')->all());
6969
}
7070

7171
public function testItCanUseBasicSearchWithQueryCallback()
7272
{
7373
$results = $this->itCanUseBasicSearchWithQueryCallback();
7474

7575
$this->assertSame([
76-
1 => 'Laravel Framework',
77-
12 => 'Reta Larkin',
78-
40 => 'Otis Larson MD',
79-
41 => 'Gudrun Larkin',
8076
42 => 'Dax Larkin',
8177
43 => 'Dana Larson Sr.',
8278
44 => 'Amos Larson Sr.',
@@ -88,16 +84,11 @@ public function testItCanUseBasicSearchToFetchKeys()
8884
$results = $this->itCanUseBasicSearchToFetchKeys();
8985

9086
$this->assertSame([
91-
1,
9287
11,
93-
12,
94-
39,
95-
40,
96-
41,
9788
42,
89+
20,
9890
43,
9991
44,
100-
20,
10192
], $results->all());
10293
}
10394

@@ -106,16 +97,11 @@ public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys()
10697
$results = $this->itCanUseBasicSearchWithQueryCallbackToFetchKeys();
10798

10899
$this->assertSame([
109-
1,
110100
11,
111-
12,
112-
39,
113-
40,
114-
41,
115101
42,
102+
20,
116103
43,
117104
44,
118-
20,
119105
], $results->all());
120106
}
121107

@@ -132,35 +118,26 @@ public function testItCanUsePaginatedSearch()
132118
[$page1, $page2] = $this->itCanUsePaginatedSearch();
133119

134120
$this->assertSame([
135-
'Larry Casper',
136-
'Dax Larkin',
137-
'Prof. Larry Prosacco DVM',
138-
'Amos Larson Sr.',
139-
'Dana Larson Sr.',
121+
11 => 'Larry Casper',
122+
42 => 'Dax Larkin',
123+
20 => 'Prof. Larry Prosacco DVM',
140124
], $page1->pluck('name', 'id')->all());
141125

142126
$this->assertSame([
143-
41 => 'Gudrun Larkin',
144-
42 => 'Dax Larkin',
145127
43 => 'Dana Larson Sr.',
146128
44 => 'Amos Larson Sr.',
147-
20 => 'Prof. Larry Prosacco DVM',
148-
], $page2->pluck('name')->all());
129+
], $page2->pluck('name', 'id')->all());
149130
}
150131

151132
public function testItCanUsePaginatedSearchWithQueryCallback()
152133
{
153134
[$page1, $page2] = $this->itCanUsePaginatedSearchWithQueryCallback();
154135

155136
$this->assertSame([
156-
1 => 'Laravel Framework',
157-
12 => 'Reta Larkin',
158-
40 => 'Otis Larson MD',
137+
42 => 'Dax Larkin',
159138
], $page1->pluck('name', 'id')->all());
160139

161140
$this->assertSame([
162-
41 => 'Gudrun Larkin',
163-
42 => 'Dax Larkin',
164141
43 => 'Dana Larson Sr.',
165142
44 => 'Amos Larson Sr.',
166143
], $page2->pluck('name', 'id')->all());
@@ -170,4 +147,9 @@ protected static function scoutDriver(): string
170147
{
171148
return 'mongodb';
172149
}
150+
151+
protected function getUserModel(): string
152+
{
153+
return SqlUser::class;
154+
}
173155
}

tests/Scout/SearchableTests.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\Sequence;
66
use Illuminate\Foundation\Application;
77
use Illuminate\Support\LazyCollection;
8-
use MongoDB\Laravel\Tests\Models\User;
8+
use MongoDB\Laravel\Tests\Models\SqlUser;
99
use Orchestra\Testbench\Factories\UserFactory;
1010

1111
use function range;
@@ -23,6 +23,8 @@ trait SearchableTests
2323
protected function defineScoutEnvironment($app)
2424
{
2525
$app['config']->set('scout.driver', static::scoutDriver());
26+
$app['config']->set('scout.prefix', 'scout_');
27+
$app['config']->set('auth.providers.users.model', SqlUser::class);
2628
}
2729

2830
/**
@@ -32,6 +34,8 @@ protected function defineScoutDatabaseMigrations(): void
3234
{
3335
$this->loadLaravelMigrations();
3436

37+
SqlUser::executeSchema();
38+
3539
$collect = LazyCollection::make(function () {
3640
yield ['name' => 'Laravel Framework'];
3741

@@ -68,33 +72,33 @@ protected function defineScoutDatabaseMigrations(): void
6872

6973
protected function itCanUseBasicSearch()
7074
{
71-
return User::search('lar')->take(10)->get();
75+
return SqlUser::search('lar')->take(10)->get();
7276
}
7377

7478
protected function itCanUseBasicSearchWithQueryCallback()
7579
{
76-
return User::search('lar')->take(10)->query(function ($query) {
80+
return SqlUser::search('lar')->take(10)->query(function ($query) {
7781
return $query->whereNotNull('email_verified_at');
7882
})->get();
7983
}
8084

8185
protected function itCanUseBasicSearchToFetchKeys()
8286
{
83-
return User::search('lar')->take(10)->keys();
87+
return SqlUser::search('lar')->take(10)->keys();
8488
}
8589

8690
protected function itCanUseBasicSearchWithQueryCallbackToFetchKeys()
8791
{
88-
return User::search('lar')->take(10)->query(function ($query) {
92+
return SqlUser::search('lar')->take(10)->query(function ($query) {
8993
return $query->whereNotNull('email_verified_at');
9094
})->keys();
9195
}
9296

9397
protected function itCanUsePaginatedSearch()
9498
{
9599
return [
96-
User::search('lar')->take(10)->paginate(5, 'page', 1),
97-
User::search('lar')->take(10)->paginate(5, 'page', 2),
100+
SqlUser::search('lar')->take(10)->paginate(3, 'page', 1),
101+
SqlUser::search('lar')->take(10)->paginate(3, 'page', 2),
98102
];
99103
}
100104

@@ -105,8 +109,8 @@ protected function itCanUsePaginatedSearchWithQueryCallback()
105109
};
106110

107111
return [
108-
User::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 1),
109-
User::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 2),
112+
SqlUser::search('lar')->take(10)->query($queryCallback)->paginate(3, 'page', 1),
113+
SqlUser::search('lar')->take(10)->query($queryCallback)->paginate(3, 'page', 2),
110114
];
111115
}
112116
}

tests/TestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function getEnvironmentSetUp($app): void
4848
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
4949

5050
$app['config']->set('auth.model', User::class);
51-
$app['config']->set('auth.providers.users.model', User::class);
51+
$app['config']->set('auth.providers.users.model', static::getUserModel());
5252
$app['config']->set('cache.driver', 'array');
5353

5454
$app['config']->set('cache.stores.mongodb', [
@@ -70,4 +70,10 @@ protected function getEnvironmentSetUp($app): void
7070
$app['config']->set('scout.driver', 'mongodb');
7171
$app['config']->set('scout.prefix', 'scout_');
7272
}
73+
74+
/** @return class-string<\Illuminate\Foundation\Auth\User> */
75+
protected function getUserModel(): string
76+
{
77+
return User::class;
78+
}
7379
}

0 commit comments

Comments
 (0)