TestCommon.php 46 KB

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