Skip to content

Commit

Permalink
Fix tests and use distinct model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 7, 2025
1 parent b358b82 commit c628969
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 136 deletions.
15 changes: 0 additions & 15 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,6 @@ private function usesSoftDelete(Model|EloquentCollection $model): bool
return in_array(SoftDeletes::class, class_uses_recursive($model));
}

private function getMapping(Model $model): array
{
$mapping = self::DEFAULT_DEFINITION;

if ($this->usesSoftDelete($model)) {
// This field is a boolean represented with the integers 0 and 1
// https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
$mapping['fields']['__soft_deleted'] ??= [
'type' => 'boolean',
];
}

return $mapping;
}

/**
* Wait for the callback to return true, use it for asynchronous
* Atlas Search index management operations.
Expand Down
14 changes: 0 additions & 14 deletions tests/Models/SqlUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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 @@ -20,7 +19,6 @@
class SqlUser extends EloquentModel
{
use HybridRelations;
use Searchable;

protected $connection = 'sqlite';
protected $table = 'users';
Expand Down Expand Up @@ -56,14 +54,6 @@ public function experiences(): MorphToMany
return $this->morphedByMany(Experience::class, 'experienced');
}

public function toSearchableArray()
{
return [
'email' => $this->email,
'name' => $this->name,
];
}

/**
* Check if we need to run the schema.
*/
Expand All @@ -76,10 +66,6 @@ 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
15 changes: 0 additions & 15 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use MongoDB\Laravel\Eloquent\Builder;
use MongoDB\Laravel\Eloquent\DocumentModel;
use MongoDB\Laravel\Eloquent\MassPrunable;
Expand All @@ -38,7 +37,6 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
use CanResetPassword;
use Notifiable;
use MassPrunable;
use Searchable;

protected $keyType = 'string';
protected $connection = 'mongodb';
Expand Down Expand Up @@ -141,17 +139,4 @@ public function prunable(): Builder
{
return $this->where('age', '>', 18);
}

public function getScoutKey(): string
{
return (string) $this->getKey();
}

public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
}
48 changes: 48 additions & 0 deletions tests/Scout/Models/ScoutUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Scout\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
use Laravel\Scout\Searchable;
use MongoDB\Laravel\Eloquent\SoftDeletes;

use function assert;

class ScoutUser extends Model
{
use Searchable;
use SoftDeletes;

protected $connection = 'sqlite';
protected $table = 'scout_users';
protected static $unguarded = true;

public function searchableAs(): string
{
return 'mongodb_scout_users';
}

/**
* Check if we need to run the schema.
*/
public static function executeSchema(): void
{
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);

$schema->dropIfExists('scout_users');
$schema->create('scout_users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->nullable();
$table->date('email_verified_at')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;
namespace MongoDB\Laravel\Tests\Scout\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;
namespace MongoDB\Laravel\Tests\Scout\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Eloquent\SoftDeletes;

class SearchableModel extends Model
{
use Searchable;
use SoftDeletes;

protected $connection = 'sqlite';
protected $table = 'searchable';
protected static $unguarded = true;
protected $connection = 'sqlite';
protected $fillable = ['id', 'name', 'date'];

public function searchableAs(): string
{
return 'table_searchable';
return 'collection_searchable';
}

public function indexableAs(): string
{
return 'table_indexable';
return 'collection_indexable';
}

/**
Expand Down
32 changes: 8 additions & 24 deletions tests/Scout/ScoutEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@
use Illuminate\Support\Collection as IlluminateCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Jobs\RemoveFromSearch;
use LogicException;
use Mockery as m;
use MongoDB\BSON\Document;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\CursorInterface;
use MongoDB\Laravel\Scout\ScoutEngine;
use MongoDB\Laravel\Tests\Models\SearchableInSameNamespace;
use MongoDB\Laravel\Tests\Models\SearchableModel;
use MongoDB\Laravel\Tests\Scout\Models\SearchableModel;
use MongoDB\Laravel\Tests\TestCase;
use MongoDB\Model\BSONDocument;
use PHPUnit\Framework\Attributes\DataProvider;

use function array_replace_recursive;
use function env;
use function serialize;
use function sprintf;
use function unserialize;

/** Unit tests that do not require an Atlas Search cluster */
class ScoutEngineTest extends TestCase
{
private const EXPECTED_SEARCH_OPTIONS = ['typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array']];
private const EXPECTED_SEARCH_OPTIONS = ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']];

/** @param callable(): Builder $builder */
#[DataProvider('provideSearchPipelines')]
Expand All @@ -41,7 +37,7 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
$database = m::mock(Database::class);
$collection = m::mock(Collection::class);
$database->shouldReceive('selectCollection')
->with('table_searchable')
->with('collection_searchable')
->andReturn($collection);
$cursor = m::mock(CursorInterface::class);
$cursor->shouldReceive('toArray')->once()->with()->andReturn($data);
Expand Down Expand Up @@ -337,7 +333,7 @@ public function testPaginate()
$collection = m::mock(Collection::class);
$cursor = m::mock(CursorInterface::class);
$database->shouldReceive('selectCollection')
->with('table_searchable')
->with('collection_searchable')
->andReturn($collection);
$collection->shouldReceive('aggregate')
->once()
Expand Down Expand Up @@ -451,7 +447,7 @@ public function testUpdate(): void
$database = m::mock(Database::class);
$collection = m::mock(Collection::class);
$database->shouldReceive('selectCollection')
->with('table_indexable')
->with('collection_indexable')
->andReturn($collection);
$collection->shouldReceive('bulkWrite')
->once()
Expand Down Expand Up @@ -490,7 +486,7 @@ public function testUpdateWithSoftDelete(): void
$database = m::mock(Database::class);
$collection = m::mock(Collection::class);
$database->shouldReceive('selectCollection')
->with('table_indexable')
->with('collection_indexable')
->andReturn($collection);
$collection->shouldReceive('bulkWrite')
->once()
Expand All @@ -516,7 +512,7 @@ public function testDelete(): void
$database = m::mock(Database::class);
$collection = m::mock(Collection::class);
$database->shouldReceive('selectCollection')
->with('table_indexable')
->with('collection_indexable')
->andReturn($collection);
$collection->shouldReceive('deleteMany')
->once()
Expand All @@ -540,7 +536,7 @@ public function testDeleteWithRemoveableScoutCollection(): void
$database = m::mock(Database::class);
$collection = m::mock(Collection::class);
$database->shouldReceive('selectCollection')
->with('table_indexable')
->with('collection_indexable')
->andReturn($collection);
$collection->shouldReceive('deleteMany')
->once()
Expand All @@ -549,16 +545,4 @@ public function testDeleteWithRemoveableScoutCollection(): void
$engine = new ScoutEngine($database, softDelete: false, prefix: 'ignored_prefix_');
$engine->delete($job->models);
}

public function testItCannotIndexInTheSameNamespace()
{
self::expectException(LogicException::class);
self::expectExceptionMessage(sprintf(
'The MongoDB Scout collection "%s.searchable_in_same_namespaces" must use a different collection from the collection name of the model "%s". Set the "scout.prefix" configuration or use a distinct MongoDB database',
env('MONGODB_DATABASE', 'unittest'),
SearchableInSameNamespace::class,
),);

SearchableInSameNamespace::create(['name' => 'test']);
}
}
Loading

0 comments on commit c628969

Please sign in to comment.