Merge pull request #966 from limingxinleo/1.1-page

Fixed type error, when use paginator in non-worker process.
This commit is contained in:
李铭昕 2019-11-16 23:55:54 +08:00 committed by GitHub
commit 08eb341d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 3 deletions

View File

@ -3,6 +3,7 @@
## Fixed
- [#956](https://github.com/hyperf/hyperf/pull/956) Fixed bug that `RedisHandler::incr` fails in cluster mode for model cache.
- [#966](https://github.com/hyperf/hyperf/pull/966) Fixed type error, when use paginator in non-worker process.
# v1.1.6 - 2019-11-14

View File

@ -155,7 +155,7 @@ class LengthAwarePaginator extends AbstractPaginator implements Arrayable, Array
/**
* Get the current page for the request.
*/
protected function setCurrentPage(int $currentPage, string $pageName): int
protected function setCurrentPage(?int $currentPage, string $pageName): int
{
$currentPage = $currentPage ?: static::resolveCurrentPage($pageName);

View File

@ -17,6 +17,8 @@ use Hyperf\Framework\Event\BootApplication;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Paginator\Paginator;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use Psr\Http\Message\ServerRequestInterface;
class PageResolverListener implements ListenerInterface
{
@ -37,9 +39,13 @@ class PageResolverListener implements ListenerInterface
public function process(object $event)
{
Paginator::currentPageResolver(function ($pageName = 'page') {
if (! ApplicationContext::hasContainer() || ! interface_exists(RequestInterface::class)) {
if (! ApplicationContext::hasContainer() ||
! interface_exists(RequestInterface::class) ||
! Context::has(ServerRequestInterface::class)
) {
return 1;
}
$container = ApplicationContext::getContainer();
$page = $container->get(RequestInterface::class)->input($pageName);

View File

@ -130,7 +130,7 @@ class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Cou
/**
* Get the current page for the request.
*/
protected function setCurrentPage(int $currentPage): int
protected function setCurrentPage(?int $currentPage): int
{
$currentPage = $currentPage ?: static::resolveCurrentPage();

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Paginator;
use Hyperf\Di\Container;
use Hyperf\Framework\Event\BootApplication;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Request;
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Paginator\Listener\PageResolverListener;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface;
/**
* @internal
* @coversNothing
*/
class PageResolverListenerTest extends TestCase
{
protected function setUp()
{
Context::set(PsrServerRequestInterface::class, null);
Context::set('http.request.parsedData', null);
}
protected function tearDown()
{
Mockery::close();
Context::set(PsrServerRequestInterface::class, null);
Context::set('http.request.parsedData', null);
}
public function testPageResolve()
{
$this->getContainer();
$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=2', $paginator->nextPageUrl());
$listener = new PageResolverListener();
$listener->process(new BootApplication());
$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=2', $paginator->nextPageUrl());
Context::set(PsrServerRequestInterface::class, value(function () {
$request = new \Hyperf\HttpMessage\Server\Request('GET', '/index');
return $request->withQueryParams(['page' => 2]);
}));
$paginator = new LengthAwarePaginator([1, 2], 10, 2, null);
$this->assertSame('/?page=3', $paginator->nextPageUrl());
}
protected function getContainer()
{
$container = Mockery::mock(Container::class);
$container->shouldReceive('get')->with(RequestInterface::class)->andReturn(new Request());
ApplicationContext::setContainer($container);
return $container;
}
}