TestCommon.php 8.8 KB

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