generated from zingimmick/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix illegal argument exception when use UUIDs as primary keys (#63)
* Reproduce issue #62 * Rector Rectify * Skip ScoutHasUuidsTest when class HasUuids not exists * Skip ScoutHasUuidsTest when class HasUuids not exists * Rector Rectify * Remove default sort option * Rector Rectify * Remove default sort option * Rector Rectify * Fix phpdoc --------- Co-authored-by: zingimmick <zingimmick@users.noreply.github.com>
- Loading branch information
1 parent
f70b6c5
commit c1833d3
Showing
6 changed files
with
196 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zing\LaravelScout\OpenSearch\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Laravel\Scout\Searchable; | ||
|
||
/** | ||
* @property string $name | ||
* @property int $is_visible | ||
* | ||
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder query() | ||
*/ | ||
class SearchableModelHasUuids extends Model | ||
{ | ||
use HasUuids; | ||
use Searchable; | ||
use SoftDeletes; | ||
|
||
protected $primaryKey = 'uuid'; | ||
|
||
/** | ||
* @return array{name: string, is_visible: int} | ||
*/ | ||
public function toSearchableArray(): array | ||
{ | ||
return [ | ||
'name' => $this->name, | ||
'is_visible' => $this->is_visible, | ||
]; | ||
} | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $fillable = ['name', 'is_visible']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zing\LaravelScout\OpenSearch\Tests; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Laravel\Scout\Builder; | ||
use Zing\LaravelScout\OpenSearch\Tests\Fixtures\SearchableModelHasUuids; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ScoutHasUuidsTest extends TestCase | ||
{ | ||
use WithFaker; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
if (! trait_exists(HasUuids::class)) { | ||
self::markTestSkipped('Support for HasUuids available since 9.0.'); | ||
} | ||
|
||
parent::setUpBeforeClass(); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$searchableModelHasUuids = new SearchableModelHasUuids(); | ||
$searchableModelHasUuids->searchableUsing() | ||
->createIndex($searchableModelHasUuids->searchableAs()); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$searchableModelHasUuids = new SearchableModelHasUuids(); | ||
$searchableModelHasUuids->searchableUsing() | ||
->deleteIndex($searchableModelHasUuids->searchableAs()); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testSearch(): void | ||
{ | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 1', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 2', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 3', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 4', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 5', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test search 6', | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'not matched', | ||
]); | ||
sleep(1); | ||
self::assertCount(6, SearchableModelHasUuids::search('test')->get()); | ||
SearchableModelHasUuids::query()->firstOrFail()->delete(); | ||
sleep(1); | ||
self::assertCount(5, SearchableModelHasUuids::search('test')->get()); | ||
self::assertCount(1, SearchableModelHasUuids::search('test')->paginate(2, 'page', 3)->items()); | ||
if (method_exists(Builder::class, 'cursor')) { | ||
self::assertCount(5, SearchableModelHasUuids::search('test')->cursor()); | ||
} | ||
|
||
self::assertCount(5, SearchableModelHasUuids::search('test')->keys()); | ||
SearchableModelHasUuids::removeAllFromSearch(); | ||
sleep(1); | ||
self::assertCount(0, SearchableModelHasUuids::search('test')->get()); | ||
self::assertCount(0, SearchableModelHasUuids::search('test')->paginate(2, 'page', 3)->items()); | ||
if (method_exists(Builder::class, 'cursor')) { | ||
self::assertCount(0, SearchableModelHasUuids::search('test')->cursor()); | ||
} | ||
|
||
self::assertCount(0, SearchableModelHasUuids::search('test')->keys()); | ||
} | ||
|
||
public function testWhere(): void | ||
{ | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test', | ||
'is_visible' => 1, | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test', | ||
'is_visible' => 1, | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'test', | ||
'is_visible' => 0, | ||
]); | ||
SearchableModelHasUuids::query()->create([ | ||
'name' => 'nothing', | ||
]); | ||
sleep(1); | ||
self::assertCount(3, SearchableModelHasUuids::search('test')->get()); | ||
self::assertCount(2, SearchableModelHasUuids::search('test')->where('is_visible', 1)->get()); | ||
if (method_exists(Builder::class, 'whereIn')) { | ||
self::assertCount(3, SearchableModelHasUuids::search('test')->whereIn('is_visible', [0, 1])->get()); | ||
self::assertCount(0, SearchableModelHasUuids::search('test')->whereIn('is_visible', [])->get()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters