Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 21, 2024
1 parent 4647cf0 commit 4afc618
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
],
];

if ($builder->limit) {
$pipeline[] = ['$limit' => $builder->limit];
}

if ($offset) {
$pipeline[] = ['$skip' => $offset];
}

if ($builder->limit) {
$pipeline[] = ['$limit' => $builder->limit];
}

$options = [
'allowDiskUse' => true,
'typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array'],
Expand Down
6 changes: 6 additions & 0 deletions tests/Models/SqlUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
use Laravel\Scout\Searchable;
use MongoDB\Laravel\Eloquent\HybridRelations;
use MongoDB\Laravel\Relations\MorphToMany;

Expand All @@ -19,6 +20,7 @@
class SqlUser extends EloquentModel
{
use HybridRelations;
use Searchable;

protected $connection = 'sqlite';
protected $table = 'users';
Expand Down Expand Up @@ -66,6 +68,10 @@ public static function executeSchema(): void
$schema->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->nullable();
$table->date('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->string('remember_token')->nullable();
$table->timestamps();
});

Expand Down
4 changes: 2 additions & 2 deletions tests/Scout/ScoutEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ public function testPaginate()
],
],
[
'$limit' => 5,
'$skip' => 10,
],
[
'$skip' => 10,
'$limit' => 5,
],
], $args[0]);

Expand Down
60 changes: 21 additions & 39 deletions tests/Scout/ScoutIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MongoDB\Laravel\Tests\Scout;

use MongoDB\Laravel\Tests\Models\User;
use MongoDB\Laravel\Tests\Models\SqlUser;
use MongoDB\Laravel\Tests\TestCase;

use function class_exists;
Expand Down Expand Up @@ -34,7 +34,7 @@ protected function defineScoutDatabaseMigrations()
{
$this->baseDefineScoutDatabaseMigrations();

$this->importScoutIndexFrom(User::class);
$this->importScoutIndexFrom(SqlUser::class);
}

protected function importScoutIndexFrom($model = null)
Expand All @@ -50,7 +50,7 @@ protected function importScoutIndexFrom($model = null)

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

parent::tearDown();
}
Expand All @@ -60,23 +60,19 @@ public function testItCanUseBasicSearch()
$results = $this->itCanUseBasicSearch();

$this->assertSame([
'Larry Casper',
'Dax Larkin',
'Prof. Larry Prosacco DVM',
'Amos Larson Sr.',
'Dana Larson Sr.',
], $results->pluck('name')->all());
11 => 'Larry Casper',
42 => 'Dax Larkin',
20 => 'Prof. Larry Prosacco DVM',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
], $results->pluck('name', 'id')->all());
}

public function testItCanUseBasicSearchWithQueryCallback()
{
$results = $this->itCanUseBasicSearchWithQueryCallback();

$this->assertSame([
1 => 'Laravel Framework',
12 => 'Reta Larkin',
40 => 'Otis Larson MD',
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
Expand All @@ -88,16 +84,11 @@ public function testItCanUseBasicSearchToFetchKeys()
$results = $this->itCanUseBasicSearchToFetchKeys();

$this->assertSame([
1,
11,
12,
39,
40,
41,
42,
20,
43,
44,
20,
], $results->all());
}

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

$this->assertSame([
1,
11,
12,
39,
40,
41,
42,
20,
43,
44,
20,
], $results->all());
}

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

$this->assertSame([
'Larry Casper',
'Dax Larkin',
'Prof. Larry Prosacco DVM',
'Amos Larson Sr.',
'Dana Larson Sr.',
11 => 'Larry Casper',
42 => 'Dax Larkin',
20 => 'Prof. Larry Prosacco DVM',
], $page1->pluck('name', 'id')->all());

$this->assertSame([
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
20 => 'Prof. Larry Prosacco DVM',
], $page2->pluck('name')->all());
], $page2->pluck('name', 'id')->all());
}

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

$this->assertSame([
1 => 'Laravel Framework',
12 => 'Reta Larkin',
40 => 'Otis Larson MD',
42 => 'Dax Larkin',
], $page1->pluck('name', 'id')->all());

$this->assertSame([
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
], $page2->pluck('name', 'id')->all());
Expand All @@ -170,4 +147,9 @@ protected static function scoutDriver(): string
{
return 'mongodb';
}

protected function getUserModel(): string
{
return SqlUser::class;
}
}
22 changes: 13 additions & 9 deletions tests/Scout/SearchableTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Foundation\Application;
use Illuminate\Support\LazyCollection;
use MongoDB\Laravel\Tests\Models\User;
use MongoDB\Laravel\Tests\Models\SqlUser;
use Orchestra\Testbench\Factories\UserFactory;

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

/**
Expand All @@ -32,6 +34,8 @@ protected function defineScoutDatabaseMigrations(): void
{
$this->loadLaravelMigrations();

SqlUser::executeSchema();

$collect = LazyCollection::make(function () {
yield ['name' => 'Laravel Framework'];

Expand Down Expand Up @@ -68,33 +72,33 @@ protected function defineScoutDatabaseMigrations(): void

protected function itCanUseBasicSearch()
{
return User::search('lar')->take(10)->get();
return SqlUser::search('lar')->take(10)->get();
}

protected function itCanUseBasicSearchWithQueryCallback()
{
return User::search('lar')->take(10)->query(function ($query) {
return SqlUser::search('lar')->take(10)->query(function ($query) {
return $query->whereNotNull('email_verified_at');
})->get();
}

protected function itCanUseBasicSearchToFetchKeys()
{
return User::search('lar')->take(10)->keys();
return SqlUser::search('lar')->take(10)->keys();
}

protected function itCanUseBasicSearchWithQueryCallbackToFetchKeys()
{
return User::search('lar')->take(10)->query(function ($query) {
return SqlUser::search('lar')->take(10)->query(function ($query) {
return $query->whereNotNull('email_verified_at');
})->keys();
}

protected function itCanUsePaginatedSearch()
{
return [
User::search('lar')->take(10)->paginate(5, 'page', 1),
User::search('lar')->take(10)->paginate(5, 'page', 2),
SqlUser::search('lar')->take(10)->paginate(3, 'page', 1),
SqlUser::search('lar')->take(10)->paginate(3, 'page', 2),
];
}

Expand All @@ -105,8 +109,8 @@ protected function itCanUsePaginatedSearchWithQueryCallback()
};

return [
User::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 1),
User::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 2),
SqlUser::search('lar')->take(10)->query($queryCallback)->paginate(3, 'page', 1),
SqlUser::search('lar')->take(10)->query($queryCallback)->paginate(3, 'page', 2),
];
}
}
8 changes: 7 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function getEnvironmentSetUp($app): void
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);

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

$app['config']->set('cache.stores.mongodb', [
Expand All @@ -70,4 +70,10 @@ protected function getEnvironmentSetUp($app): void
$app['config']->set('scout.driver', 'mongodb');
$app['config']->set('scout.prefix', 'scout_');
}

/** @return class-string<\Illuminate\Foundation\Auth\User> */
protected function getUserModel(): string
{
return User::class;
}
}

0 comments on commit 4afc618

Please sign in to comment.