TestCommon.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Table_TestSetup
  24. */
  25. require_once 'Zend/Db/Table/TestSetup.php';
  26. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  27. /**
  28. * @category Zend
  29. * @package Zend_Db
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Db_Table_Rowset_TestCommon extends Zend_Db_Table_TestSetup
  35. {
  36. public function testTableRowsetIterator()
  37. {
  38. $table = $this->_table['bugs'];
  39. $rows = $table->find(array(1, 2));
  40. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rows,
  41. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rows));
  42. // see if we're at the beginning
  43. $this->assertEquals(0, $rows->key());
  44. $this->assertTrue($rows->valid());
  45. // get first row and see if it's the right one
  46. $row1 = $rows->current();
  47. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  48. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  49. $bug_id = $this->_db->foldCase('bug_id');
  50. $this->assertEquals(1, $row1->$bug_id);
  51. // advance to next row
  52. $rows->next();
  53. $this->assertEquals(1, $rows->key());
  54. $this->assertTrue($rows->valid());
  55. // get second row and see if it's the right one
  56. $row2 = $rows->current();
  57. $this->assertType('Zend_Db_Table_Row_Abstract', $row2,
  58. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row2));
  59. $this->assertEquals(2, $row2->$bug_id);
  60. // advance beyond last row
  61. $rows->next();
  62. $this->assertEquals(2, $rows->key());
  63. $this->assertFalse($rows->valid());
  64. // current() returns null if beyond last row
  65. $row3 = $rows->current();
  66. $this->assertNull($row3);
  67. // rewind to beginning
  68. $result = $rows->rewind();
  69. $this->assertEquals($result, $rows);
  70. $this->assertEquals(0, $rows->key());
  71. $this->assertTrue($rows->valid());
  72. // get row at beginning and compare it to
  73. // the one we got earlier
  74. $row1Copy = $rows->current();
  75. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  76. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  77. $this->assertEquals(1, $row1->$bug_id);
  78. $this->assertSame($row1, $row1Copy);
  79. // test seeking to infinite fails
  80. $rows->rewind();
  81. try{
  82. $rows->seek(99999); // this index should not exist
  83. $this->fail('An exception should have been thrown here');
  84. }catch(Zend_Db_Table_Rowset_Exception $e){ }
  85. try{
  86. $rows->seek(-20);
  87. $this->fail('An exception should have been thrown here');
  88. }catch(Zend_Db_Table_Rowset_Exception $e){ }
  89. $rows->seek(1);
  90. $row = $rows->current();
  91. $this->assertEquals(1, $rows->key());
  92. $this->assertTrue($rows->valid());
  93. $this->assertEquals(2, $row2->$bug_id);
  94. $rows->rewind();
  95. $row = $rows->getRow(1);
  96. $this->assertEquals(0, $rows->key()); // pointer should not have moved
  97. $this->assertEquals(1, $row1->$bug_id);
  98. $rows->rewind();
  99. $row = $rows->getRow(1, true);
  100. $this->assertEquals(1, $rows->key()); // pointer should have moved
  101. $this->assertEquals(1, $row1->$bug_id);
  102. $rows->rewind();
  103. $rowcopy = $rows->seek(1)->current();
  104. $rows->rewind();
  105. $row1 = $rows->getRow(1);
  106. $this->assertSame($rowcopy, $row1);
  107. try{
  108. $rows->getRow(99999); // this index should not exist
  109. $this->fail('An exception should have been thrown here');
  110. }catch(Zend_Db_Table_Rowset_Exception $e){
  111. // has the exception correctly been overwritten by getRow() ?
  112. $this->assertRegExp('#No row could be found at position \d+#',$e->getMessage());
  113. }
  114. }
  115. public function testTableRowSetArrayAccess()
  116. {
  117. $table = $this->_table['bugs'];
  118. $rowset = $table->fetchAll();
  119. $this->assertTrue(isset($rowset[0]));
  120. $this->assertType('Zend_Db_Table_Row', $rowset[0]);
  121. }
  122. public function testTableRowsetEmpty()
  123. {
  124. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  125. $table = $this->_table['bugs'];
  126. $rows = $table->fetchAll("$bug_id = -1");
  127. $this->assertEquals(0, count($rows));
  128. $this->assertNull($rows->current());
  129. }
  130. public function testTableRowsetToArray()
  131. {
  132. $table = $this->_table['bugs'];
  133. $bug_description = $this->_db->foldCase('bug_description');
  134. $rows = $table->find(array(1, 2));
  135. $this->assertEquals(2, count($rows));
  136. // iterate through the rowset, because that's the only way
  137. // to force it to instantiate the individual Rows
  138. foreach ($rows as $row) {
  139. $row->$bug_description = 'foo';
  140. }
  141. $a = $rows->toArray();
  142. $this->assertTrue(is_array($a));
  143. $this->assertEquals(count($a), count($rows));
  144. $this->assertTrue(is_array($a[0]));
  145. $this->assertEquals(8, count($a[0]));
  146. $this->assertEquals('foo', $a[0][$bug_description]);
  147. }
  148. public function testTableRowsetGetConnected()
  149. {
  150. $table = $this->_table['bugs'];
  151. $bug_description = $this->_db->foldCase('bug_description');
  152. $rows = $table->find(1);
  153. $this->assertTrue($rows->isConnected());
  154. }
  155. public function testTableRowsetGetTable()
  156. {
  157. $table = $this->_table['bugs'];
  158. $bug_description = $this->_db->foldCase('bug_description');
  159. $rows = $table->find(1);
  160. $rows->setTable($table);
  161. $this->assertEquals($table, $rows->getTable());
  162. }
  163. public function testTableRowsetGetTableClass()
  164. {
  165. $table = $this->_table['bugs'];
  166. $bug_description = $this->_db->foldCase('bug_description');
  167. $rows = $table->find(1);
  168. $this->assertEquals(get_class($table), $rows->getTableClass());
  169. }
  170. public function testTableSerializeRowset()
  171. {
  172. $table = $this->_table['bugs'];
  173. $rows = $table->find(1);
  174. $serRows = serialize($rows);
  175. $rowsNew = unserialize($serRows);
  176. $this->assertFalse($rowsNew->isConnected());
  177. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowsNew,
  178. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
  179. $row1New = $rowsNew->current();
  180. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  181. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  182. }
  183. public function testTableSerializeRowsetExceptionWrongTable()
  184. {
  185. $table = $this->_table['bugs'];
  186. $bug_description = $this->_db->foldCase('bug_description');
  187. $rows = $table->find(1);
  188. // iterate through the rowset, because that's the only way
  189. // to force it to instantiate the individual Rows
  190. foreach ($rows as $row)
  191. {
  192. $row->$bug_description = $row->$bug_description;
  193. }
  194. $serRows = serialize($rows);
  195. $rowsNew = unserialize($serRows);
  196. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowsNew,
  197. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
  198. $table2 = $this->_table['products'];
  199. $connected = false;
  200. try {
  201. $connected = $rowsNew->setTable($table2);
  202. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  203. } catch (Zend_Exception $e) {
  204. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  205. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  206. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  207. }
  208. $this->assertFalse($connected);
  209. }
  210. }