first commit
This commit is contained in:
16
laravel/tests/Unit/ExampleTest.php
Normal file
16
laravel/tests/Unit/ExampleTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_that_true_is_true(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
121
laravel/tests/Unit/QueryFilterPipelineTest.php
Normal file
121
laravel/tests/Unit/QueryFilterPipelineTest.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Contracts\FilterFactoryInterface;
|
||||
use App\Contracts\FilterInterface;
|
||||
use App\Contracts\RequestFilterInterface;
|
||||
use App\Pipelines\QueryFilterPipeline;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use InvalidArgumentException;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
use Throwable;
|
||||
|
||||
final class QueryFilterPipelineTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function testApplyFiltersWithValidPerPage(): void
|
||||
{
|
||||
$builder = Mockery::mock(Builder::class);
|
||||
$pipeline = Mockery::mock(PipelineContract::class);
|
||||
$factory = Mockery::mock(FilterFactoryInterface::class);
|
||||
$filterRequest = Mockery::mock(RequestFilterInterface::class);
|
||||
$paginator = Mockery::mock(LengthAwarePaginator::class);
|
||||
|
||||
Config::shouldReceive('get')->with('filters.pagination.per_page', 15)->andReturn(15);
|
||||
Config::shouldReceive('get')->with('filters.pagination.max_per_page', 50)->andReturn(50);
|
||||
|
||||
Log::spy();
|
||||
|
||||
$values = ['is_done' => true];
|
||||
$filter = Mockery::mock(FilterInterface::class);
|
||||
|
||||
$filterRequest->shouldReceive('values')->once()->andReturn($values);
|
||||
$filterRequest->shouldReceive('filters')->once()->andReturn(array_keys($values));
|
||||
$filterRequest->shouldReceive('perPage')->once()->andReturn(10);
|
||||
|
||||
$factory->shouldReceive('make')->with('is_done', $values)->andReturn($filter);
|
||||
|
||||
$pipeline->shouldReceive('send')->with($builder)->andReturnSelf();
|
||||
$pipeline->shouldReceive('through')->with([$filter])->andReturnSelf();
|
||||
$pipeline->shouldReceive('thenReturn')->andReturn($builder);
|
||||
|
||||
$builder->shouldReceive('paginate')->with(10)->andReturn($paginator);
|
||||
|
||||
$service = new QueryFilterPipeline($pipeline, $factory);
|
||||
|
||||
$result = $service->applyFilters($builder, $filterRequest);
|
||||
|
||||
$this->assertSame($paginator, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function testApplyFiltersWithPerPageExceedingMaxThrowsException(): void
|
||||
{
|
||||
$builder = Mockery::mock(Builder::class);
|
||||
$pipeline = Mockery::mock(PipelineContract::class);
|
||||
$factory = Mockery::mock(FilterFactoryInterface::class);
|
||||
$filterRequest = Mockery::mock(RequestFilterInterface::class);
|
||||
|
||||
Config::shouldReceive('get')->with('filters.pagination.per_page', 15)->andReturn(15);
|
||||
Config::shouldReceive('get')->with('filters.pagination.max_per_page', 50)->andReturn(50);
|
||||
|
||||
$filterRequest->shouldReceive('perPage')->once()->andReturn(100);
|
||||
|
||||
$service = new QueryFilterPipeline($pipeline, $factory);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('per_page cannot exceed 50');
|
||||
|
||||
$service->applyFilters($builder, $filterRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function testApplyFiltersWithNoFilters(): void
|
||||
{
|
||||
$builder = Mockery::mock(Builder::class);
|
||||
$pipeline = Mockery::mock(PipelineContract::class);
|
||||
$factory = Mockery::mock(FilterFactoryInterface::class);
|
||||
$filterRequest = Mockery::mock(RequestFilterInterface::class);
|
||||
$paginator = Mockery::mock(LengthAwarePaginator::class);
|
||||
|
||||
Config::shouldReceive('get')->with('filters.pagination.per_page', 15)->andReturn(15);
|
||||
Config::shouldReceive('get')->with('filters.pagination.max_per_page', 50)->andReturn(50);
|
||||
Log::spy();
|
||||
|
||||
$filterRequest->shouldReceive('values')->once()->andReturn([]);
|
||||
$filterRequest->shouldReceive('filters')->once()->andReturn([]);
|
||||
$filterRequest->shouldReceive('perPage')->once()->andReturn(null);
|
||||
|
||||
$pipeline->shouldReceive('send')->with($builder)->andReturnSelf();
|
||||
$pipeline->shouldReceive('through')->with([])->andReturnSelf();
|
||||
$pipeline->shouldReceive('thenReturn')->andReturn($builder);
|
||||
|
||||
$builder->shouldReceive('paginate')->with(15)->andReturn($paginator);
|
||||
|
||||
$service = new QueryFilterPipeline($pipeline, $factory);
|
||||
|
||||
$result = $service->applyFilters($builder, $filterRequest);
|
||||
|
||||
$this->assertSame($paginator, $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user