Optimize unit test about paginator of DB

This commit is contained in:
huangzhhui 2019-01-19 23:15:35 +08:00
parent 0739e3889d
commit a2d0bf7933
2 changed files with 50 additions and 23 deletions

View File

@ -1847,9 +1847,8 @@ class Builder
* Execute the query as a "select" statement.
*
* @param array|string $columns
* @return \Hyperf\Support\Collection
*/
public function get($columns = ['*'])
public function get($columns = ['*']): Collection
{
return collect($this->onceWithColumns(Arr::wrap($columns), function () {
return $this->processor->processSelect($this, $this->runSelect());
@ -1863,9 +1862,8 @@ class Builder
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Hyperf\Contract\PaginatorInterface
*/
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null): PaginatorInterface
{
$page = $page ? : Paginator::resolveCurrentPage($pageName);
@ -1886,9 +1884,7 @@ class Builder
if (! method_exists($container, 'make')) {
throw new \RuntimeException('The DI container does not support make() method.');
}
return $container->make(PaginatorInterface::class, compact(
'items', 'perPage', 'currentPage', 'options'
));
return $container->make(PaginatorInterface::class, compact('items', 'perPage', 'currentPage', 'options'));
}
/**

View File

@ -3,13 +3,19 @@
namespace HyperfTest\Database;
use Hyperf\Contract\PaginatorInterface;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\Query\Builder;
use Hyperf\Database\Query\Grammars\Grammar;
use Hyperf\Database\Query\Grammars\MySqlGrammar;
use Hyperf\Database\Query\Processors\MySqlProcessor;
use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSource;
use Hyperf\Framework\ApplicationContext;
use Hyperf\Paginator\Paginator;
use Hyperf\Utils\Collection;
use Hyperf\Utils\Context;
use Mockery;
use Mockery\MockInterface;
@ -17,6 +23,7 @@ use PHPUnit\Framework\TestCase;
use Hyperf\Database\Query\Expression as Raw;
use Hyperf\Database\Model\Builder as ModelBuilder;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
class QueryBuilderTest extends TestCase
{
@ -2604,11 +2611,16 @@ class QueryBuilderTest extends TestCase
$results = collect([['test' => 'foo'], ['test' => 'bar']]);
$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(2);
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);
Context::set('path', $path);
define('BASE_PATH', __DIR__);
$container = Mockery::mock(Container::class);
$container->shouldReceive('make')->once()->andReturnUsing(function ($interface, $args) {
return new Paginator($args['items'], $args['perPage'], $args['currentPage'], $args['options']);
});
ApplicationContext::setContainer($container);
Paginator::currentPathResolver(function () {
return Context::get('path');
});
@ -2633,21 +2645,29 @@ class QueryBuilderTest extends TestCase
$results = collect([['test' => 'foo'], ['test' => 'bar']]);
$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(2);
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);
Context::set('path', $path);
Context::set('page', $page);
define('BASE_PATH', __DIR__);
$container = Mockery::mock(Container::class);
$container->shouldReceive('make')->once()->andReturnUsing(function ($interface, $args) {
return new Paginator($args['items'], $args['perPage'], $args['currentPage'], $args['options']);
});
ApplicationContext::setContainer($container);
Paginator::currentPageResolver(function () {
return 1;
return Context::get('page');
});
Paginator::currentPathResolver(function () use ($path) {
return $path;
Paginator::currentPathResolver(function () {
return Context::get('path');
});
$result = $builder->paginate();
$this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
$this->assertEquals(new Paginator($results, $perPage, $page, [
'path' => $path,
'pageName' => $pageName,
]), $result);
@ -2656,7 +2676,6 @@ class QueryBuilderTest extends TestCase
public function testPaginateWhenNoResults()
{
$perPage = 15;
$columns = ['*'];
$pageName = 'page';
$page = 1;
$builder = $this->getMockQueryBuilder();
@ -2664,21 +2683,33 @@ class QueryBuilderTest extends TestCase
$results = [];
$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(0);
$builder->shouldNotReceive('forPage');
$builder->shouldNotReceive('get');
Context::set('path', $path);
Context::set('page', $page);
define('BASE_PATH', __DIR__);
$container = Mockery::mock(Container::class);
$container->shouldReceive('make')->once()->andReturnUsing(function ($interface, $args) {
/** @var Collection $items */
$items = $args['items'];
$items->shouldReceive('count')->andReturn(0);
$items->shouldReceive('slice')->andReturn($items->slice(0, $args['perPage']));
return new Paginator($items, $args['perPage'], $args['currentPage'], $args['options']);
});
ApplicationContext::setContainer($container);
Paginator::currentPageResolver(function () {
return 1;
return Context::get('page');
});
Paginator::currentPathResolver(function () use ($path) {
return $path;
Paginator::currentPathResolver(function () {
return Context::get('path');
});
$result = $builder->paginate();
$this->assertEquals(new LengthAwarePaginator($results, 0, $perPage, $page, [
$this->assertEquals(new Paginator($results, $perPage, $page, [
'path' => $path,
'pageName' => $pageName,
]), $result);