|
4 | 4 |
|
5 | 5 | namespace Hypervel\Tests\Scout\Unit; |
6 | 6 |
|
| 7 | +use Hyperf\Paginator\LengthAwarePaginator; |
7 | 8 | use Hyperf\Paginator\Paginator; |
8 | 9 | use Hypervel\Database\Eloquent\Collection as EloquentCollection; |
9 | 10 | use Hypervel\Database\Eloquent\Model; |
10 | 11 | use Hypervel\Scout\Builder; |
| 12 | +use Hypervel\Scout\Contracts\PaginatesEloquentModels; |
| 13 | +use Hypervel\Scout\Contracts\PaginatesEloquentModelsUsingDatabase; |
11 | 14 | use Hypervel\Scout\Engine; |
12 | 15 | use Hypervel\Support\Collection; |
13 | 16 | use Hypervel\Tests\TestCase; |
@@ -400,6 +403,110 @@ public function testSimplePaginationCorrectlyHandlesPaginatedResults() |
400 | 403 | $this->assertSame(1, $paginated->currentPage()); |
401 | 404 | } |
402 | 405 |
|
| 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 | + |
403 | 510 | public function testMacroable() |
404 | 511 | { |
405 | 512 | Builder::macro('testMacro', function () { |
|
0 commit comments