Skip to content

Commit e02da52

Browse files
committed
Factorize integration tests
1 parent 4afc618 commit e02da52

File tree

3 files changed

+60
-176
lines changed

3 files changed

+60
-176
lines changed

tests/Scout/ScoutIntegrationTest.php

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,73 @@
22

33
namespace MongoDB\Laravel\Tests\Scout;
44

5+
use Illuminate\Database\Eloquent\Factories\Sequence;
6+
use Illuminate\Support\LazyCollection;
57
use MongoDB\Laravel\Tests\Models\SqlUser;
68
use MongoDB\Laravel\Tests\TestCase;
9+
use Orchestra\Testbench\Factories\UserFactory;
710

8-
use function class_exists;
911
use function Orchestra\Testbench\artisan;
12+
use function range;
1013
use function sleep;
1114

1215
class ScoutIntegrationTest extends TestCase
1316
{
14-
use SearchableTests {
15-
defineScoutDatabaseMigrations as baseDefineScoutDatabaseMigrations;
16-
}
17+
protected static string $userModel = SqlUser::class;
1718

1819
protected function defineEnvironment($app)
1920
{
20-
$this->defineScoutEnvironment($app);
21+
$app['config']->set('scout.driver', 'mongodb');
22+
$app['config']->set('scout.prefix', 'scout_');
2123
}
2224

23-
/**
24-
* Define database migrations.
25-
*
26-
* @return void
27-
*/
28-
protected function defineDatabaseMigrations()
25+
protected function defineDatabaseMigrations(): void
2926
{
30-
$this->defineScoutDatabaseMigrations();
31-
}
27+
SqlUser::executeSchema();
3228

33-
protected function defineScoutDatabaseMigrations()
34-
{
35-
$this->baseDefineScoutDatabaseMigrations();
29+
$collect = LazyCollection::make(function () {
30+
yield ['name' => 'Laravel Framework'];
3631

37-
$this->importScoutIndexFrom(SqlUser::class);
38-
}
32+
foreach (range(2, 10) as $key) {
33+
yield ['name' => 'Example ' . $key];
34+
}
3935

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'];
4538

46-
self::assertSame(0, artisan($this, 'scout:import', ['model' => $model]));
39+
foreach (range(13, 19) as $key) {
40+
yield ['name' => 'Example ' . $key];
41+
}
4742

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();
5061

51-
protected function tearDown(): void
52-
{
5362
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]));
5465

55-
parent::tearDown();
66+
sleep(5);
5667
}
5768

5869
public function testItCanUseBasicSearch()
5970
{
60-
$results = $this->itCanUseBasicSearch();
71+
$results = SqlUser::search('lar')->take(10)->get();
6172

6273
$this->assertSame([
6374
11 => 'Larry Casper',
@@ -70,7 +81,9 @@ public function testItCanUseBasicSearch()
7081

7182
public function testItCanUseBasicSearchWithQueryCallback()
7283
{
73-
$results = $this->itCanUseBasicSearchWithQueryCallback();
84+
$results = SqlUser::search('lar')->take(10)->query(function ($query) {
85+
return $query->whereNotNull('email_verified_at');
86+
})->get();
7487

7588
$this->assertSame([
7689
42 => 'Dax Larkin',
@@ -81,7 +94,7 @@ public function testItCanUseBasicSearchWithQueryCallback()
8194

8295
public function testItCanUseBasicSearchToFetchKeys()
8396
{
84-
$results = $this->itCanUseBasicSearchToFetchKeys();
97+
$results = SqlUser::search('lar')->take(10)->keys();
8598

8699
$this->assertSame([
87100
11,
@@ -94,7 +107,9 @@ public function testItCanUseBasicSearchToFetchKeys()
94107

95108
public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys()
96109
{
97-
$results = $this->itCanUseBasicSearchWithQueryCallbackToFetchKeys();
110+
$results = SqlUser::search('lar')->take(10)->query(function ($query) {
111+
return $query->whereNotNull('email_verified_at');
112+
})->keys();
98113

99114
$this->assertSame([
100115
11,
@@ -105,17 +120,10 @@ public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys()
105120
], $results->all());
106121
}
107122

108-
public function testItReturnSameKeysWithQueryCallback()
109-
{
110-
$this->assertSame(
111-
$this->itCanUseBasicSearchToFetchKeys()->all(),
112-
$this->itCanUseBasicSearchWithQueryCallbackToFetchKeys()->all(),
113-
);
114-
}
115-
116123
public function testItCanUsePaginatedSearch()
117124
{
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);
119127

120128
$this->assertSame([
121129
11 => 'Larry Casper',
@@ -131,7 +139,12 @@ public function testItCanUsePaginatedSearch()
131139

132140
public function testItCanUsePaginatedSearchWithQueryCallback()
133141
{
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);
135148

136149
$this->assertSame([
137150
42 => 'Dax Larkin',
@@ -142,14 +155,4 @@ public function testItCanUsePaginatedSearchWithQueryCallback()
142155
44 => 'Amos Larson Sr.',
143156
], $page2->pluck('name', 'id')->all());
144157
}
145-
146-
protected static function scoutDriver(): string
147-
{
148-
return 'mongodb';
149-
}
150-
151-
protected function getUserModel(): string
152-
{
153-
return SqlUser::class;
154-
}
155158
}

tests/Scout/SearchableTests.php

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

tests/TestCase.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
class TestCase extends OrchestraTestCase
1515
{
16+
/** @var class-string */
17+
protected static string $userModel = User::class;
18+
1619
/**
1720
* Get package providers.
1821
*
@@ -48,7 +51,7 @@ protected function getEnvironmentSetUp($app): void
4851
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
4952

5053
$app['config']->set('auth.model', User::class);
51-
$app['config']->set('auth.providers.users.model', static::getUserModel());
54+
$app['config']->set('auth.providers.users.model', self::$userModel);
5255
$app['config']->set('cache.driver', 'array');
5356

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

0 commit comments

Comments
 (0)