Skip to content

Commit

Permalink
Add missing unit tests of Paginator class
Browse files Browse the repository at this point in the history
  • Loading branch information
nunorafaelrocha committed Dec 11, 2015
1 parent b76dca1 commit cf06784
Showing 1 changed file with 249 additions and 1 deletion.
250 changes: 249 additions & 1 deletion test/Uphold/Tests/Unit/Paginator/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Uphold\Tests\Unit\Paginator;

use Uphold\Paginator\Paginator;
use Seegno\TestBundle\TestCase\BaseTestCase;
use Uphold\Exception\UpholdClientException;
use Uphold\Paginator\Paginator;

/**
* PaginatorTest.
Expand Down Expand Up @@ -77,6 +78,77 @@ public function shouldReturnCount()
$this->assertEquals($contentRange['count'], $pager->count());
}

/**
* @test
*/
public function shouldReturnZeroIfHttpCodeIs412OnCount()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 412)))
;

$pager = new Paginator($client, '/path');

$this->assertEquals(0, $pager->count());
}

/**
* @test
*/
public function shouldReturnZeroIfHttpCodeIs416OnCount()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 416)))
;

$pager = new Paginator($client, '/path');

$this->assertEquals(0, $pager->count());
}

/**
* @test
*
* @expectedException Uphold\Exception\UpholdClientException
* @expectedExceptionMessage foobar
*/
public function shouldThrownAnExceptionIfHttpCodeIsNot412Or416OnCount()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 500)))
;

$pager = new Paginator($client, '/path');
$pager->count();
}

/**
* @test
*/
Expand Down Expand Up @@ -189,4 +261,180 @@ public function shouldReturnNextResultsWithGivenModel()
$this->assertInstanceOf('Uphold\Model\Transaction', $transaction);
}
}

/**
* @test
*/
public function shouldReturnZeroIfHttpCodeIs412OnGetNext()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 412)))
;

$pager = new Paginator($client, '/path');

$this->assertEquals(array(), $pager->getNext());
}

/**
* @test
*/
public function shouldReturnZeroIfHttpCodeIs416OnGetNext()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 416)))
;

$pager = new Paginator($client, '/path');

$this->assertEquals(array(), $pager->getNext());
}

/**
* @test
*
* @expectedException Uphold\Exception\UpholdClientException
* @expectedExceptionMessage foobar
*/
public function shouldThrownAnExceptionIfHttpCodeIsNot412Or416OnGetNext()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock()
;

$client
->expects($this->once())
->method('get')
->will($this->throwException(new UpholdClientException('foobar', 'qux', 500)))
;

$pager = new Paginator($client, '/path');
$pager->getNext();
}

/**
* @test
*/
public function shouldReturnFalseIfStartIsGreaterThanOrEqualsCount()
{
$pager = $this
->getMockBuilder('Uphold\Paginator\Paginator')
->disableOriginalConstructor()
->setMethods(array('count', 'getNextRange'))
->getMock()
;

$pager
->expects($this->once())
->method('count')
->willReturn(1)
;

$pager
->expects($this->once())
->method('getNextRange')
->willReturn(array('start' => 2))
;

$this->assertEquals(false, $pager->hasNext());
}

/**
* @test
*/
public function shouldReturnTrueIfStartIsLessThanCount()
{
$pager = $this
->getMockBuilder('Uphold\Paginator\Paginator')
->disableOriginalConstructor()
->setMethods(array('count', 'getNextRange'))
->getMock()
;

$pager
->expects($this->once())
->method('count')
->willReturn(1)
;

$pager
->expects($this->once())
->method('getNextRange')
->willReturn(array('start' => 0))
;

$this->assertEquals(true, $pager->hasNext());
}

/**
* @test
*/
public function shouldReturnGivenDataIfModelIsNull()
{
$pager = $this
->getMockBuilder('Uphold\Paginator\Paginator')
->disableOriginalConstructor()
->setMethods(null)
->getMock()
;

$data = array('foo' => 'bar');

$this->assertEquals($data, $pager->hydrate($data));
}

/**
* @test
*/
public function shouldReturnHydratedResults()
{
$client = $this
->getMockBuilder('Uphold\UpholdClient')
->disableOriginalConstructor()
->getMock()
;

$pager = new Paginator($client, '/path');
$pager->setModel('Uphold\Model\User');

$data = array(
array(
'firstName' => 'foo',
'lastName' => 'bar',
),
array(
'firstName' => 'waldo',
'lastName' => 'fred',
),
);

$results = $pager->hydrate($data);

foreach ($results as $key => $object) {
$this->assertInstanceOf('Uphold\Model\User', $object);
$this->assertEquals($data[$key]['firstName'], $object->getFirstName());
$this->assertEquals($data[$key]['lastName'], $object->getLastName());
}
}
}

0 comments on commit cf06784

Please sign in to comment.