diff --git a/src/Adapter/DbSelect.php b/src/Adapter/DbSelect.php index 631d2fe..d92263d 100644 --- a/src/Adapter/DbSelect.php +++ b/src/Adapter/DbSelect.php @@ -103,7 +103,7 @@ public function getItems($offset, $itemCountPerPage) $resultSet = clone $this->resultSetPrototype; $resultSet->initialize($result); - return $resultSet; + return iterator_to_array($resultSet); } /** diff --git a/test/Adapter/DbSelectTest.php b/test/Adapter/DbSelectTest.php index dab9389..9204a01 100644 --- a/test/Adapter/DbSelectTest.php +++ b/test/Adapter/DbSelectTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Paginator\Adapter; use Zend\Paginator\Adapter\DbSelect; -use Zend\Db\Sql\Select; /** * @group Zend_Paginator @@ -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 */ @@ -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'); @@ -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)); @@ -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() @@ -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)); + } } diff --git a/test/Adapter/DbTableGatewayTest.php b/test/Adapter/DbTableGatewayTest.php index 342d298..5f22f2b 100644 --- a/test/Adapter/DbTableGatewayTest.php +++ b/test/Adapter/DbTableGatewayTest.php @@ -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() @@ -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() @@ -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() @@ -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); } } diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index 95827cb..66217d7 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -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; @@ -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)));