Skip to content

Commit 7a0344c

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

9 files changed

+94
-109
lines changed

src/Scout/ScoutEngine.php

Lines changed: 0 additions & 15 deletions
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

Lines changed: 0 additions & 10 deletions
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
*/

tests/Models/User.php

Lines changed: 0 additions & 10 deletions
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';
@@ -146,12 +144,4 @@ public function getScoutKey(): string
146144
{
147145
return (string) $this->getKey();
148146
}
149-
150-
public function toSearchableArray(): array
151-
{
152-
return [
153-
'id' => $this->id,
154-
'name' => $this->name,
155-
];
156-
}
157147
}

tests/Scout/Models/ScoutUser.php

Lines changed: 48 additions & 0 deletions
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 renamed to tests/Scout/Models/SearchableInSameNamespace.php

Lines changed: 1 addition & 1 deletion
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 renamed to tests/Scout/Models/SearchableModel.php

Lines changed: 7 additions & 10 deletions
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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use MongoDB\Database;
1717
use MongoDB\Driver\CursorInterface;
1818
use MongoDB\Laravel\Scout\ScoutEngine;
19-
use MongoDB\Laravel\Tests\Models\SearchableInSameNamespace;
20-
use MongoDB\Laravel\Tests\Models\SearchableModel;
19+
use MongoDB\Laravel\Tests\Scout\Models\SearchableInSameNamespace;
20+
use MongoDB\Laravel\Tests\Scout\Models\SearchableModel;
2121
use MongoDB\Laravel\Tests\TestCase;
2222
use MongoDB\Model\BSONDocument;
2323
use PHPUnit\Framework\Attributes\DataProvider;
@@ -31,7 +31,7 @@
3131
/** Unit tests that do not require an Atlas Search cluster */
3232
class ScoutEngineTest extends TestCase
3333
{
34-
private const EXPECTED_SEARCH_OPTIONS = ['typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array']];
34+
private const EXPECTED_SEARCH_OPTIONS = ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']];
3535

3636
/** @param callable(): Builder $builder */
3737
#[DataProvider('provideSearchPipelines')]
@@ -41,7 +41,7 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
4141
$database = m::mock(Database::class);
4242
$collection = m::mock(Collection::class);
4343
$database->shouldReceive('selectCollection')
44-
->with('table_searchable')
44+
->with('collection_searchable')
4545
->andReturn($collection);
4646
$cursor = m::mock(CursorInterface::class);
4747
$cursor->shouldReceive('toArray')->once()->with()->andReturn($data);
@@ -337,7 +337,7 @@ public function testPaginate()
337337
$collection = m::mock(Collection::class);
338338
$cursor = m::mock(CursorInterface::class);
339339
$database->shouldReceive('selectCollection')
340-
->with('table_searchable')
340+
->with('collection_searchable')
341341
->andReturn($collection);
342342
$collection->shouldReceive('aggregate')
343343
->once()
@@ -451,7 +451,7 @@ public function testUpdate(): void
451451
$database = m::mock(Database::class);
452452
$collection = m::mock(Collection::class);
453453
$database->shouldReceive('selectCollection')
454-
->with('table_indexable')
454+
->with('collection_indexable')
455455
->andReturn($collection);
456456
$collection->shouldReceive('bulkWrite')
457457
->once()
@@ -490,7 +490,7 @@ public function testUpdateWithSoftDelete(): void
490490
$database = m::mock(Database::class);
491491
$collection = m::mock(Collection::class);
492492
$database->shouldReceive('selectCollection')
493-
->with('table_indexable')
493+
->with('collection_indexable')
494494
->andReturn($collection);
495495
$collection->shouldReceive('bulkWrite')
496496
->once()
@@ -516,7 +516,7 @@ public function testDelete(): void
516516
$database = m::mock(Database::class);
517517
$collection = m::mock(Collection::class);
518518
$database->shouldReceive('selectCollection')
519-
->with('table_indexable')
519+
->with('collection_indexable')
520520
->andReturn($collection);
521521
$collection->shouldReceive('deleteMany')
522522
->once()
@@ -540,7 +540,7 @@ public function testDeleteWithRemoveableScoutCollection(): void
540540
$database = m::mock(Database::class);
541541
$collection = m::mock(Collection::class);
542542
$database->shouldReceive('selectCollection')
543-
->with('table_indexable')
543+
->with('collection_indexable')
544544
->andReturn($collection);
545545
$collection->shouldReceive('deleteMany')
546546
->once()

0 commit comments

Comments
 (0)