TestCommon.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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-2010 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. require_once 'Zend/Db/Table/Row.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Db
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Db
  35. * @group Zend_Db_Table
  36. * @group Zend_Db_Table_Row
  37. */
  38. abstract class Zend_Db_Table_Row_TestCommon extends Zend_Db_Table_TestSetup
  39. {
  40. protected function _testTableRow()
  41. {
  42. $table = $this->_table['bugs'];
  43. $bug_id = $this->_db->foldCase('bug_id');
  44. $bug_description = $this->_db->foldCase('bug_description');
  45. $bug_status = $this->_db->foldCase('bug_status');
  46. $created_on = $this->_db->foldCase('created_on');
  47. $updated_on = $this->_db->foldCase('updated_on');
  48. $reported_by = $this->_db->foldCase('reported_by');
  49. $assigned_to = $this->_db->foldCase('assigned_to');
  50. $verified_by = $this->_db->foldCase('verified_by');
  51. $data = array(
  52. $bug_id => '1',
  53. $bug_description => 'System needs electricity to run',
  54. $bug_status => 'NEW',
  55. $created_on => '2007-04-01',
  56. $updated_on => '2007-04-01',
  57. $reported_by => 'goofy',
  58. $assigned_to => 'mmouse',
  59. $verified_by => 'dduck'
  60. );
  61. $config = array(
  62. 'table' => $table,
  63. 'stored' => true,
  64. 'readOnly' => false,
  65. 'data' => $data,
  66. );
  67. $this->_useMyIncludePath();
  68. Zend_Loader::loadClass('My_ZendDbTable_Row_TestTableRow');
  69. return new My_ZendDbTable_Row_TestTableRow($config);
  70. }
  71. public function testTableFindRow()
  72. {
  73. $table = $this->_table['bugs'];
  74. $rowset = $table->find(1);
  75. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  76. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  77. $this->assertEquals(1, count($rowset));
  78. }
  79. public function testTableRowConstructor()
  80. {
  81. $table = $this->_table['bugs'];
  82. $config = array(
  83. 'db' => $this->_db,
  84. 'table' => $table,
  85. 'data' => array(),
  86. 'readOnly' => true
  87. );
  88. $row1 = new Zend_Db_Table_Row($config);
  89. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  90. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  91. try {
  92. $bug_description = $row1->bug_description;
  93. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  94. } catch (Zend_Exception $e) {
  95. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  96. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  97. $this->assertEquals("Specified column \"bug_description\" is not in the row", $e->getMessage());
  98. }
  99. $config['data'] = 'Invalid';
  100. try {
  101. $row1 = new Zend_Db_Table_Row($config);
  102. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  103. } catch (Zend_Exception $e) {
  104. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  105. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  106. $this->assertEquals("Data must be an array", $e->getMessage());
  107. }
  108. }
  109. // ZF-1144
  110. public function testTableRowContructorWithTableNameSpecifiedInSubclass()
  111. {
  112. $this->_useMyIncludePath();
  113. /**
  114. * @see Zend_Db_Table_Row_TestStandaloneRow
  115. */
  116. require_once 'My/ZendDbTable/Row/TestStandaloneRow.php';
  117. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  118. $row = new My_ZendDbTable_Row_TestStandaloneRow();
  119. $this->assertType('Zend_Db_Table_Abstract', $row->getTable());
  120. Zend_Db_Table_Abstract::setDefaultAdapter();
  121. }
  122. public function testTableRowReadOnly()
  123. {
  124. $table = $this->_table['bugs'];
  125. $row1 = new Zend_Db_Table_Row(
  126. array(
  127. 'db' => $this->_db,
  128. 'table' => $table,
  129. 'readOnly' => true
  130. )
  131. );
  132. $this->assertTrue($row1->isReadOnly());
  133. }
  134. public function testTableRowToArray()
  135. {
  136. $table = $this->_table['bugs'];
  137. $rowset = $table->find(1);
  138. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  139. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  140. $row1 = $rowset->current();
  141. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  142. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  143. $a = $row1->toArray();
  144. $this->assertTrue(is_array($a));
  145. // fix for #ZF-1898
  146. $arrayObject = new ArrayObject($row1->toArray(),ArrayObject::ARRAY_AS_PROPS);
  147. $arrayObject->bug_status = 'foobar';
  148. $this->assertNotEquals('foobar',$row1->bug_status);
  149. $cols = array(
  150. 'bug_id',
  151. 'bug_description',
  152. 'bug_status',
  153. 'created_on',
  154. 'updated_on',
  155. 'reported_by',
  156. 'assigned_to',
  157. 'verified_by',
  158. );
  159. $this->assertEquals($cols, array_keys($a));
  160. }
  161. public function testTableRowSelect()
  162. {
  163. $table = $this->_table['bugs'];
  164. $rowset = $table->find(1);
  165. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  166. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  167. $row1 = $rowset->current();
  168. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  169. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  170. $select = $row1->select();
  171. $this->assertType('Zend_Db_Table_Select', $select,
  172. 'Expecting object of type Zend_Db_Table_Select, got '.get_class($select));
  173. }
  174. public function testTableRowMagicGet()
  175. {
  176. $table = $this->_table['bugs'];
  177. $bug_id = $this->_db->foldCase('bug_id');
  178. $bug_description = $this->_db->foldCase('bug_description');
  179. $bug_status = $this->_db->foldCase('bug_status');
  180. $rowset = $table->find(1);
  181. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  182. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  183. $row1 = $rowset->current();
  184. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  185. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  186. try {
  187. $this->assertEquals(1, $row1->$bug_id);
  188. $this->assertEquals('System needs electricity to run', $row1->$bug_description);
  189. $this->assertEquals('NEW', $row1->$bug_status);
  190. } catch (Zend_Exception $e) {
  191. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  192. }
  193. if (!isset($row1->$bug_id)) {
  194. $this->fail('Column "id" is set but isset() returns false');
  195. }
  196. }
  197. public function testTableRowMagicUnset()
  198. {
  199. $table = $this->_table['bugs'];
  200. $row = $table->find(1)->current();
  201. unset($row->assigned_to);
  202. $this->assertFalse(isset($row->assigned_to));
  203. $diff = array_diff_key(array('assigned_to'=>''), $row->toArray());
  204. $this->assertEquals(array('assigned_to'),array_keys($diff));
  205. }
  206. public function testTableRowMagicUnsetWhenUnsettingPkValueThrowsException()
  207. {
  208. $table = $this->_table['bugs'];
  209. $row = $table->find(1)->current();
  210. $this->setExpectedException('Zend_Db_Table_Row_Exception');
  211. unset($row->bug_id);
  212. }
  213. public function testTableRowMagicSet()
  214. {
  215. $table = $this->_table['bugs'];
  216. $bug_description = $this->_db->foldCase('bug_description');
  217. $rowset = $table->find(1);
  218. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  219. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  220. $row1 = $rowset->current();
  221. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  222. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  223. try {
  224. $row1->$bug_description = 'foo';
  225. $this->assertEquals('foo', $row1->$bug_description);
  226. } catch (Zend_Exception $e) {
  227. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  228. }
  229. }
  230. // ZF-2013
  231. public function testTableRowOffsetGet()
  232. {
  233. $table = $this->_table['bugs'];
  234. $bug_id = $this->_db->foldCase('bug_id');
  235. $bug_description = $this->_db->foldCase('bug_description');
  236. $bug_status = $this->_db->foldCase('bug_status');
  237. $rowset = $table->find(1);
  238. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  239. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  240. $row1 = $rowset->current();
  241. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  242. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  243. try {
  244. $this->assertEquals(1, $row1->offsetGet($bug_id));
  245. $this->assertEquals('System needs electricity to run', $row1->offsetGet($bug_description));
  246. $this->assertEquals('NEW', $row1->offsetGet($bug_status));
  247. } catch (Zend_Exception $e) {
  248. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  249. }
  250. }
  251. // ZF-2013
  252. public function testTableRowOffsetSet()
  253. {
  254. $table = $this->_table['bugs'];
  255. $bug_description = $this->_db->foldCase('bug_description');
  256. $rowset = $table->find(1);
  257. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  258. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  259. $row1 = $rowset->current();
  260. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  261. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  262. try {
  263. $row1->offsetSet($bug_description, 'foo');
  264. $this->assertEquals('foo', $row1->offsetGet($bug_description));
  265. } catch (Zend_Exception $e) {
  266. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  267. }
  268. }
  269. /**
  270. * @group ZF-8902
  271. */
  272. public function testTableRowOffsetUnset()
  273. {
  274. $table = $this->_table['bugs'];
  275. $bug_description = $this->_db->foldCase('bug_description');
  276. $rowset = $table->find(1);
  277. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  278. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  279. $row1 = $rowset->current();
  280. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  281. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  282. try {
  283. $this->assertTrue($row1->offsetExists($bug_description));
  284. $row1->offsetUnset($bug_description);
  285. $this->assertFalse($row1->offsetExists($bug_description));
  286. } catch (Zend_Exception $e) {
  287. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  288. }
  289. }
  290. public function testTableRowSetFromArray()
  291. {
  292. $table = $this->_table['bugs'];
  293. $bug_description = $this->_db->foldCase('bug_description');
  294. $bug_status = $this->_db->foldCase('bug_status');
  295. $data = array(
  296. $bug_description => 'New Description',
  297. $bug_status => 'INVALID'
  298. );
  299. $rowset = $table->find(1);
  300. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  301. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  302. $row1 = $rowset->current();
  303. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  304. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  305. $result = $row1->setFromArray($data);
  306. $this->assertSame($result, $row1);
  307. try {
  308. $this->assertEquals($data[$bug_description], $row1->$bug_description);
  309. $this->assertEquals($data[$bug_status], $row1->$bug_status);
  310. } catch (Zend_Exception $e) {
  311. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  312. }
  313. }
  314. /**
  315. * ZF-2243: Zend_Db_Table::createRow and Zend_Db_Table_Row::setFromArray have same behaviour
  316. */
  317. public function testTableRowSetFromBigArray()
  318. {
  319. $table = $this->_table['bugs'];
  320. $bug_description = $this->_db->foldCase('bug_description');
  321. $bug_status = $this->_db->foldCase('bug_status');
  322. // Data issued from form object
  323. $data = array(
  324. $bug_description => 'New Description',
  325. $bug_status => 'INVALID',
  326. 'btnAccept' => 1 // Button value
  327. );
  328. $rowset = $table->find(1);
  329. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  330. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  331. $row1 = $rowset->current();
  332. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  333. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  334. $row1->setFromArray($data);
  335. try {
  336. $button = $row1->btnAccept;
  337. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  338. } catch (Zend_Exception $e) {
  339. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  340. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  341. $this->assertEquals("Specified column \"btnAccept\" is not in the row", $e->getMessage());
  342. }
  343. }
  344. public function testTableRowSaveInsert()
  345. {
  346. $table = $this->_table['bugs'];
  347. $data = array(
  348. 'bug_description' => 'New Description',
  349. 'bug_status' => 'INVALID'
  350. );
  351. try {
  352. $row3 = $table->createRow($data);
  353. $this->assertNull($row3->bug_id);
  354. $row3->save();
  355. $this->assertEquals(5, $row3->bug_id);
  356. $this->assertEquals($data['bug_description'], $row3->bug_description);
  357. $this->assertEquals($data['bug_status'], $row3->bug_status);
  358. } catch (Zend_Exception $e) {
  359. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  360. }
  361. }
  362. public function testTableRowSaveInsertSequence()
  363. {
  364. $table = $this->_getTable('My_ZendDbTable_TableProducts',
  365. array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
  366. $product_id = $this->_db->foldCase('product_id');
  367. $product_name = $this->_db->foldCase('product_name');
  368. $data = array (
  369. $product_name => 'Solaris'
  370. );
  371. $row3 = $table->createRow($data);
  372. $row3->save();
  373. try {
  374. $this->assertEquals(4, $row3->$product_id);
  375. } catch (Zend_Exception $e) {
  376. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  377. }
  378. }
  379. public function testTableRowSaveUpdate()
  380. {
  381. $table = $this->_table['bugs'];
  382. $bug_id = $this->_db->foldCase('bug_id');
  383. $bug_description = $this->_db->foldCase('bug_description');
  384. $bug_status = $this->_db->foldCase('bug_status');
  385. $data = array(
  386. $bug_description => 'New Description',
  387. $bug_status => 'INVALID'
  388. );
  389. $rowset = $table->find(1);
  390. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  391. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  392. $row1 = $rowset->current();
  393. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  394. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  395. $row1->setFromArray($data);
  396. $row1->save();
  397. try {
  398. $this->assertEquals(1, $row1->$bug_id);
  399. $this->assertEquals($data[$bug_description], $row1->$bug_description);
  400. $this->assertEquals($data[$bug_status], $row1->$bug_status);
  401. } catch (Zend_Exception $e) {
  402. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  403. }
  404. }
  405. public function testTableRowSaveInvalidTable()
  406. {
  407. $table = $this->_table['bugs'];
  408. $row = $this->_testTableRow();
  409. try {
  410. $row->setTableColsToFail($table);
  411. $row->setTable($table);
  412. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  413. } catch (Zend_Exception $e) {
  414. $this->assertType('Zend_Db_Table_Exception', $e,
  415. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  416. $this->assertEquals('The specified Table does not have the same columns as the Row', $e->getMessage());
  417. }
  418. $row = $this->_testTableRow();
  419. try {
  420. $row->setPrimaryKeyToFail1($table);
  421. $row->setTable($table);
  422. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  423. } catch (Zend_Exception $e) {
  424. $this->assertType('Zend_Db_Table_Exception', $e,
  425. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  426. $this->assertEquals('The specified Table \'My_ZendDbTable_TableBugs\' does not have the same primary key as the Row', $e->getMessage());
  427. }
  428. }
  429. public function testTableRowSaveUpdateInvalidInfo()
  430. {
  431. $table = $this->_table['bugs'];
  432. $row = $this->_testTableRow();
  433. $bug_status = $this->_db->foldCase('bug_status');
  434. $row->$bug_status = 'VALID';
  435. try {
  436. $row->setTable($table);
  437. $row->setPrimaryKeyToFail1();
  438. $row->save();
  439. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  440. } catch (Zend_Exception $e) {
  441. $this->assertType('Zend_Db_Table_Exception', $e,
  442. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  443. $this->assertEquals('The primary key must be set as an array', $e->getMessage());
  444. }
  445. try {
  446. $row->setTable($table);
  447. $row->setPrimaryKeyToFail2();
  448. $row->save();
  449. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  450. } catch (Zend_Exception $e) {
  451. $this->assertType('Zend_Db_Table_Exception', $e,
  452. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  453. $this->assertEquals('The specified Table \'My_ZendDbTable_TableBugs\' does not have the same primary key as the Row', $e->getMessage());
  454. }
  455. }
  456. public function testTableRowSaveUpdateRefresh()
  457. {
  458. $table = $this->_table['bugs'];
  459. $bug_status = $this->_db->foldCase('bug_status');
  460. $row = $this->_testTableRow();
  461. $row->$bug_status = 'VALID';
  462. try {
  463. $row->save();
  464. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for missing parent');
  465. } catch (Zend_Exception $e) {
  466. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  467. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  468. $this->assertEquals('Cannot refresh row as parent is missing', $e->getMessage());
  469. }
  470. }
  471. public function testTableRowSetTable()
  472. {
  473. $table = $this->_table['bugs'];
  474. $table2 = $this->_table['products'];
  475. $rowset = $table->find(1);
  476. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  477. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  478. $row1 = $rowset->current();
  479. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  480. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  481. $row1->setTable(null);
  482. $this->assertFalse($row1->isConnected());
  483. try {
  484. $row1->setTable($table2);
  485. $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect parent table');
  486. } catch (Zend_Exception $e) {
  487. $this->assertType('Zend_Db_Table_Exception', $e,
  488. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  489. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  490. }
  491. }
  492. public function testTableRowSetInvalidTable()
  493. {
  494. $table = $this->_table['bugs'];
  495. $row = $this->_testTableRow();
  496. try {
  497. $row->setTableToFail();
  498. $row->setTable($table);
  499. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  500. } catch (Zend_Exception $e) {
  501. $this->assertType('Zend_Db_Table_Exception', $e,
  502. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  503. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableBugs, expecting class to be instance of foo', $e->getMessage());
  504. }
  505. }
  506. public function testTableRowExceptionGetColumnNotInRow()
  507. {
  508. $table = $this->_table['bugs'];
  509. $rowset = $table->find(1);
  510. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  511. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  512. $row1 = $rowset->current();
  513. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  514. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  515. $column = 'doesNotExist';
  516. try {
  517. $dummy = $row1->$column;
  518. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  519. } catch (Zend_Exception $e) {
  520. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  521. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  522. $this->assertEquals("Specified column \"$column\" is not in the row", $e->getMessage());
  523. }
  524. }
  525. public function testTableRowExceptionSetColumnNotInRow()
  526. {
  527. $table = $this->_table['bugs'];
  528. $rowset = $table->find(1);
  529. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  530. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  531. $row1 = $rowset->current();
  532. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  533. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  534. $column = 'doesNotExist';
  535. try {
  536. $row1->$column = 'dummy value';
  537. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  538. } catch (Zend_Exception $e) {
  539. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  540. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  541. $this->assertEquals("Specified column \"$column\" is not in the row", $e->getMessage());
  542. }
  543. }
  544. public function testTableRowExceptionBogusPrimaryKey()
  545. {
  546. $table = $this->_table['bugs_products'];
  547. $bogusData = array(
  548. 'bug_id' => 3,
  549. 'foo' => 'bar'
  550. );
  551. $row = new Zend_Db_Table_Row(array('table' => $table, 'data' => $bogusData));
  552. try {
  553. $rowsAffected = $row->delete();
  554. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  555. } catch (Zend_Exception $e) {
  556. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  557. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  558. $this->assertEquals("The specified Table 'My_ZendDbTable_TableBugsProducts' does not have the same primary key as the Row", $e->getMessage());
  559. }
  560. }
  561. public function testTableRowSetPrimaryKey()
  562. {
  563. $table = $this->_table['bugs'];
  564. $bug_id = $this->_db->foldCase('bug_id');
  565. $rowset = $table->find(1);
  566. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  567. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  568. $row1 = $rowset->current();
  569. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  570. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  571. try {
  572. $row1->$bug_id = 6;
  573. $row1->save();
  574. $this->assertEquals(6, $row1->$bug_id);
  575. } catch (Zend_Exception $e) {
  576. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  577. }
  578. }
  579. public function testTableRowSerialize()
  580. {
  581. $table = $this->_table['bugs'];
  582. $rowset = $table->find(1);
  583. $row1 = $rowset->current();
  584. $serRow1 = serialize($row1);
  585. $row1New = unserialize($serRow1);
  586. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  587. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  588. $this->assertEquals($row1->toArray(), $row1New->toArray());
  589. }
  590. public function testTableRowSerializeExceptionNotConnected()
  591. {
  592. $table = $this->_table['bugs'];
  593. $rowset = $table->find(1);
  594. $row1 = $rowset->current();
  595. $serRow1 = serialize($row1);
  596. $row1New = unserialize($serRow1);
  597. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  598. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  599. $bug_description = $this->_db->foldCase('bug_description');
  600. $row1New->$bug_description = 'New description';
  601. try {
  602. $row1New->save();
  603. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  604. } catch (Zend_Exception $e) {
  605. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  606. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  607. $this->assertEquals("Cannot save a Row unless it is connected", $e->getMessage());
  608. }
  609. }
  610. public function testTableRowSerializeReconnectedUpdate()
  611. {
  612. $table = $this->_table['bugs'];
  613. $rowset = $table->find(1);
  614. $row1 = $rowset->current();
  615. $serRow1 = serialize($row1);
  616. $row1New = unserialize($serRow1);
  617. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  618. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  619. try {
  620. $connected = $row1New->setTable($table);
  621. } catch (Zend_Exception $e) {
  622. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  623. }
  624. $this->assertTrue($connected);
  625. $bug_description = $this->_db->foldCase('bug_description');
  626. $bug_status = $this->_db->foldCase('bug_status');
  627. $data = array(
  628. $bug_description => 'New Description',
  629. $bug_status => 'INVALID'
  630. );
  631. $row1New->setFromArray($data);
  632. try {
  633. $rowsAffected = $row1New->save();
  634. } catch (Zend_Exception $e) {
  635. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  636. }
  637. $this->assertEquals(1, $rowsAffected);
  638. }
  639. public function testTableRowSerializeReconnectedDelete()
  640. {
  641. $table = $this->_table['bugs'];
  642. $rowset = $table->find(1);
  643. $row1 = $rowset->current();
  644. $serRow1 = serialize($row1);
  645. $row1New = unserialize($serRow1);
  646. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  647. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  648. try {
  649. $connected = $row1New->setTable($table);
  650. } catch (Zend_Exception $e) {
  651. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  652. }
  653. $this->assertTrue($connected);
  654. try {
  655. $rowsAffected = $row1New->delete();
  656. } catch (Zend_Exception $e) {
  657. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  658. }
  659. $this->assertEquals(1, $rowsAffected);
  660. }
  661. public function testTableRowSerializeExceptionWrongTable()
  662. {
  663. $table = $this->_table['bugs'];
  664. $rowset = $table->find(1);
  665. $row1 = $rowset->current();
  666. $serRow1 = serialize($row1);
  667. $row1New = unserialize($serRow1);
  668. $this->assertType('Zend_Db_Table_Row_Abstract', $row1New,
  669. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  670. $table2 = $this->_table['products'];
  671. $connected = false;
  672. try {
  673. $connected = $row1New->setTable($table2);
  674. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  675. } catch (Zend_Exception $e) {
  676. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  677. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  678. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  679. }
  680. $this->assertFalse($connected);
  681. }
  682. public function testTableRowSetReadOnly()
  683. {
  684. $table = $this->_testTableRowSetReadOnlyGetTableBugs();
  685. $bug_status = $this->_db->foldCase('bug_status');
  686. $rowset = $table->find(1);
  687. $row1 = $rowset->current();
  688. $row1->setReadOnly(true);
  689. $this->assertTrue($row1->isReadOnly());
  690. $data = array(
  691. 'bug_description' => 'New Description',
  692. 'bug_status' => 'INVALID'
  693. );
  694. $row2 = $table->createRow($data);
  695. $row2->setReadOnly(true);
  696. try {
  697. $row2->save();
  698. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  699. } catch (Zend_Exception $e) {
  700. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  701. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  702. $this->assertEquals('This row has been marked read-only', $e->getMessage());
  703. }
  704. $row2->setReadOnly(false);
  705. $row2->save();
  706. $row2->$bug_status = 'VALID';
  707. $row2->setReadOnly(true);
  708. try {
  709. $row2->save();
  710. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  711. } catch (Zend_Exception $e) {
  712. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  713. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  714. $this->assertEquals('This row has been marked read-only', $e->getMessage());
  715. }
  716. $row2->setReadOnly(false);
  717. $row2->save();
  718. }
  719. public function testTableRowInvalidTransformColumn()
  720. {
  721. $row = $this->_testTableRow();
  722. try {
  723. $row->setInvalidColumn();
  724. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for invalid column type');
  725. } catch (Zend_Exception $e) {
  726. $this->assertType('Zend_Db_Table_Exception', $e,
  727. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  728. $this->assertEquals('Specified column is not a string', $e->getMessage());
  729. }
  730. }
  731. /**
  732. * @group ZF-9836
  733. */
  734. public function testTableRowIsIterable()
  735. {
  736. $table = $this->_table['bugs'];
  737. $rowset = $table->find(1);
  738. $row = $rowset->current();
  739. $this->assertTrue($row instanceof Traversable);
  740. $this->assertTrue($row instanceof IteratorAggregate);
  741. $this->assertType('ArrayIterator', $row->getIterator());
  742. $count=0;
  743. foreach ($row as $columnValue) {
  744. $count++;
  745. }
  746. $this->assertEquals(8, $count, 'The row was iterated, there should be 8 columns iterated');
  747. }
  748. /**
  749. * Utility methods below
  750. */
  751. /**
  752. * Allow adapters with sequences to declare them
  753. * @return Zend_Db_Table_Abstract
  754. */
  755. protected function _testTableRowSetReadOnlyGetTableBugs()
  756. {
  757. return $this->_table['bugs'];
  758. }
  759. }