Skip to content

Commit c628969

Browse files
committed
Fix tests and use distinct model classes
1 parent b358b82 commit c628969

9 files changed

+126
-136
lines changed

src/Scout/ScoutEngine.php

-15
Original file line numberDiff line numberDiff line change
@@ -487,21 +487,6 @@ private function usesSoftDelete(Model|EloquentCollection $model): bool
487487
return in_array(SoftDeletes::class, class_uses_recursive($model));
488488
}
489489

490-
private function getMapping(Model $model): array
491-
{
492-
$mapping = self::DEFAULT_DEFINITION;
493-
494-
if ($this->usesSoftDelete($model)) {
495-
// This field is a boolean represented with the integers 0 and 1
496-
// https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
497-
$mapping['fields']['__soft_deleted'] ??= [
498-
'type' => 'boolean',
499-
];
500-
}
501-
502-
return $mapping;
503-
}
504-
505490
/**
506491
* Wait for the callback to return true, use it for asynchronous
507492
* Atlas Search index management operations.

tests/Models/SqlUser.php

-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Database\Schema\Blueprint;
1212
use Illuminate\Database\Schema\SQLiteBuilder;
1313
use Illuminate\Support\Facades\Schema;
14-
use Laravel\Scout\Searchable;
1514
use MongoDB\Laravel\Eloquent\HybridRelations;
1615
use MongoDB\Laravel\Relations\MorphToMany;
1716

@@ -20,7 +19,6 @@
2019
class SqlUser extends EloquentModel
2120
{
2221
use HybridRelations;
23-
use Searchable;
2422

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

59-
public function toSearchableArray()
60-
{
61-
return [
62-
'email' => $this->email,
63-
'name' => $this->name,
64-
];
65-
}
66-
6757
/**
6858
* Check if we need to run the schema.
6959
*/
@@ -76,10 +66,6 @@ public static function executeSchema(): void
7666
$schema->create('users', function (Blueprint $table) {
7767
$table->increments('id');
7868
$table->string('name');
79-
$table->string('email')->nullable();
80-
$table->date('email_verified_at')->nullable();
81-
$table->string('password')->nullable();
82-
$table->string('remember_token')->nullable();
8369
$table->timestamps();
8470
});
8571

tests/Models/User.php

-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Illuminate\Database\Eloquent\Model;
1515
use Illuminate\Notifications\Notifiable;
1616
use Illuminate\Support\Str;
17-
use Laravel\Scout\Searchable;
1817
use MongoDB\Laravel\Eloquent\Builder;
1918
use MongoDB\Laravel\Eloquent\DocumentModel;
2019
use MongoDB\Laravel\Eloquent\MassPrunable;
@@ -38,7 +37,6 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
3837
use CanResetPassword;
3938
use Notifiable;
4039
use MassPrunable;
41-
use Searchable;
4240

4341
protected $keyType = 'string';
4442
protected $connection = 'mongodb';
@@ -141,17 +139,4 @@ public function prunable(): Builder
141139
{
142140
return $this->where('age', '>', 18);
143141
}
144-
145-
public function getScoutKey(): string
146-
{
147-
return (string) $this->getKey();
148-
}
149-
150-
public function toSearchableArray(): array
151-
{
152-
return [
153-
'id' => $this->id,
154-
'name' => $this->name,
155-
];
156-
}
157142
}

tests/Scout/Models/ScoutUser.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Scout\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Database\Schema\SQLiteBuilder;
10+
use Illuminate\Support\Facades\Schema;
11+
use Laravel\Scout\Searchable;
12+
use MongoDB\Laravel\Eloquent\SoftDeletes;
13+
14+
use function assert;
15+
16+
class ScoutUser extends Model
17+
{
18+
use Searchable;
19+
use SoftDeletes;
20+
21+
protected $connection = 'sqlite';
22+
protected $table = 'scout_users';
23+
protected static $unguarded = true;
24+
25+
public function searchableAs(): string
26+
{
27+
return 'mongodb_scout_users';
28+
}
29+
30+
/**
31+
* Check if we need to run the schema.
32+
*/
33+
public static function executeSchema(): void
34+
{
35+
$schema = Schema::connection('sqlite');
36+
assert($schema instanceof SQLiteBuilder);
37+
38+
$schema->dropIfExists('scout_users');
39+
$schema->create('scout_users', function (Blueprint $table) {
40+
$table->increments('id');
41+
$table->string('name');
42+
$table->string('email')->nullable();
43+
$table->date('email_verified_at')->nullable();
44+
$table->timestamps();
45+
$table->softDeletes();
46+
});
47+
}
48+
}

tests/Models/SearchableInSameNamespace.php tests/Scout/Models/SearchableInSameNamespace.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace MongoDB\Laravel\Tests\Models;
5+
namespace MongoDB\Laravel\Tests\Scout\Models;
66

77
use Illuminate\Database\Eloquent\Model;
88
use Laravel\Scout\Searchable;

tests/Models/SearchableModel.php tests/Scout/Models/SearchableModel.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
<?php
22

3-
declare(strict_types=1);
4-
5-
namespace MongoDB\Laravel\Tests\Models;
3+
namespace MongoDB\Laravel\Tests\Scout\Models;
64

5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
77
use Laravel\Scout\Searchable;
8-
use MongoDB\Laravel\Eloquent\Model;
9-
use MongoDB\Laravel\Eloquent\SoftDeletes;
108

119
class SearchableModel extends Model
1210
{
1311
use Searchable;
1412
use SoftDeletes;
1513

16-
protected $connection = 'sqlite';
17-
protected $table = 'searchable';
18-
protected static $unguarded = true;
14+
protected $connection = 'sqlite';
15+
protected $fillable = ['id', 'name', 'date'];
1916

2017
public function searchableAs(): string
2118
{
22-
return 'table_searchable';
19+
return 'collection_searchable';
2320
}
2421

2522
public function indexableAs(): string
2623
{
27-
return 'table_indexable';
24+
return 'collection_indexable';
2825
}
2926

3027
/**

tests/Scout/ScoutEngineTest.php

+8-24
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@
88
use Illuminate\Support\Collection as IlluminateCollection;
99
use Laravel\Scout\Builder;
1010
use Laravel\Scout\Jobs\RemoveFromSearch;
11-
use LogicException;
1211
use Mockery as m;
1312
use MongoDB\BSON\Document;
1413
use MongoDB\BSON\UTCDateTime;
1514
use MongoDB\Collection;
1615
use MongoDB\Database;
1716
use MongoDB\Driver\CursorInterface;
1817
use MongoDB\Laravel\Scout\ScoutEngine;
19-
use MongoDB\Laravel\Tests\Models\SearchableInSameNamespace;
20-
use MongoDB\Laravel\Tests\Models\SearchableModel;
18+
use MongoDB\Laravel\Tests\Scout\Models\SearchableModel;
2119
use MongoDB\Laravel\Tests\TestCase;
2220
use MongoDB\Model\BSONDocument;
2321
use PHPUnit\Framework\Attributes\DataProvider;
2422

2523
use function array_replace_recursive;
26-
use function env;
2724
use function serialize;
28-
use function sprintf;
2925
use function unserialize;
3026

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

3632
/** @param callable(): Builder $builder */
3733
#[DataProvider('provideSearchPipelines')]
@@ -41,7 +37,7 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
4137
$database = m::mock(Database::class);
4238
$collection = m::mock(Collection::class);
4339
$database->shouldReceive('selectCollection')
44-
->with('table_searchable')
40+
->with('collection_searchable')
4541
->andReturn($collection);
4642
$cursor = m::mock(CursorInterface::class);
4743
$cursor->shouldReceive('toArray')->once()->with()->andReturn($data);
@@ -337,7 +333,7 @@ public function testPaginate()
337333
$collection = m::mock(Collection::class);
338334
$cursor = m::mock(CursorInterface::class);
339335
$database->shouldReceive('selectCollection')
340-
->with('table_searchable')
336+
->with('collection_searchable')
341337
->andReturn($collection);
342338
$collection->shouldReceive('aggregate')
343339
->once()
@@ -451,7 +447,7 @@ public function testUpdate(): void
451447
$database = m::mock(Database::class);
452448
$collection = m::mock(Collection::class);
453449
$database->shouldReceive('selectCollection')
454-
->with('table_indexable')
450+
->with('collection_indexable')
455451
->andReturn($collection);
456452
$collection->shouldReceive('bulkWrite')
457453
->once()
@@ -490,7 +486,7 @@ public function testUpdateWithSoftDelete(): void
490486
$database = m::mock(Database::class);
491487
$collection = m::mock(Collection::class);
492488
$database->shouldReceive('selectCollection')
493-
->with('table_indexable')
489+
->with('collection_indexable')
494490
->andReturn($collection);
495491
$collection->shouldReceive('bulkWrite')
496492
->once()
@@ -516,7 +512,7 @@ public function testDelete(): void
516512
$database = m::mock(Database::class);
517513
$collection = m::mock(Collection::class);
518514
$database->shouldReceive('selectCollection')
519-
->with('table_indexable')
515+
->with('collection_indexable')
520516
->andReturn($collection);
521517
$collection->shouldReceive('deleteMany')
522518
->once()
@@ -540,7 +536,7 @@ public function testDeleteWithRemoveableScoutCollection(): void
540536
$database = m::mock(Database::class);
541537
$collection = m::mock(Collection::class);
542538
$database->shouldReceive('selectCollection')
543-
->with('table_indexable')
539+
->with('collection_indexable')
544540
->andReturn($collection);
545541
$collection->shouldReceive('deleteMany')
546542
->once()
@@ -549,16 +545,4 @@ public function testDeleteWithRemoveableScoutCollection(): void
549545
$engine = new ScoutEngine($database, softDelete: false, prefix: 'ignored_prefix_');
550546
$engine->delete($job->models);
551547
}
552-
553-
public function testItCannotIndexInTheSameNamespace()
554-
{
555-
self::expectException(LogicException::class);
556-
self::expectExceptionMessage(sprintf(
557-
'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',
558-
env('MONGODB_DATABASE', 'unittest'),
559-
SearchableInSameNamespace::class,
560-
),);
561-
562-
SearchableInSameNamespace::create(['name' => 'test']);
563-
}
564548
}

0 commit comments

Comments
 (0)