| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_Table_TestSetup
- */
- require_once 'Zend/Db/Table/TestSetup.php';
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Db
- * @group Zend_Db_Table
- * @group Zend_Db_Table_Rowset
- */
- abstract class Zend_Db_Table_Rowset_TestCommon extends Zend_Db_Table_TestSetup
- {
- public function testTableRowsetIterator()
- {
- $table = $this->_table['bugs'];
- $rows = $table->find(array(1, 2));
- $this->assertTrue($rows instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rows));
- // see if we're at the beginning
- $this->assertEquals(0, $rows->key());
- $this->assertTrue($rows->valid());
- // get first row and see if it's the right one
- $row1 = $rows->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(1, $row1->$bug_id);
- // advance to next row
- $rows->next();
- $this->assertEquals(1, $rows->key());
- $this->assertTrue($rows->valid());
- // get second row and see if it's the right one
- $row2 = $rows->current();
- $this->assertTrue($row2 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row2));
- $this->assertEquals(2, $row2->$bug_id);
- // advance beyond last row
- $rows->next();
- $this->assertEquals(2, $rows->key());
- $this->assertFalse($rows->valid());
- // current() returns null if beyond last row
- $row3 = $rows->current();
- $this->assertNull($row3);
- // rewind to beginning
- $result = $rows->rewind();
- $this->assertEquals($result, $rows);
- $this->assertEquals(0, $rows->key());
- $this->assertTrue($rows->valid());
- // get row at beginning and compare it to
- // the one we got earlier
- $row1Copy = $rows->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $this->assertEquals(1, $row1->$bug_id);
- $this->assertSame($row1, $row1Copy);
- // test seeking to infinite fails
- $rows->rewind();
- try{
- $rows->seek(99999); // this index should not exist
- $this->fail('An exception should have been thrown here');
- }catch(Zend_Db_Table_Rowset_Exception $e){ }
- try{
- $rows->seek(-20);
- $this->fail('An exception should have been thrown here');
- }catch(Zend_Db_Table_Rowset_Exception $e){ }
- $rows->seek(1);
- $row = $rows->current();
- $this->assertEquals(1, $rows->key());
- $this->assertTrue($rows->valid());
- $this->assertEquals(2, $row2->$bug_id);
- $rows->rewind();
- $row = $rows->getRow(1);
- $this->assertEquals(0, $rows->key()); // pointer should not have moved
- $this->assertEquals(1, $row1->$bug_id);
- $rows->rewind();
- $row = $rows->getRow(1, true);
- $this->assertEquals(1, $rows->key()); // pointer should have moved
- $this->assertEquals(1, $row1->$bug_id);
- $rows->rewind();
- $rowcopy = $rows->seek(1)->current();
- $rows->rewind();
- $row1 = $rows->getRow(1);
- $this->assertSame($rowcopy, $row1);
- try{
- $rows->getRow(99999); // this index should not exist
- $this->fail('An exception should have been thrown here');
- }catch(Zend_Db_Table_Rowset_Exception $e){
- // has the exception correctly been overwritten by getRow() ?
- $this->assertRegExp('#No row could be found at position \d+#',$e->getMessage());
- }
- }
- public function testTableRowSetArrayAccess()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll();
- $this->assertTrue(isset($rowset[0]));
- $this->assertTrue($rowset[0] instanceof Zend_Db_Table_Row);
- }
- public function testTableRowsetEmpty()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $rows = $table->fetchAll("$bug_id = -1");
- $this->assertEquals(0, count($rows));
- $this->assertNull($rows->current());
- }
- public function testTableRowsetToArray()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $rows = $table->find(array(1, 2));
- $this->assertEquals(2, count($rows));
- // iterate through the rowset, because that's the only way
- // to force it to instantiate the individual Rows
- foreach ($rows as $row) {
- $row->$bug_description = 'foo';
- }
- $a = $rows->toArray();
- $this->assertTrue(is_array($a));
- $this->assertEquals(count($a), count($rows));
- $this->assertTrue(is_array($a[0]));
- $this->assertEquals(8, count($a[0]));
- $this->assertEquals('foo', $a[0][$bug_description]);
- }
- public function testTableRowsetGetConnected()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $rows = $table->find(1);
- $this->assertTrue($rows->isConnected());
- }
- public function testTableRowsetGetTable()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $rows = $table->find(1);
- $rows->setTable($table);
- $this->assertEquals($table, $rows->getTable());
- }
- public function testTableRowsetGetTableClass()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $rows = $table->find(1);
- $this->assertEquals(get_class($table), $rows->getTableClass());
- }
- public function testTableSerializeRowset()
- {
- $table = $this->_table['bugs'];
- $rows = $table->find(1);
- $serRows = serialize($rows);
- $rowsNew = unserialize($serRows);
- $this->assertFalse($rowsNew->isConnected());
- $this->assertTrue($rowsNew instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
- $row1New = $rowsNew->current();
- $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
- }
- public function testTableSerializeRowsetExceptionWrongTable()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $rows = $table->find(1);
- // iterate through the rowset, because that's the only way
- // to force it to instantiate the individual Rows
- foreach ($rows as $row)
- {
- $row->$bug_description = $row->$bug_description;
- }
- $serRows = serialize($rows);
- $rowsNew = unserialize($serRows);
- $this->assertTrue($rowsNew instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
- $table2 = $this->_table['products'];
- $connected = false;
- try {
- $connected = $rowsNew->setTable($table2);
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
- 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
- $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
- }
- $this->assertFalse($connected);
- }
- /**
- * @group ZF-9213
- * @group ZF-10173
- */
- public function testTableRowsetIndexesValid()
- {
- $rowset = $this->_table['bugs']->fetchAll();
- try {
- $rowset[-1];
- $this->fail();
- } catch (Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Rowset_Exception);
- $this->assertContains('Illegal index', $e->getMessage());
- }
- $this->assertTrue($rowset[0] instanceof Zend_Db_Table_Row);
- try {
- $row = $rowset[count($rowset) + 1];
- $this->fail();
- } catch (Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Rowset_Exception);
- $this->assertContains('Illegal index', $e->getMessage());
- }
- $this->assertEquals(0, $rowset->key());
- }
- /**
- * @group ZF-8486
- */
- public function testTableRowsetIteratesAndWillReturnLastRowAfter()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
- foreach ($rowset as $row) {
- $lastRow = $row;
- }
- $numRows = $rowset->count();
- $this->assertEquals(4, $numRows);
- $rowset->seek(3);
- $seekLastRow = $rowset->current();
- $this->assertSame($lastRow, $seekLastRow);
- }
- /**
- * @group ZF-8486
- */
- public function testTableRowsetThrowsExceptionOnInvalidSeek()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
- $rowset->seek(3);
- $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'Illegal index 4');
- $rowset->seek(4);
- }
- /**
- * @group ZF-8486
- */
- public function testTableRowsetThrowsExceptionOnInvalidGetRow()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
- $rowset->getRow(3);
- $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'No row could be found at position 4');
- $rowset->getRow(4);
- }
- /**
- * @group GH-172
- */
- public function testTableRowsetContainsDisconnectedRowObjectsWhenDeserialized()
- {
- $table = $this->_table['bugs'];
- $ser = serialize($table->fetchAll('bug_id = 1', 'bug_id ASC'));
- $rowset = unserialize($ser);
- $row = $rowset->current();
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract);
- $this->assertFalse($row->isConnected());
- $this->assertNull($row->getTable());
- }
- /**
- * @group GH-172
- */
- public function testConnectedRowObjectsAreCreatedByRowsetAfterSetTableIsCalled()
- {
- $table = $this->_table['bugs'];
- $ser = serialize($table->fetchAll('bug_id = 1', 'bug_id ASC'));
- $rowset = unserialize($ser);
- // Reconnect the rowset
- $rowset->setTable($table);
- $this->assertTrue($rowset->isConnected());
- $row = $rowset->current();
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract);
- $this->assertTrue($row->isConnected());
- $this->assertSame($row->getTable(), $table);
- }
- }
|