Skip to content

Commit

Permalink
Test Paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
slavcodev committed Sep 18, 2015
1 parent 982f1bd commit b4e9ddc
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
1 change: 0 additions & 1 deletion tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function setUp()
{
parent::setUp();


if (!getenv(ApiKeyProvider::ENV_APIKEY)) {
$this->markTestSkipped();
} else {
Expand Down
95 changes: 95 additions & 0 deletions tests/Rest/PaginationTest.php
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);
}
}
}

0 comments on commit b4e9ddc

Please sign in to comment.