TestCommon.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Table_TestSetup
  24. */
  25. require_once 'Zend/Db/Table/TestSetup.php';
  26. require_once 'Zend/Db/Table/Row.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Db
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Db
  34. * @group Zend_Db_Table
  35. * @group Zend_Db_Table_Row
  36. */
  37. abstract class Zend_Db_Table_Row_TestCommon extends Zend_Db_Table_TestSetup
  38. {
  39. protected function _testTableRow()
  40. {
  41. $table = $this->_table['bugs'];
  42. $bug_id = $this->_db->foldCase('bug_id');
  43. $bug_description = $this->_db->foldCase('bug_description');
  44. $bug_status = $this->_db->foldCase('bug_status');
  45. $created_on = $this->_db->foldCase('created_on');
  46. $updated_on = $this->_db->foldCase('updated_on');
  47. $reported_by = $this->_db->foldCase('reported_by');
  48. $assigned_to = $this->_db->foldCase('assigned_to');
  49. $verified_by = $this->_db->foldCase('verified_by');
  50. $data = array(
  51. $bug_id => '1',
  52. $bug_description => 'System needs electricity to run',
  53. $bug_status => 'NEW',
  54. $created_on => '2007-04-01',
  55. $updated_on => '2007-04-01',
  56. $reported_by => 'goofy',
  57. $assigned_to => 'mmouse',
  58. $verified_by => 'dduck'
  59. );
  60. $config = array(
  61. 'table' => $table,
  62. 'stored' => true,
  63. 'readOnly' => false,
  64. 'data' => $data,
  65. );
  66. $this->_useMyIncludePath();
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  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->assertTrue($row->getTable() instanceof Zend_Db_Table_Abstract);
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  138. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  139. $row1 = $rowset->current();
  140. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  165. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  166. $row1 = $rowset->current();
  167. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  168. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  169. $select = $row1->select();
  170. $this->assertTrue($select instanceof Zend_Db_Table_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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  181. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  182. $row1 = $rowset->current();
  183. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  218. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  219. $row1 = $rowset->current();
  220. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  238. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  239. $row1 = $rowset->current();
  240. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  257. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  258. $row1 = $rowset->current();
  259. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  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. /**
  269. * @group ZF-8902
  270. */
  271. public function testTableRowOffsetUnset()
  272. {
  273. $table = $this->_table['bugs'];
  274. $bug_description = $this->_db->foldCase('bug_description');
  275. $rowset = $table->find(1);
  276. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  277. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  278. $row1 = $rowset->current();
  279. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  280. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  281. try {
  282. $this->assertTrue($row1->offsetExists($bug_description));
  283. $row1->offsetUnset($bug_description);
  284. $this->assertFalse($row1->offsetExists($bug_description));
  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. public function testTableRowSetFromArray()
  290. {
  291. $table = $this->_table['bugs'];
  292. $bug_description = $this->_db->foldCase('bug_description');
  293. $bug_status = $this->_db->foldCase('bug_status');
  294. $data = array(
  295. $bug_description => 'New Description',
  296. $bug_status => 'INVALID'
  297. );
  298. $rowset = $table->find(1);
  299. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  300. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  301. $row1 = $rowset->current();
  302. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  303. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  304. $result = $row1->setFromArray($data);
  305. $this->assertSame($result, $row1);
  306. try {
  307. $this->assertEquals($data[$bug_description], $row1->$bug_description);
  308. $this->assertEquals($data[$bug_status], $row1->$bug_status);
  309. } catch (Zend_Exception $e) {
  310. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  311. }
  312. }
  313. /**
  314. * ZF-2243: Zend_Db_Table::createRow and Zend_Db_Table_Row::setFromArray have same behaviour
  315. */
  316. public function testTableRowSetFromBigArray()
  317. {
  318. $table = $this->_table['bugs'];
  319. $bug_description = $this->_db->foldCase('bug_description');
  320. $bug_status = $this->_db->foldCase('bug_status');
  321. // Data issued from form object
  322. $data = array(
  323. $bug_description => 'New Description',
  324. $bug_status => 'INVALID',
  325. 'btnAccept' => 1 // Button value
  326. );
  327. $rowset = $table->find(1);
  328. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  329. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  330. $row1 = $rowset->current();
  331. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  332. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  333. $row1->setFromArray($data);
  334. try {
  335. $button = $row1->btnAccept;
  336. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  337. } catch (Zend_Exception $e) {
  338. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  339. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  340. $this->assertEquals("Specified column \"btnAccept\" is not in the row", $e->getMessage());
  341. }
  342. }
  343. public function testTableRowSaveInsert()
  344. {
  345. $table = $this->_table['bugs'];
  346. $data = array(
  347. 'bug_description' => 'New Description',
  348. 'bug_status' => 'INVALID'
  349. );
  350. try {
  351. $row3 = $table->createRow($data);
  352. $this->assertNull($row3->bug_id);
  353. $row3->save();
  354. $this->assertEquals(5, $row3->bug_id);
  355. $this->assertEquals($data['bug_description'], $row3->bug_description);
  356. $this->assertEquals($data['bug_status'], $row3->bug_status);
  357. } catch (Zend_Exception $e) {
  358. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  359. }
  360. }
  361. public function testTableRowSaveInsertSequence()
  362. {
  363. $table = $this->_getTable('My_ZendDbTable_TableProducts',
  364. array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
  365. $product_id = $this->_db->foldCase('product_id');
  366. $product_name = $this->_db->foldCase('product_name');
  367. $data = array (
  368. $product_name => 'Solaris'
  369. );
  370. $row3 = $table->createRow($data);
  371. $row3->save();
  372. try {
  373. $this->assertEquals(4, $row3->$product_id);
  374. } catch (Zend_Exception $e) {
  375. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  376. }
  377. }
  378. public function testTableRowSaveUpdate()
  379. {
  380. $table = $this->_table['bugs'];
  381. $bug_id = $this->_db->foldCase('bug_id');
  382. $bug_description = $this->_db->foldCase('bug_description');
  383. $bug_status = $this->_db->foldCase('bug_status');
  384. $data = array(
  385. $bug_description => 'New Description',
  386. $bug_status => 'INVALID'
  387. );
  388. $rowset = $table->find(1);
  389. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  390. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  391. $row1 = $rowset->current();
  392. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  393. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  394. $row1->setFromArray($data);
  395. $row1->save();
  396. try {
  397. $this->assertEquals(1, $row1->$bug_id);
  398. $this->assertEquals($data[$bug_description], $row1->$bug_description);
  399. $this->assertEquals($data[$bug_status], $row1->$bug_status);
  400. } catch (Zend_Exception $e) {
  401. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  402. }
  403. }
  404. public function testTableRowSaveInvalidTable()
  405. {
  406. $table = $this->_table['bugs'];
  407. $row = $this->_testTableRow();
  408. try {
  409. $row->setTableColsToFail($table);
  410. $row->setTable($table);
  411. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  412. } catch (Zend_Exception $e) {
  413. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  414. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  415. $this->assertEquals('The specified Table does not have the same columns as the Row', $e->getMessage());
  416. }
  417. $row = $this->_testTableRow();
  418. try {
  419. $row->setPrimaryKeyToFail1($table);
  420. $row->setTable($table);
  421. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  422. } catch (Zend_Exception $e) {
  423. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  424. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  425. $this->assertEquals('The specified Table \'My_ZendDbTable_TableBugs\' does not have the same primary key as the Row', $e->getMessage());
  426. }
  427. }
  428. public function testTableRowSaveUpdateInvalidInfo()
  429. {
  430. $table = $this->_table['bugs'];
  431. $row = $this->_testTableRow();
  432. $bug_status = $this->_db->foldCase('bug_status');
  433. $row->$bug_status = 'VALID';
  434. try {
  435. $row->setTable($table);
  436. $row->setPrimaryKeyToFail1();
  437. $row->save();
  438. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  439. } catch (Zend_Exception $e) {
  440. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  441. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  442. $this->assertEquals('The primary key must be set as an array', $e->getMessage());
  443. }
  444. try {
  445. $row->setTable($table);
  446. $row->setPrimaryKeyToFail2();
  447. $row->save();
  448. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  449. } catch (Zend_Exception $e) {
  450. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  451. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  452. $this->assertEquals('The specified Table \'My_ZendDbTable_TableBugs\' does not have the same primary key as the Row', $e->getMessage());
  453. }
  454. }
  455. public function testTableRowSaveUpdateRefresh()
  456. {
  457. $table = $this->_table['bugs'];
  458. $bug_status = $this->_db->foldCase('bug_status');
  459. $row = $this->_testTableRow();
  460. $row->$bug_status = 'VALID';
  461. try {
  462. $row->save();
  463. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for missing parent');
  464. } catch (Zend_Exception $e) {
  465. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  466. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  467. $this->assertEquals('Cannot refresh row as parent is missing', $e->getMessage());
  468. }
  469. }
  470. public function testTableRowSetTable()
  471. {
  472. $table = $this->_table['bugs'];
  473. $table2 = $this->_table['products'];
  474. $rowset = $table->find(1);
  475. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  476. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  477. $row1 = $rowset->current();
  478. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  479. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  480. $row1->setTable(null);
  481. $this->assertFalse($row1->isConnected());
  482. try {
  483. $row1->setTable($table2);
  484. $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect parent table');
  485. } catch (Zend_Exception $e) {
  486. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  487. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  488. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  489. }
  490. }
  491. public function testTableRowSetInvalidTable()
  492. {
  493. $table = $this->_table['bugs'];
  494. $row = $this->_testTableRow();
  495. try {
  496. $row->setTableToFail();
  497. $row->setTable($table);
  498. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for incorrect parent table');
  499. } catch (Zend_Exception $e) {
  500. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  501. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  502. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableBugs, expecting class to be instance of foo', $e->getMessage());
  503. }
  504. }
  505. public function testTableRowExceptionGetColumnNotInRow()
  506. {
  507. $table = $this->_table['bugs'];
  508. $rowset = $table->find(1);
  509. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  510. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  511. $row1 = $rowset->current();
  512. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  513. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  514. $column = 'doesNotExist';
  515. try {
  516. $dummy = $row1->$column;
  517. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  518. } catch (Zend_Exception $e) {
  519. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  520. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  521. $this->assertEquals("Specified column \"$column\" is not in the row", $e->getMessage());
  522. }
  523. }
  524. public function testTableRowExceptionSetColumnNotInRow()
  525. {
  526. $table = $this->_table['bugs'];
  527. $rowset = $table->find(1);
  528. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  529. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  530. $row1 = $rowset->current();
  531. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  532. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  533. $column = 'doesNotExist';
  534. try {
  535. $row1->$column = 'dummy value';
  536. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  537. } catch (Zend_Exception $e) {
  538. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  539. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  540. $this->assertEquals("Specified column \"$column\" is not in the row", $e->getMessage());
  541. }
  542. }
  543. public function testTableRowExceptionBogusPrimaryKey()
  544. {
  545. $table = $this->_table['bugs_products'];
  546. $bogusData = array(
  547. 'bug_id' => 3,
  548. 'foo' => 'bar'
  549. );
  550. $row = new Zend_Db_Table_Row(array('table' => $table, 'data' => $bogusData));
  551. try {
  552. $rowsAffected = $row->delete();
  553. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  554. } catch (Zend_Exception $e) {
  555. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  556. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  557. $this->assertEquals("The specified Table 'My_ZendDbTable_TableBugsProducts' does not have the same primary key as the Row", $e->getMessage());
  558. }
  559. }
  560. public function testTableRowSetPrimaryKey()
  561. {
  562. $table = $this->_table['bugs'];
  563. $bug_id = $this->_db->foldCase('bug_id');
  564. $rowset = $table->find(1);
  565. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  566. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  567. $row1 = $rowset->current();
  568. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  569. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  570. try {
  571. $row1->$bug_id = 6;
  572. $row1->save();
  573. $this->assertEquals(6, $row1->$bug_id);
  574. } catch (Zend_Exception $e) {
  575. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  576. }
  577. }
  578. public function testTableRowSerialize()
  579. {
  580. $table = $this->_table['bugs'];
  581. $rowset = $table->find(1);
  582. $row1 = $rowset->current();
  583. $serRow1 = serialize($row1);
  584. $row1New = unserialize($serRow1);
  585. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  586. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  587. $this->assertEquals($row1->toArray(), $row1New->toArray());
  588. }
  589. public function testTableRowSerializeExceptionNotConnected()
  590. {
  591. $table = $this->_table['bugs'];
  592. $rowset = $table->find(1);
  593. $row1 = $rowset->current();
  594. $serRow1 = serialize($row1);
  595. $row1New = unserialize($serRow1);
  596. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  597. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  598. $bug_description = $this->_db->foldCase('bug_description');
  599. $row1New->$bug_description = 'New description';
  600. try {
  601. $row1New->save();
  602. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  603. } catch (Zend_Exception $e) {
  604. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  605. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  606. $this->assertEquals("Cannot save a Row unless it is connected", $e->getMessage());
  607. }
  608. }
  609. public function testTableRowSerializeReconnectedUpdate()
  610. {
  611. $table = $this->_table['bugs'];
  612. $rowset = $table->find(1);
  613. $row1 = $rowset->current();
  614. $serRow1 = serialize($row1);
  615. $row1New = unserialize($serRow1);
  616. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  617. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  618. try {
  619. $connected = $row1New->setTable($table);
  620. } catch (Zend_Exception $e) {
  621. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  622. }
  623. $this->assertTrue($connected);
  624. $bug_description = $this->_db->foldCase('bug_description');
  625. $bug_status = $this->_db->foldCase('bug_status');
  626. $data = array(
  627. $bug_description => 'New Description',
  628. $bug_status => 'INVALID'
  629. );
  630. $row1New->setFromArray($data);
  631. try {
  632. $rowsAffected = $row1New->save();
  633. } catch (Zend_Exception $e) {
  634. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  635. }
  636. $this->assertEquals(1, $rowsAffected);
  637. }
  638. public function testTableRowSerializeReconnectedDelete()
  639. {
  640. $table = $this->_table['bugs'];
  641. $rowset = $table->find(1);
  642. $row1 = $rowset->current();
  643. $serRow1 = serialize($row1);
  644. $row1New = unserialize($serRow1);
  645. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  646. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  647. try {
  648. $connected = $row1New->setTable($table);
  649. } catch (Zend_Exception $e) {
  650. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  651. }
  652. $this->assertTrue($connected);
  653. try {
  654. $rowsAffected = $row1New->delete();
  655. } catch (Zend_Exception $e) {
  656. $this->fail("Caught exception of type \"".get_class($e)."\" where no exception was expected. Exception message: \"".$e->getMessage()."\"\n");
  657. }
  658. $this->assertEquals(1, $rowsAffected);
  659. }
  660. public function testTableRowSerializeExceptionWrongTable()
  661. {
  662. $table = $this->_table['bugs'];
  663. $rowset = $table->find(1);
  664. $row1 = $rowset->current();
  665. $serRow1 = serialize($row1);
  666. $row1New = unserialize($serRow1);
  667. $this->assertTrue($row1New instanceof Zend_Db_Table_Row_Abstract,
  668. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1New));
  669. $table2 = $this->_table['products'];
  670. $connected = false;
  671. try {
  672. $connected = $row1New->setTable($table2);
  673. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  674. } catch (Zend_Exception $e) {
  675. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  676. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  677. $this->assertEquals('The specified Table is of class My_ZendDbTable_TableProducts, expecting class to be instance of My_ZendDbTable_TableBugs', $e->getMessage());
  678. }
  679. $this->assertFalse($connected);
  680. }
  681. public function testTableRowSetReadOnly()
  682. {
  683. $table = $this->_testTableRowSetReadOnlyGetTableBugs();
  684. $bug_status = $this->_db->foldCase('bug_status');
  685. $rowset = $table->find(1);
  686. $row1 = $rowset->current();
  687. $row1->setReadOnly(true);
  688. $this->assertTrue($row1->isReadOnly());
  689. $data = array(
  690. 'bug_description' => 'New Description',
  691. 'bug_status' => 'INVALID'
  692. );
  693. $row2 = $table->createRow($data);
  694. $row2->setReadOnly(true);
  695. try {
  696. $row2->save();
  697. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  698. } catch (Zend_Exception $e) {
  699. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  700. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  701. $this->assertEquals('This row has been marked read-only', $e->getMessage());
  702. }
  703. $row2->setReadOnly(false);
  704. $row2->save();
  705. $row2->$bug_status = 'VALID';
  706. $row2->setReadOnly(true);
  707. try {
  708. $row2->save();
  709. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  710. } catch (Zend_Exception $e) {
  711. $this->assertTrue($e instanceof Zend_Db_Table_Row_Exception,
  712. 'Expecting object of type Zend_Db_Table_Row_Exception, got '.get_class($e));
  713. $this->assertEquals('This row has been marked read-only', $e->getMessage());
  714. }
  715. $row2->setReadOnly(false);
  716. $row2->save();
  717. }
  718. public function testTableRowInvalidTransformColumn()
  719. {
  720. $row = $this->_testTableRow();
  721. try {
  722. $row->setInvalidColumn();
  723. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for invalid column type');
  724. } catch (Zend_Exception $e) {
  725. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  726. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  727. $this->assertEquals('Specified column is not a string', $e->getMessage());
  728. }
  729. }
  730. /**
  731. * @group ZF-9836
  732. */
  733. public function testTableRowIsIterable()
  734. {
  735. $table = $this->_table['bugs'];
  736. $rowset = $table->find(1);
  737. $row = $rowset->current();
  738. $this->assertTrue($row instanceof Traversable);
  739. $this->assertTrue($row instanceof IteratorAggregate);
  740. $this->assertTrue($row->getIterator() instanceof ArrayIterator);
  741. $count=0;
  742. foreach ($row as $columnValue) {
  743. $count++;
  744. }
  745. $this->assertEquals(8, $count, 'The row was iterated, there should be 8 columns iterated');
  746. }
  747. /**
  748. * Utility methods below
  749. */
  750. /**
  751. * Allow adapters with sequences to declare them
  752. * @return Zend_Db_Table_Abstract
  753. */
  754. protected function _testTableRowSetReadOnlyGetTableBugs()
  755. {
  756. return $this->_table['bugs'];
  757. }
  758. }