TestCommon.php 32 KB

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