TestCommon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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-2012 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. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Db
  33. * @group Zend_Db_Table
  34. * @group Zend_Db_Table_Rowset
  35. */
  36. abstract class Zend_Db_Table_Rowset_TestCommon extends Zend_Db_Table_TestSetup
  37. {
  38. public function testTableRowsetIterator()
  39. {
  40. $table = $this->_table['bugs'];
  41. $rows = $table->find(array(1, 2));
  42. $this->assertTrue($rows instanceof Zend_Db_Table_Rowset_Abstract,
  43. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rows));
  44. // see if we're at the beginning
  45. $this->assertEquals(0, $rows->key());
  46. $this->assertTrue($rows->valid());
  47. // get first row and see if it's the right one
  48. $row1 = $rows->current();
  49. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  50. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  51. $bug_id = $this->_db->foldCase('bug_id');
  52. $this->assertEquals(1, $row1->$bug_id);
  53. // advance to next row
  54. $rows->next();
  55. $this->assertEquals(1, $rows->key());
  56. $this->assertTrue($rows->valid());
  57. // get second row and see if it's the right one
  58. $row2 = $rows->current();
  59. $this->assertTrue($row2 instanceof Zend_Db_Table_Row_Abstract,
  60. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row2));
  61. $this->assertEquals(2, $row2->$bug_id);
  62. // advance beyond last row
  63. $rows->next();
  64. $this->assertEquals(2, $rows->key());
  65. $this->assertFalse($rows->valid());
  66. // current() returns null if beyond last row
  67. $row3 = $rows->current();
  68. $this->assertNull($row3);
  69. // rewind to beginning
  70. $result = $rows->rewind();
  71. $this->assertEquals($result, $rows);
  72. $this->assertEquals(0, $rows->key());
  73. $this->assertTrue($rows->valid());
  74. // get row at beginning and compare it to
  75. // the one we got earlier
  76. $row1Copy = $rows->current();
  77. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  78. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  79. $this->assertEquals(1, $row1->$bug_id);
  80. $this->assertSame($row1, $row1Copy);
  81. // test seeking to infinite fails
  82. $rows->rewind();
  83. try{
  84. $rows->seek(99999); // this index should not exist
  85. $this->fail('An exception should have been thrown here');
  86. }catch(Zend_Db_Table_Rowset_Exception $e){ }
  87. try{
  88. $rows->seek(-20);
  89. $this->fail('An exception should have been thrown here');
  90. }catch(Zend_Db_Table_Rowset_Exception $e){ }
  91. $rows->seek(1);
  92. $row = $rows->current();
  93. $this->assertEquals(1, $rows->key());
  94. $this->assertTrue($rows->valid());
  95. $this->assertEquals(2, $row2->$bug_id);
  96. $rows->rewind();
  97. $row = $rows->getRow(1);
  98. $this->assertEquals(0, $rows->key()); // pointer should not have moved
  99. $this->assertEquals(1, $row1->$bug_id);
  100. $rows->rewind();
  101. $row = $rows->getRow(1, true);
  102. $this->assertEquals(1, $rows->key()); // pointer should have moved
  103. $this->assertEquals(1, $row1->$bug_id);
  104. $rows->rewind();
  105. $rowcopy = $rows->seek(1)->current();
  106. $rows->rewind();
  107. $row1 = $rows->getRow(1);
  108. $this->assertSame($rowcopy, $row1);
  109. try{
  110. $rows->getRow(99999); // this index should not exist
  111. $this->fail('An exception should have been thrown here');
  112. }catch(Zend_Db_Table_Rowset_Exception $e){
  113. // has the exception correctly been overwritten by getRow() ?
  114. $this->assertRegExp('#No row could be found at position \d+#',$e->getMessage());
  115. }
  116. }
  117. public function testTableRowSetArrayAccess()
  118. {
  119. $table = $this->_table['bugs'];
  120. $rowset = $table->fetchAll();
  121. $this->assertTrue(isset($rowset[0]));
  122. $this->assertTrue($rowset[0] instanceof Zend_Db_Table_Row);
  123. }
  124. public function testTableRowsetEmpty()
  125. {
  126. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  127. $table = $this->_table['bugs'];
  128. $rows = $table->fetchAll("$bug_id = -1");
  129. $this->assertEquals(0, count($rows));
  130. $this->assertNull($rows->current());
  131. }
  132. public function testTableRowsetToArray()
  133. {
  134. $table = $this->_table['bugs'];
  135. $bug_description = $this->_db->foldCase('bug_description');
  136. $rows = $table->find(array(1, 2));
  137. $this->assertEquals(2, count($rows));
  138. // iterate through the rowset, because that's the only way
  139. // to force it to instantiate the individual Rows
  140. foreach ($rows as $row) {
  141. $row->$bug_description = 'foo';
  142. }
  143. $a = $rows->toArray();
  144. $this->assertTrue(is_array($a));
  145. $this->assertEquals(count($a), count($rows));
  146. $this->assertTrue(is_array($a[0]));
  147. $this->assertEquals(8, count($a[0]));
  148. $this->assertEquals('foo', $a[0][$bug_description]);
  149. }
  150. public function testTableRowsetGetConnected()
  151. {
  152. $table = $this->_table['bugs'];
  153. $bug_description = $this->_db->foldCase('bug_description');
  154. $rows = $table->find(1);
  155. $this->assertTrue($rows->isConnected());
  156. }
  157. public function testTableRowsetGetTable()
  158. {
  159. $table = $this->_table['bugs'];
  160. $bug_description = $this->_db->foldCase('bug_description');
  161. $rows = $table->find(1);
  162. $rows->setTable($table);
  163. $this->assertEquals($table, $rows->getTable());
  164. }
  165. public function testTableRowsetGetTableClass()
  166. {
  167. $table = $this->_table['bugs'];
  168. $bug_description = $this->_db->foldCase('bug_description');
  169. $rows = $table->find(1);
  170. $this->assertEquals(get_class($table), $rows->getTableClass());
  171. }
  172. public function testTableSerializeRowset()
  173. {
  174. $table = $this->_table['bugs'];
  175. $rows = $table->find(1);
  176. $serRows = serialize($rows);
  177. $rowsNew = unserialize($serRows);
  178. $this->assertFalse($rowsNew->isConnected());
  179. $this->assertTrue($rowsNew instanceof Zend_Db_Table_Rowset_Abstract,
  180. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
  181. $row1New = $rowsNew->current();
  182. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  183. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  184. }
  185. public function testTableSerializeRowsetExceptionWrongTable()
  186. {
  187. $table = $this->_table['bugs'];
  188. $bug_description = $this->_db->foldCase('bug_description');
  189. $rows = $table->find(1);
  190. // iterate through the rowset, because that's the only way
  191. // to force it to instantiate the individual Rows
  192. foreach ($rows as $row)
  193. {
  194. $row->$bug_description = $row->$bug_description;
  195. }
  196. $serRows = serialize($rows);
  197. $rowsNew = unserialize($serRows);
  198. $this->assertTrue($rowsNew instanceof Zend_Db_Table_Rowset_Abstract,
  199. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowsNew));
  200. $table2 = $this->_table['products'];
  201. $connected = false;
  202. try {
  203. $connected = $rowsNew->setTable($table2);
  204. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  205. } catch (Zend_Exception $e) {
  206. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  207. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  208. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  209. }
  210. $this->assertFalse($connected);
  211. }
  212. /**
  213. * @group ZF-9213
  214. * @group ZF-10173
  215. */
  216. public function testTableRowsetIndexesValid()
  217. {
  218. $rowset = $this->_table['bugs']->fetchAll();
  219. try {
  220. $rowset[-1];
  221. $this->fail();
  222. } catch (Exception $e) {
  223. $this->assertTrue($e instanceof Zend_Db_Table_Rowset_Exception);
  224. $this->assertContains('Illegal index', $e->getMessage());
  225. }
  226. $this->assertTrue($rowset[0] instanceof Zend_Db_Table_Row);
  227. try {
  228. $row = $rowset[count($rowset) + 1];
  229. $this->fail();
  230. } catch (Exception $e) {
  231. $this->assertTrue($e instanceof Zend_Db_Table_Rowset_Exception);
  232. $this->assertContains('Illegal index', $e->getMessage());
  233. }
  234. $this->assertEquals(0, $rowset->key());
  235. }
  236. /**
  237. * @group ZF-8486
  238. */
  239. public function testTableRowsetIteratesAndWillReturnLastRowAfter()
  240. {
  241. $table = $this->_table['bugs'];
  242. $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
  243. foreach ($rowset as $row) {
  244. $lastRow = $row;
  245. }
  246. $numRows = $rowset->count();
  247. $this->assertEquals(4, $numRows);
  248. $rowset->seek(3);
  249. $seekLastRow = $rowset->current();
  250. $this->assertSame($lastRow, $seekLastRow);
  251. }
  252. /**
  253. * @group ZF-8486
  254. */
  255. public function testTableRowsetThrowsExceptionOnInvalidSeek()
  256. {
  257. $table = $this->_table['bugs'];
  258. $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
  259. $rowset->seek(3);
  260. $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'Illegal index 4');
  261. $rowset->seek(4);
  262. }
  263. /**
  264. * @group ZF-8486
  265. */
  266. public function testTableRowsetThrowsExceptionOnInvalidGetRow()
  267. {
  268. $table = $this->_table['bugs'];
  269. $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
  270. $rowset->getRow(3);
  271. $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'No row could be found at position 4');
  272. $rowset->getRow(4);
  273. }
  274. /**
  275. * @group GH-172
  276. */
  277. public function testTableRowsetContainsDisconnectedRowObjectsWhenDeserialized()
  278. {
  279. $table = $this->_table['bugs'];
  280. $ser = serialize($table->fetchAll('bug_id = 1', 'bug_id ASC'));
  281. $rowset = unserialize($ser);
  282. $row = $rowset->current();
  283. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract);
  284. $this->assertFalse($row->isConnected());
  285. $this->assertNull($row->getTable());
  286. }
  287. /**
  288. * @group GH-172
  289. */
  290. public function testConnectedRowObjectsAreCreatedByRowsetAfterSetTableIsCalled()
  291. {
  292. $table = $this->_table['bugs'];
  293. $ser = serialize($table->fetchAll('bug_id = 1', 'bug_id ASC'));
  294. $rowset = unserialize($ser);
  295. // Reconnect the rowset
  296. $rowset->setTable($table);
  297. $this->assertTrue($rowset->isConnected());
  298. $row = $rowset->current();
  299. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract);
  300. $this->assertTrue($row->isConnected());
  301. $this->assertSame($row->getTable(), $table);
  302. }
  303. }