TestCommon.php 48 KB

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