TestCommon.php 32 KB

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