TestCommon.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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_Registry
  28. */
  29. require_once 'Zend/Registry.php';
  30. /**
  31. * @see Zend_Db_Table
  32. */
  33. require_once 'Zend/Db/Table.php';
  34. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  35. /**
  36. * @category Zend
  37. * @package Zend_Db
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. abstract class Zend_Db_Table_TestCommon extends Zend_Db_Table_TestSetup
  43. {
  44. public function testTableConstructor()
  45. {
  46. $bugs = $this->_table['bugs'];
  47. $info = $bugs->info();
  48. $config = array('db' => $this->_db,
  49. 'schema' => $info['schema'],
  50. 'name' => $info['name'],
  51. 'primary' => $info['primary'],
  52. 'cols' => $info['cols'],
  53. 'metadata' => $info['metadata'],
  54. 'metadataCache' => null,
  55. 'rowClass' => $info['rowClass'],
  56. 'rowsetClass' => $info['rowsetClass'],
  57. 'referenceMap' => $info['referenceMap'],
  58. 'dependentTables' => $info['dependentTables'],
  59. 'sequence' => $info['sequence'],
  60. 'unknownKey' => 'testValue');
  61. $table = new My_ZendDbTable_TableBugs($config);
  62. }
  63. // ZF-2379
  64. public function testAddReference()
  65. {
  66. $expectedReferences = array(
  67. 'columns' => array('reported_by'),
  68. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  69. 'refColumns' => array('account_name')
  70. );
  71. $products = $this->_table['products'];
  72. $products->addReference('Reporter', 'reported_by',
  73. 'My_ZendDbTable_TableAccounts', 'account_name');
  74. $references = $products->getReference('My_ZendDbTable_TableAccounts');
  75. $this->assertEquals($expectedReferences, $references);
  76. }
  77. // ZF-2666
  78. public function testIsIdentity()
  79. {
  80. $bugs = $this->_table['bugs'];
  81. $this->assertTrue($bugs->isIdentity('bug_id'));
  82. }
  83. /**
  84. * @group ZF-2510
  85. */
  86. public function testMetadataCacheInClassFlagShouldBeEnabledByDefault()
  87. {
  88. $bugs = $this->_table['bugs'];
  89. $this->assertTrue($bugs->metadataCacheInClass());
  90. }
  91. /**
  92. * @group ZF-2510
  93. */
  94. public function testMetadataCacheInClassFlagShouldBeMutable()
  95. {
  96. $bugs = $this->_table['bugs'];
  97. $this->assertTrue($bugs->metadataCacheInClass());
  98. $bugs->setMetadataCacheInClass(false);
  99. $this->assertFalse($bugs->metadataCacheInClass());
  100. }
  101. public function testTableInfo()
  102. {
  103. $bugs = $this->_table['bugs'];
  104. $this->assertType('Zend_Db_Table_Abstract', $bugs);
  105. $info = $bugs->info();
  106. $keys = array(
  107. Zend_Db_Table_Abstract::SCHEMA,
  108. Zend_Db_Table_Abstract::NAME,
  109. Zend_Db_Table_Abstract::COLS,
  110. Zend_Db_Table_Abstract::PRIMARY,
  111. Zend_Db_Table_Abstract::METADATA,
  112. Zend_Db_Table_Abstract::ROW_CLASS,
  113. Zend_Db_Table_Abstract::ROWSET_CLASS,
  114. Zend_Db_Table_Abstract::REFERENCE_MAP,
  115. Zend_Db_Table_Abstract::DEPENDENT_TABLES,
  116. Zend_Db_Table_Abstract::SEQUENCE,
  117. );
  118. $this->assertEquals($keys, array_keys($info));
  119. $this->assertEquals('zfbugs', $info['name']);
  120. $this->assertEquals(8, count($info['cols']));
  121. $cols = array(
  122. 'bug_id',
  123. 'bug_description',
  124. 'bug_status',
  125. 'created_on',
  126. 'updated_on',
  127. 'reported_by',
  128. 'assigned_to',
  129. 'verified_by'
  130. );
  131. $this->assertEquals($cols, $info['cols']);
  132. $this->assertEquals(1, count($info['primary']));
  133. $pk = array('bug_id');
  134. $this->assertEquals($pk, array_values($info['primary']));
  135. $name = $bugs->info(Zend_Db_Table_Abstract::NAME);
  136. $this->assertEquals('zfbugs', $name);
  137. try {
  138. $value = $bugs->info('_non_existent_');
  139. $this->fail('Expected to catch Zend_Db_Table_Exception');
  140. } catch (Zend_Exception $e) {
  141. $this->assertType('Zend_Db_Table_Exception', $e);
  142. $this->assertEquals('There is no table information for the key "_non_existent_"', $e->getMessage());
  143. }
  144. }
  145. /**
  146. * Ensures expected behavior when a table is assigned a Row class of stdClass
  147. *
  148. * @return void
  149. */
  150. public function testTableSetRowClassStdclass()
  151. {
  152. $productRowset = $this->_table['products']->setRowClass('stdClass')->fetchAll();
  153. $this->assertEquals(
  154. 3,
  155. $productRowsetCount = count($productRowset),
  156. "Expected rowset with 3 elements; got $productRowsetCount"
  157. );
  158. foreach ($productRowset as $productRow) {
  159. $this->assertThat(
  160. $productRow,
  161. $this->isInstanceOf('stdClass'),
  162. 'Expected row to be instance of stdClass; got ' . get_class($productRow)
  163. );
  164. }
  165. }
  166. /**
  167. * Ensures expected behavior when a table is assigned a Rowset class of stdClass
  168. *
  169. * @return void
  170. */
  171. public function testTableSetRowsetClassStdclass()
  172. {
  173. $productRowset = $this->_table['products']->setRowsetClass('stdClass')->fetchAll();
  174. $this->assertThat(
  175. $productRowset,
  176. $this->isInstanceOf('stdClass'),
  177. 'Expected rowset to be instance of stdClass; got ' . get_class($productRowset)
  178. );
  179. }
  180. public function testTableImplicitName()
  181. {
  182. include_once '_files/My/ZendDbTable/TableSpecial.php';
  183. // TableSpecial.php contains class bugs_products too.
  184. $table = new zfbugs_products(array('db' => $this->_db));
  185. $info = $table->info();
  186. $this->assertContains('name', array_keys($info));
  187. $this->assertEquals('zfbugs_products', $info['name']);
  188. }
  189. public function testTableOptionName()
  190. {
  191. $tableName = 'zfbugs';
  192. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  193. array('name' => $tableName)
  194. );
  195. $info = $table->info();
  196. $this->assertContains('name', array_keys($info));
  197. $this->assertEquals($tableName, $info['name']);
  198. }
  199. public function testTableOptionSchema()
  200. {
  201. $schemaName = $this->_util->getSchema();
  202. $tableName = 'zfbugs';
  203. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  204. array('name' => $tableName, 'schema' => $schemaName)
  205. );
  206. $info = $table->info();
  207. $this->assertContains('schema', array_keys($info));
  208. $this->assertEquals($schemaName, $info['schema']);
  209. }
  210. public function testTableArgumentAdapter()
  211. {
  212. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  213. $this->_db);
  214. $db = $table->getAdapter();
  215. $this->assertSame($this->_db, $db);
  216. }
  217. public function testTableOptionAdapter()
  218. {
  219. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  220. array('db' => $this->_db));
  221. $db = $table->getAdapter();
  222. $this->assertSame($this->_db, $db);
  223. }
  224. public function testTableOptionRowClass()
  225. {
  226. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  227. array('rowClass' => 'stdClass'));
  228. $rowClass = $table->getRowClass();
  229. $this->assertEquals($rowClass, 'stdClass');
  230. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  231. array('rowsetClass' => 'stdClass'));
  232. $rowsetClass = $table->getRowsetClass();
  233. $this->assertEquals($rowsetClass, 'stdClass');
  234. }
  235. public function testTableGetRowClass()
  236. {
  237. $table = $this->_table['products'];
  238. $this->assertType('Zend_Db_Table_Abstract', $table);
  239. $rowClass = $table->getRowClass();
  240. $this->assertEquals($rowClass, 'Zend_Db_Table_Row');
  241. $rowsetClass = $table->getRowsetClass();
  242. $this->assertEquals($rowsetClass, 'Zend_Db_Table_Rowset');
  243. }
  244. public function testTableOptionReferenceMap()
  245. {
  246. $refReporter = array(
  247. 'columns' => array('reported_by'),
  248. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  249. 'refColumns' => array('account_id')
  250. );
  251. $refEngineer = array(
  252. 'columns' => array('assigned_to'),
  253. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  254. 'refColumns' => array('account_id')
  255. );
  256. $refMap = array(
  257. 'Reporter' => $refReporter,
  258. 'Engineer' => $refEngineer
  259. );
  260. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  261. array('referenceMap' => $refMap));
  262. $this->assertEquals($refReporter, $table->getReference('My_ZendDbTable_TableAccounts'));
  263. $this->assertEquals($refReporter, $table->getReference('My_ZendDbTable_TableAccounts', 'Reporter'));
  264. $this->assertEquals($refEngineer, $table->getReference('My_ZendDbTable_TableAccounts', 'Engineer'));
  265. }
  266. public function testTableExceptionOptionReferenceMap()
  267. {
  268. $refReporter = array(
  269. 'columns' => array('reported_by'),
  270. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  271. 'refColumns' => array('account_id')
  272. );
  273. $refEngineer = array(
  274. 'columns' => array('assigned_to'),
  275. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  276. 'refColumns' => array('account_id')
  277. );
  278. $refMap = array(
  279. 'Reporter' => $refReporter,
  280. 'Engineer' => $refEngineer
  281. );
  282. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  283. array('referenceMap' => $refMap));
  284. try {
  285. $ref = $table->getReference('My_ZendDbTable_TableAccounts', 'Verifier');
  286. $this->fail('Expected to catch Zend_Db_Table_Exception');
  287. } catch (Zend_Exception $e) {
  288. $this->assertType('Zend_Db_Table_Exception', $e);
  289. $this->assertEquals('No reference rule "Verifier" from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableAccounts', $e->getMessage());
  290. }
  291. try {
  292. $ref = $table->getReference('My_ZendDbTable_TableProducts');
  293. $this->fail('Expected to catch Zend_Db_Table_Exception');
  294. } catch (Zend_Exception $e) {
  295. $this->assertType('Zend_Db_Table_Exception', $e);
  296. $this->assertEquals('No reference from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableProducts', $e->getMessage());
  297. }
  298. try {
  299. $ref = $table->getReference('My_ZendDbTable_TableProducts', 'Product');
  300. $this->fail('Expected to catch Zend_Db_Table_Exception');
  301. } catch (Zend_Exception $e) {
  302. $this->assertType('Zend_Db_Table_Exception', $e);
  303. $this->assertEquals('No reference rule "Product" from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableProducts', $e->getMessage());
  304. }
  305. try {
  306. $ref = $table->getReference('My_ZendDbTable_TableProducts', 'Reporter');
  307. $this->fail('Expected to catch Zend_Db_Table_Exception');
  308. } catch (Zend_Exception $e) {
  309. $this->assertType('Zend_Db_Table_Exception', $e);
  310. $this->assertEquals('Reference rule "Reporter" does not reference table My_ZendDbTable_TableProducts', $e->getMessage());
  311. }
  312. }
  313. public function testTableOptionDependentTables()
  314. {
  315. $depTables = array('Zend_Db_Table_Foo');
  316. $table = $this->_getTable('My_ZendDbTable_TableBugs',
  317. array('dependentTables' => $depTables));
  318. $this->assertEquals($depTables, $table->getDependentTables());
  319. }
  320. public function testTableSetRowClass()
  321. {
  322. $table = $this->_table['products'];
  323. $this->assertType('Zend_Db_Table_Abstract', $table);
  324. $table->setRowClass('stdClass');
  325. $rowClass = $table->getRowClass();
  326. $this->assertEquals($rowClass, 'stdClass');
  327. $table->setRowsetClass('stdClass');
  328. $rowsetClass = $table->getRowsetClass();
  329. $this->assertEquals($rowsetClass, 'stdClass');
  330. }
  331. public function testTableSetDefaultAdapter()
  332. {
  333. /**
  334. * Don't use _getTable() method because it defaults the adapter
  335. */
  336. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  337. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  338. $db = Zend_Db_Table_Abstract::getDefaultAdapter();
  339. $this->assertSame($this->_db, $db);
  340. $table = new My_ZendDbTable_TableBugs();
  341. $db = $table->getAdapter();
  342. $this->assertSame($this->_db, $db);
  343. }
  344. public function testTableWithNoAdapterAndNoDefaultAdapter()
  345. {
  346. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  347. $this->assertNull(Zend_Db_Table_Abstract::getDefaultAdapter());
  348. try {
  349. $table = new My_ZendDbTable_TableBugs();
  350. $this->fail('Zend_Db_Table_Exception should be thrown');
  351. }catch(Zend_Exception $e) {
  352. $this->assertType('Zend_Db_Table_Exception', $e,
  353. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  354. }
  355. }
  356. public function testTableSetDefaultAdapterNull()
  357. {
  358. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  359. $db = Zend_Db_Table_Abstract::getDefaultAdapter();
  360. $this->assertSame($this->_db, $db);
  361. Zend_Db_Table_Abstract::setDefaultAdapter();
  362. $this->assertNull(Zend_Db_Table_Abstract::getDefaultAdapter());
  363. }
  364. public function testTableSetDefaultAdapterRegistry()
  365. {
  366. /**
  367. * Don't use _getTable() method because it defaults the adapter
  368. */
  369. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  370. Zend_Registry::set('registered_db', $this->_db);
  371. Zend_Db_Table_Abstract::setDefaultAdapter('registered_db');
  372. $db = Zend_Db_Table_Abstract::getDefaultAdapter();
  373. $this->assertSame($this->_db, $db);
  374. $table = new My_ZendDbTable_TableBugs();
  375. $db = $table->getAdapter();
  376. $this->assertSame($this->_db, $db);
  377. }
  378. public function testTableSetDefaultAdapterException()
  379. {
  380. try {
  381. Zend_Db_Table_Abstract::setDefaultAdapter(new stdClass());
  382. $this->fail('Expected to catch Zend_Db_Table_Exception');
  383. } catch (Zend_Exception $e) {
  384. $this->assertType('Zend_Db_Table_Exception', $e,
  385. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  386. $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
  387. }
  388. try {
  389. Zend_Db_Table_Abstract::setDefaultAdapter(327);
  390. $this->fail('Expected to catch Zend_Db_Table_Exception');
  391. } catch (Exception $e) {
  392. $this->assertType('Zend_Db_Table_Exception', $e,
  393. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  394. $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
  395. }
  396. }
  397. public function testTableExceptionPrimaryKeyNotSpecified()
  398. {
  399. try {
  400. $table = $this->_getTable('My_ZendDbTable_TableBugs', array('primary' => ''));
  401. $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
  402. $this->fail('Expected to catch Zend_Db_Table_Exception');
  403. } catch (Zend_Exception $e) {
  404. $this->assertType('Zend_Db_Table_Exception', $e,
  405. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  406. $this->assertContains("Primary key column(s)", $e->getMessage());
  407. $this->assertContains("are not columns in this table", $e->getMessage());
  408. }
  409. }
  410. public function testTableExceptionInvalidPrimaryKey()
  411. {
  412. try {
  413. $table = new My_ZendDbTable_TableBugs(array('primary' => 'foo'));
  414. $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
  415. $this->fail('Expected to catch Zend_Db_Table_Exception');
  416. } catch (Zend_Exception $e) {
  417. $this->assertType('Zend_Db_Table_Exception', $e,
  418. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  419. $this->assertContains("Primary key column(s)", $e->getMessage());
  420. $this->assertContains("are not columns in this table", $e->getMessage());
  421. }
  422. }
  423. public function testTableExceptionNoPrimaryKey()
  424. {
  425. // create a table that has no primary key
  426. $this->_util->createTable('noprimarykey', array('id' => 'INTEGER'));
  427. $tableName = $this->_util->getTableName('noprimarykey');
  428. try {
  429. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  430. array('name' => $tableName));
  431. $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
  432. $this->fail('Expected to catch Zend_Db_Table_Exception');
  433. } catch (Zend_Exception $e) {
  434. $this->assertType('Zend_Db_Table_Exception', $e,
  435. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  436. $this->assertEquals('A table must have a primary key, but none was found', $e->getMessage());
  437. }
  438. $this->_util->dropTable($tableName);
  439. }
  440. public function testTableWithNoPrimaryKeyButOptionSpecifiesOne()
  441. {
  442. // create a table that has no primary key constraint
  443. $this->_util->createTable('noprimarykey', array('id' => 'INTEGER'));
  444. $tableName = $this->_util->getTableName('noprimarykey');
  445. try {
  446. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  447. array('name' => $tableName, 'primary' => 'id'));
  448. } catch (Zend_Exception $e) {
  449. $this->fail('Expected to succeed without a Zend_Db_Table_Exception');
  450. }
  451. $info = $table->info();
  452. $this->assertEquals(array(1=>'id'), $info['primary']);
  453. $this->_util->dropTable($tableName);
  454. }
  455. public function testTableAdapterException()
  456. {
  457. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  458. /**
  459. * options array points 'db' to integer scalar
  460. */
  461. try {
  462. $table = new My_ZendDbTable_TableBugs(array('db' => 327));
  463. $this->fail('Expected to catch Zend_Db_Table_Exception');
  464. } catch (Zend_Exception $e) {
  465. $this->assertType('Zend_Db_Table_Exception', $e,
  466. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  467. $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
  468. }
  469. /**
  470. * options array points 'db' to Registry key containing integer scalar
  471. */
  472. Zend_Registry::set('registered_db', 327);
  473. try {
  474. $table = new My_ZendDbTable_TableBugs(array('db' => 'registered_db'));
  475. $this->fail('Expected to catch Zend_Db_Table_Exception');
  476. } catch (Zend_Exception $e) {
  477. $this->assertType('Zend_Db_Table_Exception', $e,
  478. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  479. $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
  480. }
  481. }
  482. public function testTableFindSingleRow()
  483. {
  484. $table = $this->_table['bugs'];
  485. $rowset = $table->find(1);
  486. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  487. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  488. $this->assertEquals(1, count($rowset));
  489. }
  490. public function testTableFindMultipleRows()
  491. {
  492. $table = $this->_table['bugs'];
  493. $rowset = $table->find(array(1, 2));
  494. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  495. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  496. $this->assertEquals(2, count($rowset));
  497. }
  498. public function testTableFindExceptionTooFewKeys()
  499. {
  500. $table = $this->_table['bugs_products'];
  501. try {
  502. $table->find(1);
  503. $this->fail('Expected to catch Zend_Db_Table_Exception for missing key');
  504. } catch (Zend_Exception $e) {
  505. $this->assertType('Zend_Db_Table_Exception', $e,
  506. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  507. $this->assertEquals('Too few columns for the primary key', $e->getMessage());
  508. }
  509. }
  510. public function testTableFindExceptionTooManyKeys()
  511. {
  512. $table = $this->_table['bugs'];
  513. try {
  514. $table->find(1, 2);
  515. $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect key count');
  516. } catch (Zend_Exception $e) {
  517. $this->assertType('Zend_Db_Table_Exception', $e,
  518. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  519. $this->assertEquals('Too many columns for the primary key', $e->getMessage());
  520. }
  521. }
  522. public function testTableFindCompoundSingleRow()
  523. {
  524. $table = $this->_table['bugs_products'];
  525. $rowset = $table->find(1, 2);
  526. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  527. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  528. $this->assertEquals(1, count($rowset));
  529. }
  530. public function testTableFindCompoundMultipleRows()
  531. {
  532. $table = $this->_table['bugs_products'];
  533. $rowset = $table->find(array(1, 1), array(2, 3));
  534. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  535. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  536. $this->assertEquals(2, count($rowset));
  537. }
  538. public function testTableFindCompoundMultipleExceptionIncorrectValueCount()
  539. {
  540. $table = $this->_table['bugs_products'];
  541. try {
  542. $rowset = $table->find(array(1, 1), 2);
  543. $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect key count');
  544. } catch (Zend_Exception $e) {
  545. $this->assertType('Zend_Db_Table_Exception', $e,
  546. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  547. $this->assertEquals('Missing value(s) for the primary key', $e->getMessage());
  548. }
  549. }
  550. /**
  551. * @group ZF-3349
  552. */
  553. public function testTableFindMultipleRowsWithKeys()
  554. {
  555. $table = $this->_table['products'];
  556. $rowset = $table->find(array(0 => 1, 1 => 2, 99 => 3));
  557. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  558. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  559. $this->assertEquals(3, count($rowset));
  560. }
  561. public function testTableInsert()
  562. {
  563. $table = $this->_table['bugs'];
  564. $row = array (
  565. 'bug_id' => null,
  566. 'bug_description' => 'New bug',
  567. 'bug_status' => 'NEW',
  568. 'created_on' => '2007-04-02',
  569. 'updated_on' => '2007-04-02',
  570. 'reported_by' => 'micky',
  571. 'assigned_to' => 'goofy',
  572. 'verified_by' => 'dduck'
  573. );
  574. $insertResult = $table->insert($row);
  575. $lastInsertId = $this->_db->lastInsertId();
  576. $this->assertEquals($insertResult, $lastInsertId);
  577. $this->assertEquals(5, $lastInsertId);
  578. }
  579. public function testTableInsertWithSchema()
  580. {
  581. $schemaName = $this->_util->getSchema();
  582. $tableName = 'zfbugs';
  583. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  584. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  585. array('name' => $tableName, 'schema' => $schemaName)
  586. );
  587. $row = array (
  588. 'bug_description' => 'New bug',
  589. 'bug_status' => 'NEW',
  590. 'created_on' => '2007-04-02',
  591. 'updated_on' => '2007-04-02',
  592. 'reported_by' => 'micky',
  593. 'assigned_to' => 'goofy',
  594. 'verified_by' => 'dduck'
  595. );
  596. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  597. $this->_db->getProfiler()->setEnabled(true);
  598. $insertResult = $table->insert($row);
  599. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  600. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  601. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  602. $this->assertContains("INSERT INTO $tableSpec ", $qp->getQuery());
  603. }
  604. public function testTableInsertSequence()
  605. {
  606. $table = $this->_getTable('My_ZendDbTable_TableProducts',
  607. array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
  608. $row = array (
  609. 'product_name' => 'Solaris'
  610. );
  611. $insertResult = $table->insert($row);
  612. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  613. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  614. $this->assertEquals($insertResult, $lastInsertId);
  615. $this->assertEquals($insertResult, $lastSequenceId);
  616. $this->assertEquals(4, $insertResult);
  617. }
  618. public function testTableInsertNaturalCompound()
  619. {
  620. $table = $this->_table['bugs_products'];
  621. $row = array(
  622. 'bug_id' => 2,
  623. 'product_id' => 1
  624. );
  625. $primary = $table->insert($row);
  626. $this->assertType('array', $primary);
  627. $this->assertEquals(2, count($primary));
  628. $this->assertEquals(array(2, 1), array_values($primary));
  629. }
  630. /**
  631. * @todo
  632. *
  633. public function testTableInsertNaturalExceptionKeyViolation()
  634. {
  635. $table = $this->_table['bugs'];
  636. $row = array (
  637. 'bug_id' => 1,
  638. 'bug_description' => 'New bug',
  639. 'bug_status' => 'NEW',
  640. 'created_on' => '2007-04-02',
  641. 'updated_on' => '2007-04-02',
  642. 'reported_by' => 'micky',
  643. 'assigned_to' => 'goofy'
  644. );
  645. try {
  646. $insertResult = $table->insert($row);
  647. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  648. } catch (Zend_Exception $e) {
  649. echo "*** caught ".get_class($e)."\n";
  650. echo "*** ".$e->getMessage()."\n";
  651. $this->assertEquals('xxx', $e->getMessage());
  652. }
  653. }
  654. */
  655. /**
  656. * @todo
  657. *
  658. public function testTableInsertNaturalCompoundExceptionKeyViolation()
  659. {
  660. $table = $this->_table['bugs_products'];
  661. $row = array(
  662. 'bug_id' => 1,
  663. 'product_id' => 1
  664. );
  665. try {
  666. $table->insert($row);
  667. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  668. } catch (Zend_Exception $e) {
  669. echo "*** caught ".get_class($e)."\n";
  670. echo "*** ".$e->getMessage()."\n";
  671. $this->assertEquals('xxx', $e->getMessage());
  672. }
  673. }
  674. */
  675. /**
  676. * See ZF-1739 in our issue tracker.
  677. */
  678. public function testTableInsertMemoryUsageZf1739()
  679. {
  680. $this->markTestSkipped('Very slow test inserts thousands of rows');
  681. $table = $this->_table['products'];
  682. // insert one row to prime the pump
  683. $table->insert(array('product_name' => "product0"));
  684. // measure current memory usage
  685. $mem1 = memory_get_usage();
  686. // insert a lot of rows
  687. $n = 100000;
  688. for ($i = 1; $i <= $n; $i++)
  689. {
  690. $table->insert(array('product_name' => "product$i"));
  691. if ($i % 1000 == 0) {
  692. echo '.';
  693. }
  694. }
  695. // measure new memory usage
  696. $mem2 = memory_get_usage();
  697. // compare new memory usage to original
  698. $mem_delta = $mem2-$mem1;
  699. $this->assertThat($mem_delta, $this->lessThan(513));
  700. }
  701. public function testTableUpdate()
  702. {
  703. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  704. $bug_description = $this->_db->foldCase('bug_description');
  705. $bug_status = $this->_db->foldCase('bug_status');
  706. $data = array(
  707. $bug_description => 'Implement Do What I Mean function',
  708. $bug_status => 'INCOMPLETE'
  709. );
  710. $table = $this->_table['bugs'];
  711. $result = $table->update($data, "$bug_id = 2");
  712. $this->assertEquals(1, $result);
  713. // Query the row to see if we have the new values.
  714. $rowset = $table->find(2);
  715. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  716. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  717. $this->assertEquals(1, count($rowset), "Expecting rowset count to be 1");
  718. $row = $rowset->current();
  719. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  720. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  721. $bug_id = $this->_db->foldCase('bug_id');
  722. $this->assertEquals(2, $row->$bug_id, "Expecting row->bug_id to be 2");
  723. $this->assertEquals($data[$bug_description], $row->$bug_description);
  724. $this->assertEquals($data[$bug_status], $row->$bug_status);
  725. }
  726. public function testTableUpdateWithSchema()
  727. {
  728. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  729. $bug_description = $this->_db->foldCase('bug_description');
  730. $bug_status = $this->_db->foldCase('bug_status');
  731. $schemaName = $this->_util->getSchema();
  732. $tableName = 'zfbugs';
  733. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  734. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  735. array('name' => $tableName, 'schema' => $schemaName)
  736. );
  737. $data = array(
  738. $bug_description => 'Implement Do What I Mean function',
  739. $bug_status => 'INCOMPLETE'
  740. );
  741. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  742. $this->_db->getProfiler()->setEnabled(true);
  743. $result = $table->update($data, "$bug_id = 2");
  744. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  745. $this->assertEquals(1, $result);
  746. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  747. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  748. $this->assertContains("UPDATE $tableSpec ", $qp->getQuery());
  749. }
  750. public function testTableUpdateWhereArray()
  751. {
  752. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  753. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  754. $bug_description = $this->_db->foldCase('bug_description');
  755. $data = array(
  756. $bug_description => 'Synesthesia',
  757. );
  758. $where = array(
  759. "$bug_id IN (1, 3)",
  760. "$bug_status != 'UNKNOWN'"
  761. );
  762. $this->assertEquals(2, $this->_table['bugs']->update($data, $where));
  763. $count = 0;
  764. foreach ($this->_table['bugs']->find(array(1, 3)) as $row) {
  765. $this->assertEquals($data[$bug_description], $row->$bug_description);
  766. ++$count;
  767. }
  768. $this->assertEquals(2, $count);
  769. }
  770. public function testTableDelete()
  771. {
  772. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  773. $table = $this->_table['bugs'];
  774. $rowset = $table->find(array(1, 2));
  775. $this->assertEquals(2, count($rowset));
  776. $table->delete("$bug_id = 2");
  777. $rowset = $table->find(array(1, 2));
  778. $this->assertEquals(1, count($rowset));
  779. }
  780. public function testTableDeleteWithSchema()
  781. {
  782. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  783. $schemaName = $this->_util->getSchema();
  784. $tableName = 'zfbugs';
  785. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  786. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  787. array('name' => $tableName, 'schema' => $schemaName)
  788. );
  789. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  790. $this->_db->getProfiler()->setEnabled(true);
  791. $result = $table->delete("$bug_id = 2");
  792. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  793. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  794. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  795. $this->assertContains("DELETE FROM $tableSpec ", $qp->getQuery());
  796. }
  797. public function testTableDeleteWhereArray()
  798. {
  799. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  800. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  801. $where = array(
  802. "$bug_id IN (1, 3)",
  803. "$bug_status != 'UNKNOWN'"
  804. );
  805. $this->assertEquals(2, $this->_table['bugs']->delete($where));
  806. $this->assertEquals(0, count($this->_table['bugs']->find(array(1, 3))));
  807. }
  808. public function testTableCreateRow()
  809. {
  810. $table = $this->_table['bugs'];
  811. $row = $table->createRow();
  812. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  813. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  814. $this->assertTrue(isset($row->bug_description));
  815. $this->assertEquals($row, $table->fetchNew());
  816. }
  817. public function testTableCreateRowWithData()
  818. {
  819. $table = $this->_table['bugs'];
  820. $data = array (
  821. 'bug_description' => 'New bug',
  822. 'bug_status' => 'NEW',
  823. 'created_on' => '2007-04-02',
  824. 'updated_on' => '2007-04-02',
  825. 'reported_by' => 'micky',
  826. 'assigned_to' => 'goofy'
  827. );
  828. $row = $table->createRow($data);
  829. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  830. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  831. $this->assertTrue(isset($row->bug_description));
  832. $this->assertEquals('New bug', $row->bug_description);
  833. }
  834. public function testTableFetchRow()
  835. {
  836. $table = $this->_table['bugs'];
  837. $bug_description = $this->_db->foldCase('bug_description');
  838. $row = $table->fetchRow();
  839. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  840. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  841. $this->assertTrue(isset($row->$bug_description));
  842. }
  843. public function testTableFetchRowWhere()
  844. {
  845. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  846. $table = $this->_table['bugs'];
  847. $row = $table->fetchRow("$bug_id = 2");
  848. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  849. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  850. $bug_id = $this->_db->foldCase('bug_id');
  851. $this->assertEquals(2, $row->$bug_id);
  852. }
  853. public function testTableFetchRowWhereArray()
  854. {
  855. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  856. $table = $this->_table['bugs'];
  857. $row = $table->fetchRow(array("$bug_id = ?" => 2));
  858. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  859. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  860. $bug_id = $this->_db->foldCase('bug_id');
  861. $this->assertEquals(2, $row->$bug_id);
  862. }
  863. public function testTableFetchRowWhereSelect()
  864. {
  865. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  866. $table = $this->_table['bugs'];
  867. $select = $table->select()
  868. ->where("$bug_id = ?", 2);
  869. $row = $table->fetchRow($select);
  870. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  871. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  872. $bug_id = $this->_db->foldCase('bug_id');
  873. $this->assertEquals(2, $row->$bug_id);
  874. }
  875. public function testTableFetchRowOrderAsc()
  876. {
  877. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  878. $table = $this->_table['bugs'];
  879. $row = $table->fetchRow("$bug_id > 1", "bug_id ASC");
  880. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  881. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  882. $bug_id = $this->_db->foldCase('bug_id');
  883. $this->assertEquals(2, $row->$bug_id);
  884. }
  885. public function testTableFetchRowOrderSelectAsc()
  886. {
  887. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  888. $table = $this->_table['bugs'];
  889. $select = $table->select()
  890. ->where("$bug_id > ?", 1)
  891. ->order("bug_id ASC");
  892. $row = $table->fetchRow($select);
  893. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  894. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  895. $bug_id = $this->_db->foldCase('bug_id');
  896. $this->assertEquals(2, $row->$bug_id);
  897. }
  898. public function testTableFetchRowOrderDesc()
  899. {
  900. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  901. $table = $this->_table['bugs'];
  902. $row = $table->fetchRow(null, "bug_id DESC");
  903. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  904. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  905. $bug_id = $this->_db->foldCase('bug_id');
  906. $this->assertEquals(4, $row->$bug_id);
  907. }
  908. public function testTableFetchRowOrderSelectDesc()
  909. {
  910. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  911. $table = $this->_table['bugs'];
  912. $select = $table->select()
  913. ->where("$bug_id > ?", 1)
  914. ->order("bug_id DESC");
  915. $row = $table->fetchRow($select);
  916. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  917. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  918. $bug_id = $this->_db->foldCase('bug_id');
  919. $this->assertEquals(4, $row->$bug_id);
  920. }
  921. public function testTableFetchRowEmpty()
  922. {
  923. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  924. $table = $this->_table['bugs'];
  925. $row = $table->fetchRow("$bug_id = -1");
  926. $this->assertEquals(null, $row,
  927. 'Expecting null result for non-existent row');
  928. }
  929. public function testTableFetchAll()
  930. {
  931. $table = $this->_table['bugs'];
  932. $rowset = $table->fetchAll();
  933. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  934. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  935. $this->assertEquals(4, count($rowset));
  936. $row1 = $rowset->current();
  937. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  938. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  939. }
  940. public function testTableFetchAllWhere()
  941. {
  942. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  943. $table = $this->_table['bugs'];
  944. $rowset = $table->fetchAll("$bug_id = 2");
  945. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  946. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  947. $this->assertEquals(1, count($rowset));
  948. $row1 = $rowset->current();
  949. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  950. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  951. $bug_id = $this->_db->foldCase('bug_id');
  952. $this->assertEquals(2, $row1->$bug_id);
  953. }
  954. public function testTableFetchAllWhereSelect()
  955. {
  956. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  957. $table = $this->_table['bugs'];
  958. $select = $table->select()
  959. ->where("$bug_id = ?", 2);
  960. $rowset = $table->fetchAll($select);
  961. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  962. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  963. $this->assertEquals(1, count($rowset));
  964. $row1 = $rowset->current();
  965. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  966. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  967. $bug_id = $this->_db->foldCase('bug_id');
  968. $this->assertEquals(2, $row1->$bug_id);
  969. }
  970. public function testTableFetchAllOrder()
  971. {
  972. $table = $this->_table['bugs'];
  973. $rowset = $table->fetchAll(null, 'bug_id DESC');
  974. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  975. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  976. $this->assertEquals(4, count($rowset));
  977. $row1 = $rowset->current();
  978. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  979. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  980. $bug_id = $this->_db->foldCase('bug_id');
  981. $this->assertEquals(4, $row1->$bug_id);
  982. }
  983. public function testTableFetchAllOrderSelect()
  984. {
  985. $table = $this->_table['bugs'];
  986. $select = $table->select()
  987. ->order('bug_id DESC');
  988. $rowset = $table->fetchAll($select);
  989. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  990. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  991. $this->assertEquals(4, count($rowset));
  992. $row1 = $rowset->current();
  993. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  994. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  995. $bug_id = $this->_db->foldCase('bug_id');
  996. $this->assertEquals(4, $row1->$bug_id);
  997. }
  998. public function testTableFetchAllOrderExpr()
  999. {
  1000. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1001. $table = $this->_table['bugs'];
  1002. $rowset = $table->fetchAll(null, new Zend_Db_Expr("$bug_id + 1 DESC"));
  1003. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  1004. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1005. $this->assertEquals(4, count($rowset));
  1006. $row1 = $rowset->current();
  1007. $this->assertType('Zend_Db_Table_Row', $row1,
  1008. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1009. $bug_id = $this->_db->foldCase('bug_id');
  1010. $this->assertEquals(4, $row1->$bug_id);
  1011. }
  1012. public function testTableFetchAllLimit()
  1013. {
  1014. $table = $this->_table['bugs'];
  1015. $rowset = $table->fetchAll(null, 'bug_id ASC', 2, 1);
  1016. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  1017. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1018. $this->assertEquals(2, count($rowset));
  1019. $row1 = $rowset->current();
  1020. $this->assertType('Zend_Db_Table_Row', $row1,
  1021. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1022. $bug_id = $this->_db->foldCase('bug_id');
  1023. $this->assertEquals(2, $row1->$bug_id);
  1024. }
  1025. public function testTableFetchAllLimitSelect()
  1026. {
  1027. $table = $this->_table['bugs'];
  1028. $select = $table->select()
  1029. ->order('bug_id ASC')
  1030. ->limit(2, 1);
  1031. $rowset = $table->fetchAll($select);
  1032. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  1033. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1034. $this->assertEquals(2, count($rowset));
  1035. $row1 = $rowset->current();
  1036. $this->assertType('Zend_Db_Table_Row', $row1,
  1037. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1038. $bug_id = $this->_db->foldCase('bug_id');
  1039. $this->assertEquals(2, $row1->$bug_id);
  1040. }
  1041. public function testTableFetchAllEmpty()
  1042. {
  1043. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1044. $table = $this->_table['bugs'];
  1045. $rowset = $table->fetchAll("$bug_id = -1");
  1046. $this->assertEquals(0, count($rowset));
  1047. }
  1048. public function testTableLoadsCustomRowClass()
  1049. {
  1050. $this->_useMyIncludePath();
  1051. if (class_exists('My_ZendDbTable_Row_TestMyRow')) {
  1052. $this->markTestSkipped("Cannot test loading the custom Row class because it is already loaded");
  1053. return;
  1054. }
  1055. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1056. 'Expected TestMyRow class not to be loaded (#1)');
  1057. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1058. 'Expected TestMyRowset class not to be loaded (#1)');
  1059. // instantiating the table does not creat a rowset
  1060. // so the custom classes are not loaded yet
  1061. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1062. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1063. 'Expected TestMyRow class not to be loaded (#2)');
  1064. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1065. 'Expected TestMyRowset class not to be loaded (#2)');
  1066. // creating a rowset makes the table load the rowset class
  1067. // and the rowset constructor loads the row class.
  1068. $bugs = $bugsTable->fetchAll();
  1069. $this->assertTrue(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1070. 'Expected TestMyRow class to be loaded (#3)');
  1071. $this->assertTrue(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1072. 'Expected TestMyRowset class to be loaded (#3)');
  1073. }
  1074. /**
  1075. * Ensures that Zend_Db_Table_Abstract::setDefaultMetadataCache() performs as expected
  1076. *
  1077. * @return void
  1078. */
  1079. public function testTableSetDefaultMetadataCache()
  1080. {
  1081. $cache = $this->_getCache();
  1082. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1083. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1084. Zend_Db_Table_Abstract::setDefaultMetadataCache();
  1085. $this->assertNull(Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1086. }
  1087. public function testTableSetDefaultMetadataCacheRegistry()
  1088. {
  1089. $cache = $this->_getCache();
  1090. Zend_Registry::set('registered_metadata_cache', $cache);
  1091. Zend_Db_Table_Abstract::setDefaultMetadataCache('registered_metadata_cache');
  1092. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1093. }
  1094. public function testTableMetadataCacheRegistry()
  1095. {
  1096. $cache = $this->_getCache();
  1097. Zend_Registry::set('registered_metadata_cache', $cache);
  1098. $tableBugsCustom1 = $this->_getTable(
  1099. 'My_ZendDbTable_TableBugsCustom',
  1100. array('metadataCache' => 'registered_metadata_cache')
  1101. );
  1102. $this->assertSame($cache, $tableBugsCustom1->getMetadataCache());
  1103. }
  1104. public function testTableSetDefaultMetadataCacheWriteAccess()
  1105. {
  1106. $cache = $this->_getCacheNowrite();
  1107. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1108. try {
  1109. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  1110. $primary = $bugsTable->info(Zend_Db_Table_Abstract::PRIMARY);
  1111. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1112. } catch (Zend_Exception $e) {
  1113. $this->assertType('Zend_Db_Table_Exception', $e);
  1114. $this->assertEquals('Failed saving metadata to metadataCache', $e->getMessage());
  1115. }
  1116. Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
  1117. }
  1118. /**
  1119. * Ensures that table metadata caching works as expected when the cache object
  1120. * is set in the configuration for a new table object.
  1121. *
  1122. * @return void
  1123. */
  1124. public function testTableMetadataCacheNew()
  1125. {
  1126. $cache = $this->_getCache();
  1127. $tableBugsCustom1 = $this->_getTable(
  1128. 'My_ZendDbTable_TableBugsCustom',
  1129. array('metadataCache' => $cache)
  1130. );
  1131. $this->assertType(
  1132. 'Zend_Cache_Core',
  1133. $tableBugsCustom1->getMetadataCache()
  1134. );
  1135. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1136. $tableBugsCustom1->setup();
  1137. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1138. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1139. $tableBugsCustom1->setup();
  1140. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache after cleaning');
  1141. }
  1142. /**
  1143. * Ensures that table metadata caching can be persistent in the object even
  1144. * after a flushed cache, if the setMetadataCacheInClass property is true.
  1145. *
  1146. * @group ZF-2510
  1147. * @return void
  1148. */
  1149. public function testTableMetadataCacheInClass()
  1150. {
  1151. $cache = $this->_getCache();
  1152. $tableBugsCustom1 = $this->_getTable(
  1153. 'My_ZendDbTable_TableBugsCustom',
  1154. array(
  1155. 'metadataCache' => $cache,
  1156. 'metadataCacheInClass' => true,
  1157. )
  1158. );
  1159. $this->assertType(
  1160. 'Zend_Cache_Core',
  1161. $tableBugsCustom1->getMetadataCache()
  1162. );
  1163. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1164. $tableBugsCustom1->setup();
  1165. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1166. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1167. $tableBugsCustom1->setup();
  1168. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache after cleaning');
  1169. }
  1170. /**
  1171. * Ensures that table metadata caching works as expected when the default cache object
  1172. * is set for the abstract table class.
  1173. *
  1174. * @return void
  1175. */
  1176. public function testTableMetadataCacheClass()
  1177. {
  1178. $cache = $this->_getCache();
  1179. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1180. $tableBugsCustom1 = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1181. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1182. $this->assertType(
  1183. 'Zend_Cache_Core',
  1184. $tableBugsCustom1->getMetadataCache()
  1185. );
  1186. $tableBugsCustom1->setup();
  1187. $this->assertTrue($tableBugsCustom1->isMetadataFromCache);
  1188. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1189. $tableBugsCustom1->setup();
  1190. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1191. }
  1192. public function testTableSetDefaultMetadataCacheException()
  1193. {
  1194. try {
  1195. Zend_Db_Table_Abstract::setDefaultMetadataCache(new stdClass());
  1196. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1197. } catch (Zend_Exception $e) {
  1198. $this->assertType('Zend_Db_Table_Exception', $e,
  1199. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1200. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1201. }
  1202. try {
  1203. Zend_Db_Table_Abstract::setDefaultMetadataCache(327);
  1204. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1205. } catch (Exception $e) {
  1206. $this->assertType('Zend_Db_Table_Exception', $e,
  1207. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1208. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1209. }
  1210. }
  1211. public function testTableMetadataCacheException()
  1212. {
  1213. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  1214. /**
  1215. * options array points 'metadataCache' to integer scalar
  1216. */
  1217. try {
  1218. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 327));
  1219. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1220. } catch (Zend_Exception $e) {
  1221. $this->assertType('Zend_Db_Table_Exception', $e,
  1222. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1223. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1224. }
  1225. /**
  1226. * options array points 'metadataCache' to Registry key containing integer scalar
  1227. */
  1228. Zend_Registry::set('registered_metadata_cache', 327);
  1229. try {
  1230. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 'registered_metadata_cache'));
  1231. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1232. } catch (Zend_Exception $e) {
  1233. $this->assertType('Zend_Db_Table_Exception', $e,
  1234. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1235. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1236. }
  1237. }
  1238. public function testTableCascadeUpdate()
  1239. {
  1240. $table = $this->_table['products'];
  1241. $row1 = $table->find(1)->current();
  1242. $rows1 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1243. $product_id = $this->_db->foldCase('product_id');
  1244. $row1->$product_id = 999999;
  1245. $row1->save();
  1246. $rows2 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1247. $this->assertEquals(999999, $row1->$product_id);
  1248. $this->assertEquals(count($rows1), count($rows2));
  1249. // Test for 'false' value in cascade config
  1250. $bug_id = $this->_db->foldCase('bug_id');
  1251. $row2 = $rows2->current();
  1252. $row2->$bug_id = 999999;
  1253. $row2->save();
  1254. }
  1255. public function testTableCascadeDelete()
  1256. {
  1257. $table = $this->_table['products'];
  1258. $row1 = $table->find(2)->current();
  1259. $row1->delete();
  1260. // Test for 'false' value in cascade config
  1261. $table = $this->_table['bugs'];
  1262. $row2 = $table->find(1)->current();
  1263. $row2->delete();
  1264. $table = $this->_table['bugs_products'];
  1265. $product_id = $this->_db->quoteIdentifier('product_id', true);
  1266. $select = $table->select()
  1267. ->where($product_id . ' = ?', 2);
  1268. $rows = $table->fetchAll($select);
  1269. $this->assertEquals(0, count($rows));
  1270. }
  1271. public function testSerialiseTable()
  1272. {
  1273. $table = $this->_table['products'];
  1274. $this->assertType('string', serialize($table));
  1275. }
  1276. /**
  1277. * @group ZF-1343
  1278. */
  1279. public function testTableFetchallCanHandleWhereWithParameritizationCharacters()
  1280. {
  1281. $product_name = $this->_db->quoteIdentifier('product_name');
  1282. $table = $this->_table['products'];
  1283. $where = $table->getAdapter()->quoteInto("$product_name = ?", "some?product's");
  1284. $rows = $table->fetchAll($where);
  1285. $this->assertEquals(0, count($rows));
  1286. }
  1287. /**
  1288. * @group ZF-3486
  1289. */
  1290. public function testTableConcreteInstantiation()
  1291. {
  1292. Zend_Db_Table::setDefaultAdapter($this->_db);
  1293. $table = new Zend_Db_Table('zfbugs');
  1294. $rowset = $table->find(1);
  1295. $this->assertEquals(1, count($rowset));
  1296. Zend_Db_Table::setDefaultAdapter();
  1297. $table = new Zend_Db_Table(array(
  1298. 'name' => 'zfbugs',
  1299. 'db' => $this->_db
  1300. ));
  1301. $rowset = $table->find(1);
  1302. $this->assertEquals(1, count($rowset));
  1303. }
  1304. /**
  1305. * Returns a clean Zend_Cache_Core with File backend
  1306. *
  1307. * @return Zend_Cache_Core
  1308. */
  1309. protected function _getCache()
  1310. {
  1311. /**
  1312. * @see Zend_Cache
  1313. */
  1314. require_once 'Zend/Cache.php';
  1315. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'cachefiles';
  1316. $frontendOptions = array(
  1317. 'automatic_serialization' => true
  1318. );
  1319. $backendOptions = array(
  1320. 'cache_dir' => $folder,
  1321. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1322. );
  1323. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1324. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1325. return $cacheFrontend;
  1326. }
  1327. /**
  1328. * Returns a clean Zend_Cache_Core with File backend
  1329. *
  1330. * @return Zend_Cache_Core
  1331. */
  1332. protected function _getCacheNowrite()
  1333. {
  1334. /**
  1335. * @see Zend_Cache
  1336. */
  1337. require_once 'Zend/Cache.php';
  1338. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'nofiles';
  1339. if (!file_exists($folder)) {
  1340. mkdir($folder, 0777);
  1341. }
  1342. $frontendOptions = array(
  1343. 'automatic_serialization' => true
  1344. );
  1345. $backendOptions = array(
  1346. 'cache_dir' => $folder,
  1347. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1348. );
  1349. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1350. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1351. rmdir($folder);
  1352. return $cacheFrontend;
  1353. }
  1354. }