-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* This file is part of the PHP Rebilly API package. | ||
* | ||
* (c) 2015 Rebilly SRL | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rebilly\Tests\Rest; | ||
|
||
use Rebilly\Entities; | ||
use Rebilly\Client; | ||
use Rebilly\Paginator; | ||
use Rebilly\Rest\Collection; | ||
use Rebilly\Tests\TestCase; | ||
|
||
/** | ||
* Class PaginationTest. | ||
* @group wip | ||
* | ||
* @author Veaceslav Medvedev <veaceslav.medvedev@rebilly.com> | ||
*/ | ||
class PaginationTest extends TestCase | ||
{ | ||
/** @var Client */ | ||
private $client; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
if (!getenv('REBILLY_TEST_APIKEY')) { | ||
$this->markTestSkipped(); | ||
} else { | ||
$this->client = new Client([ | ||
'apiKey' => getenv('REBILLY_TEST_APIKEY'), | ||
'baseUrl' => getenv('REBILLY_TEST_HOST'), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function howToUsePaginator() | ||
{ | ||
$total = 10; | ||
$limit = 3; | ||
$offset = 3; | ||
$pages = (int) ceil($total / $limit); | ||
$current = (int) ceil($offset / $limit) + 1; | ||
|
||
$params = ['offset' => $offset, 'limit' => $limit, 'fields' => 'id']; | ||
|
||
$paginator = $this->client->customers()->paginator($params); | ||
|
||
$this->assertInstanceOf(Paginator::class, $paginator); | ||
|
||
$this->assertEquals($total, $paginator->getTotalItems()); | ||
$this->assertEquals($pages, count($paginator)); | ||
$this->assertEquals($current, $paginator->key()); | ||
|
||
$this->assertFalse($paginator->isFirst()); | ||
$this->assertFalse($paginator->isLast()); | ||
|
||
$secondSegment = $paginator->current(); | ||
|
||
$this->assertInstanceOf(Collection::class, $secondSegment); | ||
$this->assertEquals($limit, count($secondSegment)); | ||
|
||
$paginator->previous(); | ||
$firstSegment = $paginator->current(); | ||
|
||
$this->assertInstanceOf(Collection::class, $firstSegment); | ||
$this->assertEquals($limit, count($firstSegment)); | ||
$this->assertNotEquals($secondSegment[0]['id'], $firstSegment[0]['id']); | ||
|
||
$paginator->next(); | ||
$secondSegmentAgain = $paginator->current(); | ||
|
||
$this->assertInstanceOf(Collection::class, $secondSegmentAgain); | ||
$this->assertEquals($limit, count($secondSegmentAgain)); | ||
$this->assertEquals($secondSegment[0]['id'], $secondSegmentAgain[0]['id']); | ||
|
||
// Iterate pages | ||
foreach ($paginator as $collection) { | ||
$this->assertInstanceOf(Collection::class, $collection); | ||
} | ||
} | ||
} |