Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zendframework/zendframework#6817-zend-paginator-…
Browse files Browse the repository at this point in the history
…dbselect-getitems-should-return-array' into develop

Close zendframework/zendframework#6812
Close zendframework/zendframework#6817
  • Loading branch information
Ocramius committed Dec 5, 2014
2 parents ed09153 + c09e916 commit 1cbf390
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getItems($offset, $itemCountPerPage)
$resultSet = clone $this->resultSetPrototype;
$resultSet->initialize($result);

return $resultSet;
return iterator_to_array($resultSet);
}

/**
Expand Down
18 changes: 13 additions & 5 deletions test/Adapter/DbSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace ZendTest\Paginator\Adapter;

use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Select;

/**
* @group Zend_Paginator
Expand All @@ -20,7 +19,7 @@ class DbSelectTest extends \PHPUnit_Framework_TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Db\Sql\Select */
protected $mockSelect;

/** @var PHPUnit_Framework_MockObject_MockObject|\Zend\Db\Sql\Select */
/** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Db\Sql\Select */
protected $mockSelectCount;

/** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Db\Adapter\Driver\StatementInterface */
Expand All @@ -35,7 +34,7 @@ class DbSelectTest extends \PHPUnit_Framework_TestCase
/** @var DbSelect */
protected $dbSelect;

public function setup()
public function setUp()
{
$this->mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
Expand All @@ -56,7 +55,7 @@ public function setup()

$this
->mockSql
->expects($this->once())
->expects($this->any())
->method('prepareStatementForSqlObject')
->with($this->isInstanceOf('Zend\Db\Sql\Select'))
->will($this->returnValue($this->mockStatement));
Expand All @@ -71,7 +70,7 @@ public function testGetItems()
$this->mockSelect->expects($this->once())->method('limit')->with($this->equalTo(10));
$this->mockSelect->expects($this->once())->method('offset')->with($this->equalTo(2));
$items = $this->dbSelect->getItems(2, 10);
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items);
$this->assertEquals(array(), $items);
}

public function testCount()
Expand All @@ -92,4 +91,13 @@ public function testCustomCount()
$count = $this->dbSelect->count();
$this->assertEquals(7, $count);
}

/**
* @group 6817
* @group 6812
*/
public function testReturnValueIsArray()
{
$this->assertInternalType('array', $this->dbSelect->getItems(0, 10));
}
}
8 changes: 4 additions & 4 deletions test/Adapter/DbTableGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGetItems()
->will($this->returnValue($mockResult));

$items = $this->dbTableGateway->getItems(2, 10);
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items);
$this->assertEquals(array(), $items);
}

public function testCount()
Expand Down Expand Up @@ -97,7 +97,7 @@ public function testGetItemsWithWhereAndOrder()
->will($this->returnValue($mockResult));

$items = $this->dbTableGateway->getItems(2, 10);
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items);
$this->assertEquals(array(), $items);
}

public function testGetItemsWithWhereAndOrderAndGroup()
Expand All @@ -118,7 +118,7 @@ public function testGetItemsWithWhereAndOrderAndGroup()
->will($this->returnValue($mockResult));

$items = $this->dbTableGateway->getItems(2, 10);
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items);
$this->assertEquals(array(), $items);
}

public function testGetItemsWithWhereAndOrderAndGroupAndHaving()
Expand All @@ -140,6 +140,6 @@ public function testGetItemsWithWhereAndOrderAndGroupAndHaving()
->will($this->returnValue($mockResult));

$items = $this->dbTableGateway->getItems(2, 10);
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items);
$this->assertEquals(array(), $items);
}
}
58 changes: 58 additions & 0 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@

namespace ZendTest\Paginator;

use ArrayIterator;
use ArrayObject;
use ReflectionMethod;
use stdClass;
use Zend\Cache\StorageFactory as CacheFactory;
use Zend\Config;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter as DbAdapter;
use Zend\Db\Sql;
use Zend\Filter;
use Zend\Paginator;
use Zend\Paginator\Adapter;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Exception;
use Zend\View;
use Zend\View\Helper;
Expand Down Expand Up @@ -423,6 +427,60 @@ public function testGetsItemsByPage()
$this->assertEquals($page1, $this->paginator->getItemsByPage(1));
}

/**
* @group 6817
* @group 6812
*/
public function testGetsItemsByPageHandleDbSelectAdapter()
{
$resultSet = new ResultSet;
$result = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$resultSet->initialize(array(
new ArrayObject(array('foo' => 'bar')),
new ArrayObject(array('foo' => 'bar')),
new ArrayObject(array('foo' => 'bar')),
));

$result
->expects($this->once())
->method('current')
->will($this->returnValue(array(DbSelect::ROW_COUNT_COLUMN_NAME => 3)));
$result->expects($this->once())->method('current')->will($this->returnValue($resultSet->getDataSource()));

$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockStatement->expects($this->any())->method('execute')->will($this->returnValue($result));
$mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($mockStatement));
$mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
$mockPlatform->expects($this->any())->method('getName')->will($this->returnValue('platform'));
$mockAdapter = $this->getMockForAbstractClass(
'Zend\Db\Adapter\Adapter',
array($mockDriver, $mockPlatform)
);
$mockSql = $this->getMock(
'Zend\Db\Sql\Sql',
array('prepareStatementForSqlObject', 'execute'),
array($mockAdapter)
);
$mockSql->expects($this->any())
->method('prepareStatementForSqlObject')
->with($this->isInstanceOf('Zend\Db\Sql\Select'))
->will($this->returnValue($mockStatement));
$mockSelect = $this->getMock('Zend\Db\Sql\Select');

$dbSelect = new DbSelect($mockSelect, $mockSql);
$this->assertInstanceOf('ArrayIterator', $resultSet->getDataSource());

$paginator = new Paginator\Paginator($dbSelect);
$this->assertInstanceOf('ArrayIterator', $paginator->getItemsByPage(1));

$paginator = new Paginator\Paginator(new Paginator\Adapter\Iterator($resultSet->getDataSource()));

foreach ($paginator as $item) {
$this->assertInstanceOf('ArrayObject', $item);
}
}

public function testGetsItemCount()
{
$this->assertEquals(101, $this->paginator->getItemCount(range(1, 101)));
Expand Down

0 comments on commit 1cbf390

Please sign in to comment.