public function testCreate()
 {
     $filterParams = ['$page' => 2, '$limit' => 5];
     $numberOfItems = mt_rand(100, 1000);
     $this->testedObject->prepareFilterParams($filterParams);
     $result = $this->testedObject->create($numberOfItems);
     $this->assertInstanceOf(PaginatorInfo::class, $result);
     $this->assertSame($numberOfItems, $result->getNumberOfItems());
 }
 public function testIndexAction_WithGetRequest()
 {
     $bookEntityOne = $this->bookEntityProvider->getBookEntityWithRandomData();
     $bookEntityTwo = $this->bookEntityProvider->getBookEntityWithRandomData();
     $books = [$bookEntityOne, $bookEntityTwo];
     $paginatorMock = $this->getMockBuilder(PaginatorAdapter::class)->disableOriginalConstructor()->setMethods(array('count', 'getIterator'))->getMock();
     $paginatorMock->expects($this->any())->method('count')->willReturn(2);
     $paginatorMock->expects($this->any())->method('getIterator')->willReturn($books);
     $this->filterResultsServiceMock->expects($this->once())->method('getFilteredResults')->will($this->returnValue($paginatorMock));
     $paginator = new PaginatorInfo(2, 1, 10);
     $this->paginatorInfoFactoryMock->expects($this->once())->method('create')->with(2)->willReturn($paginator);
     $result = $this->controller->dispatch(new Request());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
     $expectedResult = array('books' => $books, 'paginator' => $paginator, 'route' => 'library/books/*', 'query' => array('$page' => 1, '$limit' => 10));
     $this->assertSame($expectedResult, $result);
 }
Exemple #3
0
 public function indexAction()
 {
     try {
         $filterParams = $this->paginatorInfoFactory->prepareFilterParams($this->params()->fromQuery());
         $this->queryFilter->setQueryParameters($filterParams);
         /** @var PaginatorAdapter $paginator */
         $paginator = $this->service->getFilteredResults($this->queryFilter);
         $paginatorInfo = $this->paginatorInfoFactory->create($paginator->count());
         $paginatorInfo->preparePagesToShow();
         return ['books' => $paginator->getIterator(), 'paginator' => $paginatorInfo, 'route' => 'library/books/*', 'query' => $filterParams];
     } catch (QueryFilterException $e) {
         $this->flashMessenger()->addErrorMessage($e->getMessage());
     } catch (PaginationException $e) {
         $this->flashMessenger()->addErrorMessage($e->getMessage());
     }
     return array('books' => array());
 }
 public function indexAction()
 {
     try {
         $this->queryFilter->setQueryParameters($this->paginatorInfoFactory->prepareFilterParams($this->params()->fromQuery()));
         /** @var PaginatorAdapter $paginator */
         $paginator = $this->service->getFilteredResults($this->queryFilter, $hydrationMode = Query::HYDRATE_ARRAY);
         $paginatorInfo = $this->paginatorInfoFactory->create($paginator->count());
         $data = array('data' => $paginator->getIterator());
         if ($paginatorInfo->shouldDisplay()) {
             $data['pagination'] = $paginatorInfo->toArray();
         }
         return new JsonModel($data);
     } catch (QueryFilterException $e) {
         throw new Exception\BadRequestException($e->getMessage(), Response::STATUS_CODE_400, $e);
     } catch (PaginationException $e) {
         throw new Exception\BadRequestException($e->getMessage(), Response::STATUS_CODE_400, $e);
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }