2
0

TestCommon.php 32 KB

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