Skip to content

Commit e9b45bc

Browse files
committed
Add tests for pagination contract delegation in Builder
- Add tests verifying Builder delegates to PaginatesEloquentModels interface - Add tests verifying Builder delegates to PaginatesEloquentModelsUsingDatabase interface - Fix import ordering in Builder.php (php-cs-fixer)
1 parent 969752c commit e9b45bc

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/scout/src/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Closure;
88
use Hyperf\Contract\Arrayable;
9-
use Hyperf\Database\Connection;
109
use Hyperf\Contract\LengthAwarePaginatorInterface;
1110
use Hyperf\Contract\PaginatorInterface;
11+
use Hyperf\Database\Connection;
1212
use Hyperf\Paginator\LengthAwarePaginator;
1313
use Hyperf\Paginator\Paginator;
1414
use Hypervel\Database\Eloquent\Collection as EloquentCollection;

tests/Scout/Unit/BuilderTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Hypervel\Tests\Scout\Unit;
66

7+
use Hyperf\Paginator\LengthAwarePaginator;
78
use Hyperf\Paginator\Paginator;
89
use Hypervel\Database\Eloquent\Collection as EloquentCollection;
910
use Hypervel\Database\Eloquent\Model;
1011
use Hypervel\Scout\Builder;
12+
use Hypervel\Scout\Contracts\PaginatesEloquentModels;
13+
use Hypervel\Scout\Contracts\PaginatesEloquentModelsUsingDatabase;
1114
use Hypervel\Scout\Engine;
1215
use Hypervel\Support\Collection;
1316
use Hypervel\Tests\TestCase;
@@ -400,6 +403,110 @@ public function testSimplePaginationCorrectlyHandlesPaginatedResults()
400403
$this->assertSame(1, $paginated->currentPage());
401404
}
402405

406+
public function testPaginateDelegatesToEngineWhenImplementsPaginatesEloquentModels()
407+
{
408+
Paginator::currentPageResolver(fn () => 1);
409+
Paginator::currentPathResolver(fn () => 'http://localhost/foo');
410+
411+
$model = m::mock(Model::class);
412+
$model->shouldReceive('getPerPage')->andReturn(15);
413+
414+
// Create a mock engine that implements PaginatesEloquentModels
415+
$engine = m::mock(Engine::class . ', ' . PaginatesEloquentModels::class);
416+
$model->shouldReceive('searchableUsing')->andReturn($engine);
417+
418+
$expectedPaginator = new LengthAwarePaginator([], 0, 15, 1);
419+
420+
// The engine's paginate method should be called directly
421+
$engine->shouldReceive('paginate')
422+
->once()
423+
->with(m::type(Builder::class), 15, 1)
424+
->andReturn($expectedPaginator);
425+
426+
$builder = new Builder($model, 'test query');
427+
$result = $builder->paginate();
428+
429+
$this->assertInstanceOf(LengthAwarePaginator::class, $result);
430+
}
431+
432+
public function testSimplePaginateDelegatesToEngineWhenImplementsPaginatesEloquentModels()
433+
{
434+
Paginator::currentPageResolver(fn () => 1);
435+
Paginator::currentPathResolver(fn () => 'http://localhost/foo');
436+
437+
$model = m::mock(Model::class);
438+
$model->shouldReceive('getPerPage')->andReturn(15);
439+
440+
// Create a mock engine that implements PaginatesEloquentModels
441+
$engine = m::mock(Engine::class . ', ' . PaginatesEloquentModels::class);
442+
$model->shouldReceive('searchableUsing')->andReturn($engine);
443+
444+
$expectedPaginator = new Paginator([], 15, 1);
445+
446+
// The engine's simplePaginate method should be called directly
447+
$engine->shouldReceive('simplePaginate')
448+
->once()
449+
->with(m::type(Builder::class), 15, 1)
450+
->andReturn($expectedPaginator);
451+
452+
$builder = new Builder($model, 'test query');
453+
$result = $builder->simplePaginate();
454+
455+
$this->assertInstanceOf(Paginator::class, $result);
456+
}
457+
458+
public function testPaginateDelegatesToEngineWhenImplementsPaginatesEloquentModelsUsingDatabase()
459+
{
460+
Paginator::currentPageResolver(fn () => 1);
461+
Paginator::currentPathResolver(fn () => 'http://localhost/foo');
462+
463+
$model = m::mock(Model::class);
464+
$model->shouldReceive('getPerPage')->andReturn(15);
465+
466+
// Create a mock engine that implements PaginatesEloquentModelsUsingDatabase
467+
$engine = m::mock(Engine::class . ', ' . PaginatesEloquentModelsUsingDatabase::class);
468+
$model->shouldReceive('searchableUsing')->andReturn($engine);
469+
470+
$expectedPaginator = new LengthAwarePaginator([], 0, 15, 1);
471+
472+
// The engine's paginateUsingDatabase method should be called
473+
$engine->shouldReceive('paginateUsingDatabase')
474+
->once()
475+
->with(m::type(Builder::class), 15, 'page', 1)
476+
->andReturn($expectedPaginator);
477+
478+
$builder = new Builder($model, 'test query');
479+
$result = $builder->paginate();
480+
481+
$this->assertInstanceOf(LengthAwarePaginator::class, $result);
482+
}
483+
484+
public function testSimplePaginateDelegatesToEngineWhenImplementsPaginatesEloquentModelsUsingDatabase()
485+
{
486+
Paginator::currentPageResolver(fn () => 1);
487+
Paginator::currentPathResolver(fn () => 'http://localhost/foo');
488+
489+
$model = m::mock(Model::class);
490+
$model->shouldReceive('getPerPage')->andReturn(15);
491+
492+
// Create a mock engine that implements PaginatesEloquentModelsUsingDatabase
493+
$engine = m::mock(Engine::class . ', ' . PaginatesEloquentModelsUsingDatabase::class);
494+
$model->shouldReceive('searchableUsing')->andReturn($engine);
495+
496+
$expectedPaginator = new Paginator([], 15, 1);
497+
498+
// The engine's simplePaginateUsingDatabase method should be called
499+
$engine->shouldReceive('simplePaginateUsingDatabase')
500+
->once()
501+
->with(m::type(Builder::class), 15, 'page', 1)
502+
->andReturn($expectedPaginator);
503+
504+
$builder = new Builder($model, 'test query');
505+
$result = $builder->simplePaginate();
506+
507+
$this->assertInstanceOf(Paginator::class, $result);
508+
}
509+
403510
public function testMacroable()
404511
{
405512
Builder::macro('testMacro', function () {

0 commit comments

Comments
 (0)