prestashop-module/tests/lib/api/RetailcrmApiPaginatedRequestTest.php

98 lines
2.6 KiB
PHP
Raw Normal View History

<?php
class RetailcrmApiPaginatedRequestTest extends RetailcrmTestCase
{
private $apiMock;
2021-11-03 16:19:39 +07:00
protected function setUp()
{
parent::setUp();
$this->apiMock = $this->getMockBuilder('RetailcrmProxy')
->disableOriginalConstructor()
->setMethods(
2021-11-03 16:19:39 +07:00
[
'ordersHistory',
2021-11-03 16:19:39 +07:00
]
)
2021-11-03 16:19:39 +07:00
->getMock()
;
}
public function getPageLimits()
{
2021-11-03 16:19:39 +07:00
return [
'Big history' => [2, 3, 12, 6],
'Equal history' => [2, 3, 6, 6],
'Small history' => [2, 3, 3, 3],
];
}
/**
* @dataProvider getPageLimits
*/
public function testPageLimit($limit, $pageLimit, $totalCount, $expectedTotalCount)
{
$this->apiMock->expects($this->any())
->method('ordersHistory')
2021-11-03 16:19:39 +07:00
->willReturnOnConsecutiveCalls(...$this->getHistory($limit, $totalCount))
;
$request = new RetailcrmApiPaginatedRequest();
$history = $request
->setApi($this->apiMock)
->setMethod('ordersHistory')
2021-11-03 16:19:39 +07:00
->setParams([[], '{{page}}'])
->setDataKey('history')
->setLimit($limit)
->setPageLimit($pageLimit)
->execute()
2021-11-03 16:19:39 +07:00
->getData()
;
$lastId = end($history)['id'];
$this->assertEquals($expectedTotalCount, count($history));
$this->assertEquals($expectedTotalCount, $lastId);
}
private function getHistory($limit, $totalCount)
{
$totalPageCount = ceil($totalCount / $limit);
$currentPage = 0;
while ($currentPage < $totalPageCount) {
2021-11-03 16:19:39 +07:00
$history = [];
$from = ($limit * $currentPage) + 1;
$to = ($limit * $currentPage) + $limit;
if ($to > $totalCount) {
$to = $totalCount;
}
foreach (range($from, $to) as $historyId) {
2021-11-03 16:19:39 +07:00
$history[] = [
'id' => $historyId,
2021-11-03 16:19:39 +07:00
];
}
2021-11-03 16:19:39 +07:00
++$currentPage;
yield new RetailcrmApiResponse(
'200',
json_encode(
2021-11-03 16:19:39 +07:00
[
'success' => true,
'history' => $history,
2021-11-03 16:19:39 +07:00
'pagination' => [
'limit' => $limit,
'totalCount' => $totalCount,
'currentPage' => $currentPage,
2021-11-03 16:19:39 +07:00
'totalPageCount' => $totalPageCount,
],
]
)
);
}
}
2021-11-03 16:19:39 +07:00
}