TestCommon.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. /**
  27. * @see Zend_Loader
  28. */
  29. require_once 'Zend/Loader.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. abstract class Zend_Db_Table_Relationships_TestCommon extends Zend_Db_Table_TestSetup
  39. {
  40. public function testTableRelationshipFindParentRow()
  41. {
  42. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  43. $account_name = $this->_db->foldCase('account_name');
  44. $table = $this->_table['bugs'];
  45. $childRows = $table->fetchAll("$bug_id = 1");
  46. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  47. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  48. $childRow1 = $childRows->current();
  49. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  50. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  51. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
  52. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  53. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  54. $this->assertEquals('goofy', $parentRow->$account_name);
  55. }
  56. public function testTableRelationshipFindParentRowSelect()
  57. {
  58. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  59. $account_name = $this->_db->foldCase('account_name');
  60. $account_name_column = $this->_db->quoteIdentifier('account_name', true);
  61. $table = $this->_table['bugs'];
  62. $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
  63. $childRows = $table->fetchAll("$bug_id = 1");
  64. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  65. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  66. $childRow1 = $childRows->current();
  67. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  68. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  69. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts', null, $select);
  70. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  71. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  72. $this->assertEquals('goofy', $parentRow->$account_name);
  73. }
  74. public function testTableRelationshipMagicFindParentRow()
  75. {
  76. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  77. $account_name = $this->_db->foldCase('account_name');
  78. $table = $this->_table['bugs'];
  79. $childRows = $table->fetchAll("$bug_id = 1");
  80. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  81. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  82. $childRow1 = $childRows->current();
  83. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  84. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  85. $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts();
  86. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  87. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  88. $this->assertEquals('goofy', $parentRow->$account_name);
  89. }
  90. public function testTableRelationshipMagicFindParentRowSelect()
  91. {
  92. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  93. $account_name = $this->_db->foldCase('account_name');
  94. $account_name_column = $this->_db->quoteIdentifier('account_name', true);
  95. $table = $this->_table['bugs'];
  96. $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
  97. $childRows = $table->fetchAll("$bug_id = 1");
  98. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  99. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  100. $childRow1 = $childRows->current();
  101. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  102. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  103. $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts($select);
  104. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  105. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  106. $this->assertEquals('goofy', $parentRow->$account_name);
  107. }
  108. public function testTableRelationshipMagicException()
  109. {
  110. $table = $this->_table['bugs'];
  111. $parentRows = $table->find(1);
  112. $parentRow1 = $parentRows->current();
  113. // Completely bogus method
  114. try {
  115. $result = $parentRow1->nonExistantMethod();
  116. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  117. } catch (Zend_Exception $e) {
  118. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  119. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  120. $this->assertEquals("Unrecognized method 'nonExistantMethod()'", $e->getMessage());
  121. }
  122. }
  123. /**
  124. * @expectedException PHPUnit_Framework_Error
  125. */
  126. public function testTableRelationshipFindParentRowException()
  127. {
  128. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  129. $table = $this->_table['bugs'];
  130. $childRows = $table->fetchAll("$bug_id = 1");
  131. $childRow1 = $childRows->current();
  132. try {
  133. $parentRow = $childRow1->findParentRow('nonexistant_class');
  134. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  135. } catch (Zend_Exception $e) {
  136. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  137. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  138. $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
  139. }
  140. try {
  141. $parentRow = $childRow1->findParentRow(new stdClass());
  142. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for wrong table class');
  143. } catch (Zend_Exception $e) {
  144. $this->assertType('Zend_Db_Table_Exception', $e,
  145. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  146. $this->assertEquals('Parent table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
  147. }
  148. }
  149. /**
  150. * @expectedException PHPUnit_Framework_Error
  151. */
  152. public function testTableRelationshipFindManyToManyRowset()
  153. {
  154. $table = $this->_table['bugs'];
  155. $originRows = $table->find(1);
  156. $originRow1 = $originRows->current();
  157. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
  158. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  159. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  160. $this->assertEquals(3, $destRows->count());
  161. }
  162. /**
  163. * @expectedException PHPUnit_Framework_Error
  164. */
  165. public function testTableRelationshipFindManyToManyRowsetSelect()
  166. {
  167. $product_name = $this->_db->foldCase('product_name');
  168. $bug_id = $this->_db->foldCase('bug_id');
  169. $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
  170. $table = $this->_table['bugs'];
  171. $select = $table->select()->where($bug_id_column . ' = ?', 1)
  172. ->limit(2)
  173. ->order($product_name . ' ASC');
  174. $originRows = $table->find(1);
  175. $originRow1 = $originRows->current();
  176. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts',
  177. null, null, $select);
  178. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  179. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  180. $this->assertEquals(2, $destRows->count());
  181. $childRow = $destRows->current();
  182. $this->assertEquals('Linux', $childRow->$product_name);
  183. }
  184. public function testTableRelationshipMagicFindManyToManyRowset()
  185. {
  186. $table = $this->_table['bugs'];
  187. $originRows = $table->find(1);
  188. $originRow1 = $originRows->current();
  189. $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts();
  190. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  191. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  192. $this->assertEquals(3, $destRows->count());
  193. }
  194. public function testTableRelationshipMagicFindManyToManyRowsetSelect()
  195. {
  196. $product_name = $this->_db->foldCase('product_name');
  197. $bug_id = $this->_db->foldCase('bug_id');
  198. $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
  199. $table = $this->_table['bugs'];
  200. $select = $table->select()->where($bug_id_column . ' = ?', 1)
  201. ->limit(2)
  202. ->order($product_name . ' ASC');
  203. $originRows = $table->find(1);
  204. $originRow1 = $originRows->current();
  205. $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts($select);
  206. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  207. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  208. $this->assertEquals(2, $destRows->count());
  209. $childRow = $destRows->current();
  210. $this->assertEquals('Linux', $childRow->$product_name);
  211. }
  212. public function testTableRelationshipFindManyToManyRowsetException()
  213. {
  214. $table = $this->_table['bugs'];
  215. $originRows = $table->find(1);
  216. $originRow1 = $originRows->current();
  217. // Use nonexistant class for destination table
  218. try {
  219. $destRows = $originRow1->findManyToManyRowset('nonexistant_class', 'My_ZendDbTable_TableBugsProducts');
  220. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  221. } catch (Zend_Exception $e) {
  222. $this->assertType('Zend_Db_Table_Exception', $e,
  223. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  224. $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
  225. }
  226. // Use stdClass instead of table class for destination table
  227. try {
  228. $destRows = $originRow1->findManyToManyRowset(new stdClass(), 'My_ZendDbTable_TableBugsProducts');
  229. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  230. } catch (Zend_Exception $e) {
  231. $this->assertType('Zend_Db_Table_Exception', $e,
  232. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  233. $this->assertEquals('Match table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
  234. }
  235. // Use nonexistant class for intersection table
  236. try {
  237. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'nonexistant_class');
  238. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  239. } catch (Zend_Exception $e) {
  240. $this->assertType('Zend_Db_Table_Exception', $e,
  241. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  242. $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
  243. }
  244. // Use stdClass instead of table class for intersection table
  245. try {
  246. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', new stdClass());
  247. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  248. } catch (Zend_Exception $e) {
  249. $this->assertType('Zend_Db_Table_Exception', $e,
  250. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  251. $this->assertEquals('Intersection table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
  252. }
  253. }
  254. public function testTableRelationshipFindDependentRowset()
  255. {
  256. $table = $this->_table['bugs'];
  257. $bug_id = $this->_db->foldCase('bug_id');
  258. $product_id = $this->_db->foldCase('product_id');
  259. $parentRows = $table->find(1);
  260. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  261. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  262. $parentRow1 = $parentRows->current();
  263. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts');
  264. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  265. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  266. $this->assertEquals(3, $childRows->count());
  267. $childRow1 = $childRows->current();
  268. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  269. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  270. $this->assertEquals(1, $childRow1->$bug_id);
  271. $this->assertEquals(1, $childRow1->$product_id);
  272. }
  273. public function testTableRelationshipFindDependentRowsetSelect()
  274. {
  275. $table = $this->_table['bugs'];
  276. $bug_id = $this->_db->foldCase('bug_id');
  277. $product_id = $this->_db->foldCase('product_id');
  278. $select = $table->select()->limit(2)
  279. ->order($product_id . ' DESC');
  280. $parentRows = $table->find(1);
  281. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  282. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  283. $parentRow1 = $parentRows->current();
  284. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts', null, $select);
  285. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  286. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  287. $childRow1 = $childRows->current();
  288. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  289. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  290. $this->assertEquals(1, $childRow1->$bug_id);
  291. $this->assertEquals(3, $childRow1->$product_id);
  292. }
  293. public function testTableRelationshipMagicFindDependentRowset()
  294. {
  295. $table = $this->_table['bugs'];
  296. $bug_id = $this->_db->foldCase('bug_id');
  297. $product_id = $this->_db->foldCase('product_id');
  298. $parentRows = $table->find(1);
  299. $parentRow1 = $parentRows->current();
  300. $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts();
  301. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  302. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  303. $this->assertEquals(3, $childRows->count());
  304. $childRow1 = $childRows->current();
  305. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  306. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  307. $this->assertEquals(1, $childRow1->$bug_id);
  308. $this->assertEquals(1, $childRow1->$product_id);
  309. }
  310. public function testTableRelationshipMagicFindDependentRowsetSelect()
  311. {
  312. $table = $this->_table['bugs'];
  313. $bug_id = $this->_db->foldCase('bug_id');
  314. $product_id = $this->_db->foldCase('product_id');
  315. $select = $table->select()->limit(2)
  316. ->order($product_id . ' DESC');
  317. $parentRows = $table->find(1);
  318. $parentRow1 = $parentRows->current();
  319. $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts($select);
  320. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  321. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  322. $this->assertEquals(2, $childRows->count());
  323. $childRow1 = $childRows->current();
  324. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  325. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  326. $this->assertEquals(1, $childRow1->$bug_id);
  327. $this->assertEquals(3, $childRow1->$product_id);
  328. }
  329. public function testTableRelationshipFindDependentRowsetException()
  330. {
  331. $table = $this->_table['bugs'];
  332. $parentRows = $table->find(1);
  333. $parentRow1 = $parentRows->current();
  334. try {
  335. $childRows = $parentRow1->findDependentRowset('nonexistant_class');
  336. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
  337. } catch (Zend_Exception $e) {
  338. $this->assertType('Zend_Db_Table_Exception', $e,
  339. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  340. $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
  341. }
  342. try {
  343. $childRows = $parentRow1->findDependentRowset(new stdClass());
  344. $this->fail('Expected to catch Zend_Db_Table_Row_Exception for wrong table class');
  345. } catch (Zend_Exception $e) {
  346. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  347. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  348. $this->assertEquals('Dependent table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
  349. }
  350. }
  351. /**
  352. * Ensures that basic cascading update functionality succeeds using strings for single columns
  353. *
  354. * @return void
  355. */
  356. public function testTableRelationshipCascadingUpdateUsageBasicString()
  357. {
  358. $bug = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  359. ->find(1)
  360. ->current();
  361. $bug_id = $this->_db->foldCase('bug_id');
  362. $this->assertEquals(
  363. 3,
  364. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  365. 'Expecting to find three dependent rows'
  366. );
  367. $bug->$bug_id = 333;
  368. $bug->save();
  369. $this->assertEquals(
  370. 3,
  371. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  372. 'Expecting to find three dependent rows'
  373. );
  374. foreach ($bugProducts as $bugProduct) {
  375. $this->assertEquals(333, $bugProduct->$bug_id);
  376. }
  377. $bug->$bug_id = 1;
  378. $bug->save();
  379. $this->assertEquals(
  380. 3,
  381. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  382. 'Expecting to find three dependent rows'
  383. );
  384. foreach ($bugProducts as $bugProduct) {
  385. $this->assertEquals(1, $bugProduct->$bug_id);
  386. }
  387. }
  388. /**
  389. * Ensures that basic cascading update functionality succeeds using arrays for single columns
  390. *
  391. * @return void
  392. */
  393. public function testTableRelationshipCascadingUpdateUsageBasicArray()
  394. {
  395. $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  396. ->find('mmouse')
  397. ->current();
  398. $account_name = $this->_db->foldCase('account_name');
  399. $reported_by = $this->_db->foldCase('reported_by');
  400. $this->assertEquals(
  401. 1,
  402. count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  403. 'Expecting to find one dependent row'
  404. );
  405. $account1->$account_name = 'daisy';
  406. $account1->save();
  407. $this->assertEquals(
  408. 1,
  409. count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  410. 'Expecting to find one dependent row'
  411. );
  412. foreach ($account1Bugs as $account1Bug) {
  413. $this->assertEquals('daisy', $account1Bug->$reported_by);
  414. }
  415. $account1->$account_name = 'mmouse';
  416. $account1->save();
  417. $this->assertEquals(
  418. 1,
  419. count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  420. 'Expecting to find one dependent row'
  421. );
  422. foreach ($account1Bugs as $account1Bug) {
  423. $this->assertEquals('mmouse', $account1Bug->$reported_by);
  424. }
  425. }
  426. /**
  427. * Ensures that cascading update functionality is not run when onUpdate != self::CASCADE
  428. *
  429. * @return void
  430. */
  431. public function testTableRelationshipCascadingUpdateUsageInvalidNoop()
  432. {
  433. $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
  434. ->find(1)
  435. ->current();
  436. $this->assertEquals(
  437. 1,
  438. count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  439. 'Expecting to find one dependent row'
  440. );
  441. $product_id = $this->_db->foldCase('product_id');
  442. $product1->$product_id = 333;
  443. $product1->save();
  444. $this->assertEquals(
  445. 0,
  446. count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  447. 'Expecting to find one dependent row'
  448. );
  449. $product1->$product_id = 1;
  450. $product1->save();
  451. $this->assertEquals(
  452. 1,
  453. count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  454. 'Expecting to find one dependent row'
  455. );
  456. foreach ($product1BugsProducts as $product1BugsProduct) {
  457. $this->assertEquals(1, $product1BugsProduct->$product_id);
  458. }
  459. }
  460. /**
  461. * Ensures that basic cascading delete functionality succeeds using strings for single columns
  462. *
  463. * @return void
  464. */
  465. public function testTableRelationshipCascadingDeleteUsageBasicString()
  466. {
  467. $bug1 = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  468. ->find(1)
  469. ->current();
  470. $this->assertEquals(
  471. 3,
  472. count($bug1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  473. 'Expecting to find three dependent rows'
  474. );
  475. $bug1->delete();
  476. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  477. $this->assertEquals(
  478. 0,
  479. count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$bug_id = 1")),
  480. 'Expecting cascading delete to have reduced dependent rows to zero'
  481. );
  482. }
  483. /**
  484. * Ensures that basic cascading delete functionality succeeds using arrays for single columns
  485. *
  486. * @return void
  487. */
  488. public function testTableRelationshipCascadingDeleteUsageBasicArray()
  489. {
  490. $reported_by = $this->_db->quoteIdentifier('reported_by', true);
  491. $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  492. ->find('mmouse')
  493. ->current();
  494. $this->assertEquals(
  495. 1,
  496. count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  497. 'Expecting to find one dependent row'
  498. );
  499. $account1->delete();
  500. $tableBugsCustom = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  501. $this->assertEquals(
  502. 0,
  503. count(
  504. $tableBugsCustom->fetchAll(
  505. $tableBugsCustom->getAdapter()
  506. ->quoteInto("$reported_by = ?", 'mmouse')
  507. )
  508. ),
  509. 'Expecting cascading delete to have reduced dependent rows to zero'
  510. );
  511. }
  512. /**
  513. * Ensures that cascading delete functionality is not run when onDelete != self::CASCADE
  514. *
  515. * @return void
  516. */
  517. public function testTableRelationshipCascadingDeleteUsageInvalidNoop()
  518. {
  519. $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
  520. ->find(1)
  521. ->current();
  522. $this->assertEquals(
  523. 1,
  524. count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  525. 'Expecting to find one dependent row'
  526. );
  527. $product1->delete();
  528. $product_id = $this->_db->quoteIdentifier('product_id', true);
  529. $this->assertEquals(
  530. 1,
  531. count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$product_id = 1")),
  532. 'Expecting to find one dependent row'
  533. );
  534. }
  535. public function testTableRelationshipGetReference()
  536. {
  537. $table = $this->_table['bugs'];
  538. $map = $table->getReference('My_ZendDbTable_TableAccounts', 'Reporter');
  539. $this->assertThat($map, $this->arrayHasKey('columns'));
  540. $this->assertThat($map, $this->arrayHasKey('refTableClass'));
  541. $this->assertThat($map, $this->arrayHasKey('refColumns'));
  542. }
  543. public function testTableRelationshipGetReferenceException()
  544. {
  545. $table = $this->_table['bugs'];
  546. try {
  547. $table->getReference('My_ZendDbTable_TableAccounts', 'Nonexistent');
  548. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent reference rule');
  549. } catch (Zend_Exception $e) {
  550. $this->assertType('Zend_Db_Table_Exception', $e,
  551. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  552. }
  553. try {
  554. $table->getReference('Nonexistent', 'Reporter');
  555. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
  556. } catch (Zend_Exception $e) {
  557. $this->assertType('Zend_Db_Table_Exception', $e,
  558. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  559. }
  560. try {
  561. $table->getReference('Nonexistent');
  562. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
  563. } catch (Zend_Exception $e) {
  564. $this->assertType('Zend_Db_Table_Exception', $e,
  565. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  566. }
  567. }
  568. /**
  569. * Ensures that findParentRow() returns an instance of a custom row class when passed an instance
  570. * of the table class having $_rowClass overridden.
  571. *
  572. * @return void
  573. */
  574. public function testTableRelationshipFindParentRowCustomInstance()
  575. {
  576. $this->_useMyIncludePath();
  577. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  578. Zend_Loader::loadClass($myRowClass);
  579. $bug1Reporter = $this->_table['bugs']
  580. ->find(1)
  581. ->current()
  582. ->findParentRow($this->_table['accounts']->setRowClass($myRowClass));
  583. $this->assertType($myRowClass, $bug1Reporter,
  584. "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
  585. }
  586. /**
  587. * Ensures that findParentRow() returns an instance of a custom row class when passed a string class
  588. * name, where the class has $_rowClass overridden.
  589. *
  590. * @return void
  591. */
  592. public function testTableRelationshipFindParentRowCustomClass()
  593. {
  594. $this->_useMyIncludePath();
  595. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  596. Zend_Loader::loadClass($myRowClass);
  597. Zend_Loader::loadClass('My_ZendDbTable_TableAccountsCustom');
  598. $bug1Reporter = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  599. ->find(1)
  600. ->current()
  601. ->findParentRow(new My_ZendDbTable_TableAccountsCustom(array('db' => $this->_db)));
  602. $this->assertType($myRowClass, $bug1Reporter,
  603. "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
  604. }
  605. /**
  606. * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
  607. * passed an instance of the table class.
  608. *
  609. * @return void
  610. */
  611. public function testTableRelationshipFindDependentRowsetCustomInstance()
  612. {
  613. $this->_useMyIncludePath();
  614. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  615. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  616. Zend_Loader::loadClass($myRowsetClass);
  617. $account_name = $this->_db->quoteIdentifier('account_name', true);
  618. $bugs = $this->_table['accounts']
  619. ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
  620. ->findDependentRowset(
  621. $this->_table['bugs']
  622. ->setRowsetClass($myRowsetClass)
  623. ->setRowClass($myRowClass),
  624. 'Engineer'
  625. );
  626. $this->assertType($myRowsetClass, $bugs,
  627. "Expecting object of type $myRowsetClass, got ".get_class($bugs));
  628. $this->assertEquals(3, count($bugs));
  629. foreach ($bugs as $bug) {
  630. $this->assertType($myRowClass, $bug,
  631. "Expecting object of type $myRowClass, got ".get_class($bug));
  632. }
  633. }
  634. /**
  635. * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
  636. * passed the named class.
  637. *
  638. * @return void
  639. */
  640. public function testTableRelationshipFindDependentRowsetCustomClass()
  641. {
  642. $this->_useMyIncludePath();
  643. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  644. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  645. Zend_Loader::loadClass($myRowsetClass);
  646. $account_name = $this->_db->quoteIdentifier('account_name', true);
  647. $bugs = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  648. ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
  649. ->findDependentRowset('My_ZendDbTable_TableBugsCustom', 'Engineer');
  650. $this->assertType($myRowsetClass, $bugs,
  651. "Expecting object of type $myRowsetClass, got ".get_class($bugs));
  652. $this->assertEquals(3, count($bugs));
  653. foreach ($bugs as $bug) {
  654. $this->assertType($myRowClass, $bug,
  655. "Expecting object of type $myRowClass, got ".get_class($bug));
  656. }
  657. }
  658. /**
  659. * Ensures that findManyToManyRowset() returns instances of custom row and rowset class when
  660. * passed an instance of the table class.
  661. *
  662. * @return void
  663. */
  664. public function testTableRelationshipFindManyToManyRowsetCustomInstance()
  665. {
  666. $this->_useMyIncludePath();
  667. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  668. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  669. Zend_Loader::loadClass($myRowsetClass);
  670. $bug1Products = $this->_table['bugs']
  671. ->find(1)
  672. ->current()
  673. ->findManyToManyRowset(
  674. $this->_table['products']
  675. ->setRowsetClass($myRowsetClass)
  676. ->setRowClass($myRowClass),
  677. 'My_ZendDbTable_TableBugsProducts'
  678. );
  679. $this->assertType($myRowsetClass, $bug1Products,
  680. "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
  681. $this->assertEquals(3, count($bug1Products));
  682. foreach ($bug1Products as $bug1Product) {
  683. $this->assertType($myRowClass, $bug1Product,
  684. "Expecting object of type $myRowClass, got ".get_class($bug1Product));
  685. }
  686. }
  687. /**
  688. * Ensures that findManyToManyRowset() returns instances of custom row and rowset classes when
  689. * passed the named class.
  690. *
  691. * @return void
  692. */
  693. public function testTableRelationshipFindManyToManyRowsetCustomClass()
  694. {
  695. $this->_useMyIncludePath();
  696. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  697. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  698. Zend_Loader::loadClass($myRowsetClass);
  699. $bug1Products = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  700. ->find(1)
  701. ->current()
  702. ->findManyToManyRowset(
  703. 'My_ZendDbTable_TableProductsCustom',
  704. 'My_ZendDbTable_TableBugsProductsCustom'
  705. );
  706. $this->assertType($myRowsetClass, $bug1Products,
  707. "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
  708. $this->assertEquals(3, count($bug1Products));
  709. foreach ($bug1Products as $bug1Product) {
  710. $this->assertType($myRowClass, $bug1Product,
  711. "Expecting object of type $myRowClass, got ".get_class($bug1Product));
  712. }
  713. }
  714. /**
  715. * Ensures that rows returned by findParentRow() are updatable.
  716. *
  717. * @return void
  718. */
  719. public function testTableRelationshipFindParentRowIsUpdateable()
  720. {
  721. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  722. $account_name = $this->_db->foldCase('account_name');
  723. $table = $this->_table['bugs'];
  724. $childRows = $table->fetchAll("$bug_id = 1");
  725. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  726. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  727. $childRow1 = $childRows->current();
  728. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  729. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  730. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
  731. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  732. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  733. $this->assertEquals('goofy', $parentRow->$account_name);
  734. $parentRow->$account_name = 'clarabell';
  735. try {
  736. $parentRow->save();
  737. } catch (Zend_Exception $e) {
  738. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  739. }
  740. $accounts = $this->_db->quoteIdentifier('zfaccounts', true);
  741. $account_name = $this->_db->quoteIdentifier('account_name', true);
  742. $accounts_list = $this->_db->fetchCol("SELECT $account_name from $accounts ORDER BY $account_name");
  743. // if the save() did an UPDATE instead of an INSERT, then goofy should
  744. // be missing, and clarabell should be present
  745. $this->assertEquals(array('clarabell', 'dduck', 'mmouse'), $accounts_list);
  746. }
  747. /**
  748. * Ensures that rows returned by findDependentRowset() are updatable.
  749. *
  750. * @return void
  751. */
  752. public function testTableRelationshipFindDependentRowsetIsUpdateable()
  753. {
  754. $table = $this->_table['accounts'];
  755. $bug_id_column = $this->_db->foldCase('bug_id');
  756. $bug_description = $this->_db->foldCase('bug_description');
  757. $parentRows = $table->find('mmouse');
  758. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  759. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  760. $parentRow1 = $parentRows->current();
  761. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugs');
  762. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  763. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  764. $this->assertEquals(1, $childRows->count());
  765. $childRow1 = $childRows->current();
  766. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  767. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  768. $childRow1->$bug_description = 'Updated description';
  769. $bug_id = $childRow1->$bug_id_column;
  770. try {
  771. $childRow1->save();
  772. } catch (Zend_Exception $e) {
  773. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  774. }
  775. // find the row we just updated and make sure it has the new value.
  776. $bugs_table = $this->_table['bugs'];
  777. $bugs_rows = $bugs_table->find($bug_id);
  778. $this->assertEquals(1, $bugs_rows->count());
  779. $bug1 = $bugs_rows->current();
  780. $this->assertEquals($bug_id, $bug1->$bug_id_column);
  781. $this->assertEquals('Updated description', $bug1->$bug_description);
  782. }
  783. /**
  784. * Ensures that rows returned by findManyToManyRowset() are updatable.
  785. *
  786. * @return void
  787. */
  788. public function testTableRelationshipFindManyToManyRowsetIsUpdateable()
  789. {
  790. $table = $this->_table['bugs'];
  791. $product_id_column = $this->_db->foldCase('product_id');
  792. $product_name = $this->_db->foldCase('product_name');
  793. $originRows = $table->find(1);
  794. $originRow1 = $originRows->current();
  795. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
  796. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  797. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  798. $this->assertEquals(3, $destRows->count());
  799. $row1 = $destRows->current();
  800. $product_id = $row1->$product_id_column;
  801. $row1->$product_name = 'AmigaOS';
  802. try {
  803. $row1->save();
  804. } catch (Zend_Exception $e) {
  805. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  806. }
  807. // find the row we just updated and make sure it has the new value.
  808. $products_table = $this->_table['products'];
  809. $product_rows = $products_table->find($product_id);
  810. $this->assertEquals(1, $product_rows->count());
  811. $product_row = $product_rows->current();
  812. $this->assertEquals($product_id, $product_row->$product_id_column);
  813. $this->assertEquals('AmigaOS', $product_row->$product_name);
  814. }
  815. public function testTableRelationshipOmitRefColumns()
  816. {
  817. $refMap = array(
  818. 'Reporter' => array(
  819. 'columns' => array('reported_by'),
  820. 'refTableClass' => 'My_ZendDbTable_TableAccounts'
  821. )
  822. );
  823. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  824. array(
  825. 'name' => 'zfbugs',
  826. 'referenceMap' => $refMap
  827. )
  828. );
  829. $bug1 = $table->find(1)->current();
  830. $reporter = $bug1->findParentRow('My_ZendDbTable_TableAccounts');
  831. $this->assertEquals(array('account_name' => 'goofy'), $reporter->toArray());
  832. }
  833. /**
  834. * Test that findParentRow() works even if the column names are
  835. * not the same.
  836. */
  837. public function testTableRelationshipFindParentRowWithDissimilarColumns()
  838. {
  839. $bug_id = $this->_db->foldCase('bug_id');
  840. $product_id = $this->_db->foldCase('product_id');
  841. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  842. $intRow = $intersectionTable->find(2, 3)->current();
  843. $bugRow = $intRow->findParentRow('My_ZendDbTable_TableBugs');
  844. $this->assertEquals(2, $bugRow->$bug_id);
  845. $productRow = $intRow->findParentRow('My_ZendDbTable_TableProducts');
  846. $this->assertEquals(3, $productRow->$product_id);
  847. }
  848. /**
  849. * Test that findDependentRowset() works even if the column names are
  850. * not the same.
  851. */
  852. public function testTableRelationshipFindDependentRowsetWithDissimilarColumns()
  853. {
  854. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  855. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  856. $bugRow = $bugsTable->find(2)->current();
  857. $intRows = $bugRow->findDependentRowset($intersectionTable);
  858. $this->assertEquals(array(2, 3), array_values($intRows->current()->toArray()));
  859. }
  860. /**
  861. * Test that findManyToManyRowset() works even if the column names are
  862. * not the same.
  863. */
  864. public function testTableRelationshipFindManyToManyRowsetWithDissimilarColumns()
  865. {
  866. $product_id = $this->_db->foldCase('product_id');
  867. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  868. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  869. $bugRow = $bugsTable->find(2)->current();
  870. $productRows = $bugRow->findManyToManyRowset('My_ZendDbTable_TableProducts', $intersectionTable);
  871. $this->assertEquals(3, $productRows->current()->$product_id);
  872. }
  873. /**
  874. * Test that findManyToManyRowset() works even if the column types are
  875. * not the same.
  876. */
  877. public function testTableRelationshipFindManyToManyRowsetWithDissimilarTypes()
  878. {
  879. $table = $this->_table['products'];
  880. $originRows = $table->find(1);
  881. $originRow1 = $originRows->current();
  882. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  883. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  884. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  885. $this->assertEquals(1, $destRows->count());
  886. }
  887. /**
  888. * @group ZF-3486
  889. */
  890. public function testTableRelationshipCanFindParentViaConcreteInstantiation()
  891. {
  892. Zend_Db_Table::setDefaultAdapter($this->_db);
  893. $definition = $this->_getTableDefinition();
  894. $bugsTable = new Zend_Db_Table('Bugs', $definition);
  895. $rowset = $bugsTable->find(1);
  896. $row = $rowset->current();
  897. $parent = $row->findParentRow('Accounts', 'Engineer');
  898. $this->assertEquals('mmouse', $parent->account_name);
  899. Zend_Db_Table::setDefaultAdapter();
  900. }
  901. /**
  902. * @group ZF-3486
  903. */
  904. public function testTableRelationshipCanFindDependentRowsetViaConcreteInstantiation()
  905. {
  906. Zend_Db_Table::setDefaultAdapter($this->_db);
  907. $definition = $this->_getTableDefinition();
  908. $productsTable = new Zend_Db_Table('Products', $definition);
  909. $productsRowset = $productsTable->find(1);
  910. $productRow = $productsRowset->current();
  911. $this->assertEquals('Windows', $productRow->product_name);
  912. $this->assertEquals(1, count($productRow->findDependentRowset('BugsProducts', 'Product')));
  913. $bugsProductRow = $productRow->findDependentRowset('BugsProducts', 'Product')->current();
  914. $this->assertEquals(1, $bugsProductRow->product_id);
  915. $this->assertEquals(1, $bugsProductRow->bug_id);
  916. Zend_Db_Table::setDefaultAdapter();
  917. }
  918. /**
  919. * @group ZF-3486
  920. */
  921. public function testTableRelationshipCanFindManyToManyRowsetViaConcreteInstantiation()
  922. {
  923. Zend_Db_Table::setDefaultAdapter($this->_db);
  924. $definition = $this->_getTableDefinition();
  925. $bugsTable = new Zend_Db_Table('Bugs', $definition);
  926. $bugsRowset = $bugsTable->find(1);
  927. $bugRow = $bugsRowset->current();
  928. $m2mRowset = $bugRow->findManyToManyRowset('Products', 'BugsProducts');
  929. $this->assertEquals(3, $m2mRowset->count());
  930. }
  931. /**
  932. * Utility Methods Below
  933. *
  934. */
  935. /**
  936. * _getTableDefinition()
  937. *
  938. * @return Zend_Db_Table_Definition
  939. */
  940. protected function _getTableDefinition()
  941. {
  942. $definition = array(
  943. 'Bugs' => array(
  944. 'name' => 'zfbugs',
  945. 'referenceMap' => array(
  946. 'Reporter' => array(
  947. 'columns' => 'reported_by',
  948. 'refTableClass' => 'Accounts',
  949. 'refColumns' => 'account_name'
  950. ),
  951. 'Engineer' => array(
  952. 'columns' => 'assigned_to',
  953. 'refTableClass' => 'Accounts',
  954. 'refColumns' => 'account_name'
  955. ),
  956. 'Verifier' => array(
  957. 'columns' => 'verified_by',
  958. 'refTableClass' => 'Accounts',
  959. 'refColumns' => 'account_name'
  960. )
  961. )
  962. ),
  963. 'Accounts' => array(
  964. 'name' => 'zfaccounts'
  965. ),
  966. 'BugsProducts' => array(
  967. 'name' => 'zfbugs_products',
  968. 'referenceMap' => array(
  969. 'Bug' => array(
  970. 'columns' => 'bug_id', // Deliberate non-array value
  971. 'refTableClass' => 'Bugs',
  972. 'refColumns' => 'bug_id'
  973. ),
  974. 'Product' => array(
  975. 'columns' => 'product_id',
  976. 'refTableClass' => 'Products',
  977. 'refColumns' => 'product_id',
  978. 'onDelete' => Zend_Db_Table::CASCADE,
  979. 'onUpdate' => Zend_Db_Table::CASCADE
  980. )
  981. )
  982. ),
  983. 'Products' => array(
  984. 'name' => 'zfproducts'
  985. )
  986. );
  987. return new Zend_Db_Table_Definition($definition);
  988. }
  989. /**
  990. * Create database table based on BUGS_PRODUCTS bug with alternative
  991. * spellings of column names. Then create a Table class for this
  992. * physical table and return it.
  993. */
  994. protected function _getBugsProductsWithDissimilarColumns()
  995. {
  996. $altCols = array(
  997. 'boog_id' => 'INTEGER NOT NULL',
  998. 'produck_id' => 'INTEGER NOT NULL',
  999. 'PRIMARY KEY' => 'boog_id,produck_id'
  1000. );
  1001. $this->_util->createTable('AltBugsProducts', $altCols);
  1002. $altBugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfalt_bugs_products'), true);
  1003. $bugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfbugs_products'), true);
  1004. $this->_db->query("INSERT INTO $altBugsProducts SELECT * FROM $bugsProducts");
  1005. $refMap = array(
  1006. 'Boog' => array(
  1007. 'columns' => array('boog_id'),
  1008. 'refTableClass' => 'My_ZendDbTable_TableBugs',
  1009. 'refColumns' => array('bug_id')
  1010. ),
  1011. 'Produck' => array(
  1012. 'columns' => array('produck_id'),
  1013. 'refTableClass' => 'My_ZendDbTable_TableProducts',
  1014. 'refColumns' => array('product_id')
  1015. )
  1016. );
  1017. $options = array('name' => 'zfalt_bugs_products', 'referenceMap' => $refMap);
  1018. $table = $this->_getTable('My_ZendDbTable_TableSpecial', $options);
  1019. return $table;
  1020. }
  1021. }