-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Fixes pagination count when
Laravel\Scout\Builder
contains cu…
…stom query callback (#469) * Fixes pagination count when `Laravel\Scout\Builder` contains custom query callback. Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * update namespace. Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update phpunit.xml Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * move tests. Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * CS Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update Builder.php Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
5b35059
commit 6753643
Showing
12 changed files
with
143 additions
and
24 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
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,89 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Feature; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Sequence; | ||
use Illuminate\Foundation\Auth\User; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Laravel\Scout\EngineManager; | ||
use Laravel\Scout\Engines\MeiliSearchEngine; | ||
use Laravel\Scout\ScoutServiceProvider; | ||
use Laravel\Scout\Tests\Fixtures\SearchableUserModel; | ||
use Mockery as m; | ||
use Orchestra\Testbench\Factories\UserFactory; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class BuilderTest extends TestCase | ||
{ | ||
use WithFaker; | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return [ScoutServiceProvider::class]; | ||
} | ||
|
||
protected function defineEnvironment($app) | ||
{ | ||
$app->make('config')->set('scout.driver', 'fake'); | ||
} | ||
|
||
protected function defineDatabaseMigrations() | ||
{ | ||
$this->setUpFaker(); | ||
$this->loadLaravelMigrations(); | ||
|
||
UserFactory::new()->count(50)->state(new Sequence(function () { | ||
return ['name' => 'Laravel '.$this->faker()->name()]; | ||
}))->create(); | ||
|
||
UserFactory::new()->times(50)->create(); | ||
} | ||
|
||
public function test_it_can_paginate_without_custom_query_callback() | ||
{ | ||
$this->prepareScoutSearchMockUsing('Laravel'); | ||
|
||
$paginator = SearchableUserModel::search('Laravel')->paginate(); | ||
|
||
$this->assertSame(50, $paginator->total()); | ||
$this->assertSame(4, $paginator->lastPage()); | ||
$this->assertSame(15, $paginator->perPage()); | ||
} | ||
|
||
public function test_it_can_paginate_with_custom_query_callback() | ||
{ | ||
$this->prepareScoutSearchMockUsing('Laravel'); | ||
|
||
$paginator = SearchableUserModel::search('Laravel')->query(function ($builder) { | ||
return $builder->where('id', '<', 11); | ||
})->paginate(); | ||
|
||
$this->assertSame(10, $paginator->total()); | ||
$this->assertSame(1, $paginator->lastPage()); | ||
$this->assertSame(15, $paginator->perPage()); | ||
} | ||
|
||
protected function prepareScoutSearchMockUsing($searchQuery) | ||
{ | ||
$engine = m::mock('MeiliSearch\Client'); | ||
$indexes = m::mock('MeiliSearch\Endpoints\Indexes'); | ||
|
||
$manager = $this->app->make(EngineManager::class); | ||
$manager->extend('fake', function () use ($engine) { | ||
return new MeiliSearchEngine($engine); | ||
}); | ||
|
||
$query = User::where('name', 'like', $searchQuery.'%'); | ||
|
||
$engine->shouldReceive('index')->with('users')->andReturn($indexes); | ||
$indexes->shouldReceive('rawSearch')->with($searchQuery, ['limit' => 15])->andReturn([ | ||
'hits' => $query->get()->transform(function ($result) { | ||
return [ | ||
'id' => $result->getKey(), | ||
'name' => $result->name, | ||
]; | ||
}), | ||
'nbHits' => $query->count(), | ||
]); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Fixtures; | ||
|
||
use Illuminate\Foundation\Auth\User as Model; | ||
use Laravel\Scout\Searchable; | ||
|
||
class SearchableUserModel extends Model | ||
{ | ||
use Searchable; | ||
|
||
protected $table = 'users'; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
tests/MakeSearchableTest.php → tests/Unit/MakeSearchableTest.php
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
2 changes: 1 addition & 1 deletion
2
tests/MeiliSearchEngineTest.php → tests/Unit/MeiliSearchEngineTest.php
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
2 changes: 1 addition & 1 deletion
2
tests/SearchableScopeTest.php → tests/Unit/SearchableScopeTest.php
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