TestCommon.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. public function testTableInsert()
  551. {
  552. $table = $this->_table['bugs'];
  553. $row = array (
  554. 'bug_id' => null,
  555. 'bug_description' => 'New bug',
  556. 'bug_status' => 'NEW',
  557. 'created_on' => '2007-04-02',
  558. 'updated_on' => '2007-04-02',
  559. 'reported_by' => 'micky',
  560. 'assigned_to' => 'goofy',
  561. 'verified_by' => 'dduck'
  562. );
  563. $insertResult = $table->insert($row);
  564. $lastInsertId = $this->_db->lastInsertId();
  565. $this->assertEquals($insertResult, $lastInsertId);
  566. $this->assertEquals(5, $lastInsertId);
  567. }
  568. public function testTableInsertWithSchema()
  569. {
  570. $schemaName = $this->_util->getSchema();
  571. $tableName = 'zfbugs';
  572. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  573. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  574. array('name' => $tableName, 'schema' => $schemaName)
  575. );
  576. $row = array (
  577. 'bug_description' => 'New bug',
  578. 'bug_status' => 'NEW',
  579. 'created_on' => '2007-04-02',
  580. 'updated_on' => '2007-04-02',
  581. 'reported_by' => 'micky',
  582. 'assigned_to' => 'goofy',
  583. 'verified_by' => 'dduck'
  584. );
  585. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  586. $this->_db->getProfiler()->setEnabled(true);
  587. $insertResult = $table->insert($row);
  588. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  589. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  590. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  591. $this->assertContains("INSERT INTO $tableSpec ", $qp->getQuery());
  592. }
  593. public function testTableInsertSequence()
  594. {
  595. $table = $this->_getTable('My_ZendDbTable_TableProducts',
  596. array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
  597. $row = array (
  598. 'product_name' => 'Solaris'
  599. );
  600. $insertResult = $table->insert($row);
  601. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  602. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  603. $this->assertEquals($insertResult, $lastInsertId);
  604. $this->assertEquals($insertResult, $lastSequenceId);
  605. $this->assertEquals(4, $insertResult);
  606. }
  607. public function testTableInsertNaturalCompound()
  608. {
  609. $table = $this->_table['bugs_products'];
  610. $row = array(
  611. 'bug_id' => 2,
  612. 'product_id' => 1
  613. );
  614. $primary = $table->insert($row);
  615. $this->assertType('array', $primary);
  616. $this->assertEquals(2, count($primary));
  617. $this->assertEquals(array(2, 1), array_values($primary));
  618. }
  619. /**
  620. * @todo
  621. *
  622. public function testTableInsertNaturalExceptionKeyViolation()
  623. {
  624. $table = $this->_table['bugs'];
  625. $row = array (
  626. 'bug_id' => 1,
  627. 'bug_description' => 'New bug',
  628. 'bug_status' => 'NEW',
  629. 'created_on' => '2007-04-02',
  630. 'updated_on' => '2007-04-02',
  631. 'reported_by' => 'micky',
  632. 'assigned_to' => 'goofy'
  633. );
  634. try {
  635. $insertResult = $table->insert($row);
  636. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  637. } catch (Zend_Exception $e) {
  638. echo "*** caught ".get_class($e)."\n";
  639. echo "*** ".$e->getMessage()."\n";
  640. $this->assertEquals('xxx', $e->getMessage());
  641. }
  642. }
  643. */
  644. /**
  645. * @todo
  646. *
  647. public function testTableInsertNaturalCompoundExceptionKeyViolation()
  648. {
  649. $table = $this->_table['bugs_products'];
  650. $row = array(
  651. 'bug_id' => 1,
  652. 'product_id' => 1
  653. );
  654. try {
  655. $table->insert($row);
  656. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  657. } catch (Zend_Exception $e) {
  658. echo "*** caught ".get_class($e)."\n";
  659. echo "*** ".$e->getMessage()."\n";
  660. $this->assertEquals('xxx', $e->getMessage());
  661. }
  662. }
  663. */
  664. /**
  665. * See ZF-1739 in our issue tracker.
  666. */
  667. public function testTableInsertMemoryUsageZf1739()
  668. {
  669. $this->markTestSkipped('Very slow test inserts thousands of rows');
  670. $table = $this->_table['products'];
  671. // insert one row to prime the pump
  672. $table->insert(array('product_name' => "product0"));
  673. // measure current memory usage
  674. $mem1 = memory_get_usage();
  675. // insert a lot of rows
  676. $n = 100000;
  677. for ($i = 1; $i <= $n; $i++)
  678. {
  679. $table->insert(array('product_name' => "product$i"));
  680. if ($i % 1000 == 0) {
  681. echo '.';
  682. }
  683. }
  684. // measure new memory usage
  685. $mem2 = memory_get_usage();
  686. // compare new memory usage to original
  687. $mem_delta = $mem2-$mem1;
  688. $this->assertThat($mem_delta, $this->lessThan(513));
  689. }
  690. public function testTableUpdate()
  691. {
  692. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  693. $bug_description = $this->_db->foldCase('bug_description');
  694. $bug_status = $this->_db->foldCase('bug_status');
  695. $data = array(
  696. $bug_description => 'Implement Do What I Mean function',
  697. $bug_status => 'INCOMPLETE'
  698. );
  699. $table = $this->_table['bugs'];
  700. $result = $table->update($data, "$bug_id = 2");
  701. $this->assertEquals(1, $result);
  702. // Query the row to see if we have the new values.
  703. $rowset = $table->find(2);
  704. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  705. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  706. $this->assertEquals(1, count($rowset), "Expecting rowset count to be 1");
  707. $row = $rowset->current();
  708. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  709. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  710. $bug_id = $this->_db->foldCase('bug_id');
  711. $this->assertEquals(2, $row->$bug_id, "Expecting row->bug_id to be 2");
  712. $this->assertEquals($data[$bug_description], $row->$bug_description);
  713. $this->assertEquals($data[$bug_status], $row->$bug_status);
  714. }
  715. public function testTableUpdateWithSchema()
  716. {
  717. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  718. $bug_description = $this->_db->foldCase('bug_description');
  719. $bug_status = $this->_db->foldCase('bug_status');
  720. $schemaName = $this->_util->getSchema();
  721. $tableName = 'zfbugs';
  722. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  723. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  724. array('name' => $tableName, 'schema' => $schemaName)
  725. );
  726. $data = array(
  727. $bug_description => 'Implement Do What I Mean function',
  728. $bug_status => 'INCOMPLETE'
  729. );
  730. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  731. $this->_db->getProfiler()->setEnabled(true);
  732. $result = $table->update($data, "$bug_id = 2");
  733. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  734. $this->assertEquals(1, $result);
  735. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  736. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  737. $this->assertContains("UPDATE $tableSpec ", $qp->getQuery());
  738. }
  739. public function testTableUpdateWhereArray()
  740. {
  741. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  742. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  743. $bug_description = $this->_db->foldCase('bug_description');
  744. $data = array(
  745. $bug_description => 'Synesthesia',
  746. );
  747. $where = array(
  748. "$bug_id IN (1, 3)",
  749. "$bug_status != 'UNKNOWN'"
  750. );
  751. $this->assertEquals(2, $this->_table['bugs']->update($data, $where));
  752. $count = 0;
  753. foreach ($this->_table['bugs']->find(array(1, 3)) as $row) {
  754. $this->assertEquals($data[$bug_description], $row->$bug_description);
  755. ++$count;
  756. }
  757. $this->assertEquals(2, $count);
  758. }
  759. public function testTableDelete()
  760. {
  761. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  762. $table = $this->_table['bugs'];
  763. $rowset = $table->find(array(1, 2));
  764. $this->assertEquals(2, count($rowset));
  765. $table->delete("$bug_id = 2");
  766. $rowset = $table->find(array(1, 2));
  767. $this->assertEquals(1, count($rowset));
  768. }
  769. public function testTableDeleteWithSchema()
  770. {
  771. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  772. $schemaName = $this->_util->getSchema();
  773. $tableName = 'zfbugs';
  774. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  775. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  776. array('name' => $tableName, 'schema' => $schemaName)
  777. );
  778. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  779. $this->_db->getProfiler()->setEnabled(true);
  780. $result = $table->delete("$bug_id = 2");
  781. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  782. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  783. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  784. $this->assertContains("DELETE FROM $tableSpec ", $qp->getQuery());
  785. }
  786. public function testTableDeleteWhereArray()
  787. {
  788. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  789. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  790. $where = array(
  791. "$bug_id IN (1, 3)",
  792. "$bug_status != 'UNKNOWN'"
  793. );
  794. $this->assertEquals(2, $this->_table['bugs']->delete($where));
  795. $this->assertEquals(0, count($this->_table['bugs']->find(array(1, 3))));
  796. }
  797. public function testTableCreateRow()
  798. {
  799. $table = $this->_table['bugs'];
  800. $row = $table->createRow();
  801. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  802. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  803. $this->assertTrue(isset($row->bug_description));
  804. $this->assertEquals($row, $table->fetchNew());
  805. }
  806. public function testTableCreateRowWithData()
  807. {
  808. $table = $this->_table['bugs'];
  809. $data = array (
  810. 'bug_description' => 'New bug',
  811. 'bug_status' => 'NEW',
  812. 'created_on' => '2007-04-02',
  813. 'updated_on' => '2007-04-02',
  814. 'reported_by' => 'micky',
  815. 'assigned_to' => 'goofy'
  816. );
  817. $row = $table->createRow($data);
  818. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  819. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  820. $this->assertTrue(isset($row->bug_description));
  821. $this->assertEquals('New bug', $row->bug_description);
  822. }
  823. public function testTableFetchRow()
  824. {
  825. $table = $this->_table['bugs'];
  826. $bug_description = $this->_db->foldCase('bug_description');
  827. $row = $table->fetchRow();
  828. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  829. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  830. $this->assertTrue(isset($row->$bug_description));
  831. }
  832. public function testTableFetchRowWhere()
  833. {
  834. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  835. $table = $this->_table['bugs'];
  836. $row = $table->fetchRow("$bug_id = 2");
  837. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  838. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  839. $bug_id = $this->_db->foldCase('bug_id');
  840. $this->assertEquals(2, $row->$bug_id);
  841. }
  842. public function testTableFetchRowWhereArray()
  843. {
  844. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  845. $table = $this->_table['bugs'];
  846. $row = $table->fetchRow(array("$bug_id = ?" => 2));
  847. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  848. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  849. $bug_id = $this->_db->foldCase('bug_id');
  850. $this->assertEquals(2, $row->$bug_id);
  851. }
  852. public function testTableFetchRowWhereSelect()
  853. {
  854. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  855. $table = $this->_table['bugs'];
  856. $select = $table->select()
  857. ->where("$bug_id = ?", 2);
  858. $row = $table->fetchRow($select);
  859. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  860. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  861. $bug_id = $this->_db->foldCase('bug_id');
  862. $this->assertEquals(2, $row->$bug_id);
  863. }
  864. public function testTableFetchRowOrderAsc()
  865. {
  866. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  867. $table = $this->_table['bugs'];
  868. $row = $table->fetchRow("$bug_id > 1", "bug_id ASC");
  869. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  870. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  871. $bug_id = $this->_db->foldCase('bug_id');
  872. $this->assertEquals(2, $row->$bug_id);
  873. }
  874. public function testTableFetchRowOrderSelectAsc()
  875. {
  876. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  877. $table = $this->_table['bugs'];
  878. $select = $table->select()
  879. ->where("$bug_id > ?", 1)
  880. ->order("bug_id ASC");
  881. $row = $table->fetchRow($select);
  882. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  883. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  884. $bug_id = $this->_db->foldCase('bug_id');
  885. $this->assertEquals(2, $row->$bug_id);
  886. }
  887. public function testTableFetchRowOrderDesc()
  888. {
  889. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  890. $table = $this->_table['bugs'];
  891. $row = $table->fetchRow(null, "bug_id DESC");
  892. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  893. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  894. $bug_id = $this->_db->foldCase('bug_id');
  895. $this->assertEquals(4, $row->$bug_id);
  896. }
  897. public function testTableFetchRowOrderSelectDesc()
  898. {
  899. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  900. $table = $this->_table['bugs'];
  901. $select = $table->select()
  902. ->where("$bug_id > ?", 1)
  903. ->order("bug_id DESC");
  904. $row = $table->fetchRow($select);
  905. $this->assertType('Zend_Db_Table_Row_Abstract', $row,
  906. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  907. $bug_id = $this->_db->foldCase('bug_id');
  908. $this->assertEquals(4, $row->$bug_id);
  909. }
  910. public function testTableFetchRowEmpty()
  911. {
  912. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  913. $table = $this->_table['bugs'];
  914. $row = $table->fetchRow("$bug_id = -1");
  915. $this->assertEquals(null, $row,
  916. 'Expecting null result for non-existent row');
  917. }
  918. public function testTableFetchAll()
  919. {
  920. $table = $this->_table['bugs'];
  921. $rowset = $table->fetchAll();
  922. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  923. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  924. $this->assertEquals(4, count($rowset));
  925. $row1 = $rowset->current();
  926. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  927. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  928. }
  929. public function testTableFetchAllWhere()
  930. {
  931. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  932. $table = $this->_table['bugs'];
  933. $rowset = $table->fetchAll("$bug_id = 2");
  934. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  935. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  936. $this->assertEquals(1, count($rowset));
  937. $row1 = $rowset->current();
  938. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  939. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  940. $bug_id = $this->_db->foldCase('bug_id');
  941. $this->assertEquals(2, $row1->$bug_id);
  942. }
  943. public function testTableFetchAllWhereSelect()
  944. {
  945. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  946. $table = $this->_table['bugs'];
  947. $select = $table->select()
  948. ->where("$bug_id = ?", 2);
  949. $rowset = $table->fetchAll($select);
  950. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  951. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  952. $this->assertEquals(1, count($rowset));
  953. $row1 = $rowset->current();
  954. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  955. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  956. $bug_id = $this->_db->foldCase('bug_id');
  957. $this->assertEquals(2, $row1->$bug_id);
  958. }
  959. public function testTableFetchAllOrder()
  960. {
  961. $table = $this->_table['bugs'];
  962. $rowset = $table->fetchAll(null, 'bug_id DESC');
  963. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  964. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  965. $this->assertEquals(4, count($rowset));
  966. $row1 = $rowset->current();
  967. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  968. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  969. $bug_id = $this->_db->foldCase('bug_id');
  970. $this->assertEquals(4, $row1->$bug_id);
  971. }
  972. public function testTableFetchAllOrderSelect()
  973. {
  974. $table = $this->_table['bugs'];
  975. $select = $table->select()
  976. ->order('bug_id DESC');
  977. $rowset = $table->fetchAll($select);
  978. $this->assertType('Zend_Db_Table_Rowset_Abstract', $rowset,
  979. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  980. $this->assertEquals(4, count($rowset));
  981. $row1 = $rowset->current();
  982. $this->assertType('Zend_Db_Table_Row_Abstract', $row1,
  983. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  984. $bug_id = $this->_db->foldCase('bug_id');
  985. $this->assertEquals(4, $row1->$bug_id);
  986. }
  987. public function testTableFetchAllOrderExpr()
  988. {
  989. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  990. $table = $this->_table['bugs'];
  991. $rowset = $table->fetchAll(null, new Zend_Db_Expr("$bug_id + 1 DESC"));
  992. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  993. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  994. $this->assertEquals(4, count($rowset));
  995. $row1 = $rowset->current();
  996. $this->assertType('Zend_Db_Table_Row', $row1,
  997. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  998. $bug_id = $this->_db->foldCase('bug_id');
  999. $this->assertEquals(4, $row1->$bug_id);
  1000. }
  1001. public function testTableFetchAllLimit()
  1002. {
  1003. $table = $this->_table['bugs'];
  1004. $rowset = $table->fetchAll(null, 'bug_id ASC', 2, 1);
  1005. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  1006. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1007. $this->assertEquals(2, count($rowset));
  1008. $row1 = $rowset->current();
  1009. $this->assertType('Zend_Db_Table_Row', $row1,
  1010. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1011. $bug_id = $this->_db->foldCase('bug_id');
  1012. $this->assertEquals(2, $row1->$bug_id);
  1013. }
  1014. public function testTableFetchAllLimitSelect()
  1015. {
  1016. $table = $this->_table['bugs'];
  1017. $select = $table->select()
  1018. ->order('bug_id ASC')
  1019. ->limit(2, 1);
  1020. $rowset = $table->fetchAll($select);
  1021. $this->assertType('Zend_Db_Table_Rowset', $rowset,
  1022. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1023. $this->assertEquals(2, count($rowset));
  1024. $row1 = $rowset->current();
  1025. $this->assertType('Zend_Db_Table_Row', $row1,
  1026. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1027. $bug_id = $this->_db->foldCase('bug_id');
  1028. $this->assertEquals(2, $row1->$bug_id);
  1029. }
  1030. public function testTableFetchAllEmpty()
  1031. {
  1032. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1033. $table = $this->_table['bugs'];
  1034. $rowset = $table->fetchAll("$bug_id = -1");
  1035. $this->assertEquals(0, count($rowset));
  1036. }
  1037. public function testTableLoadsCustomRowClass()
  1038. {
  1039. $this->_useMyIncludePath();
  1040. if (class_exists('My_ZendDbTable_Row_TestMyRow')) {
  1041. $this->markTestSkipped("Cannot test loading the custom Row class because it is already loaded");
  1042. return;
  1043. }
  1044. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1045. 'Expected TestMyRow class not to be loaded (#1)');
  1046. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1047. 'Expected TestMyRowset class not to be loaded (#1)');
  1048. // instantiating the table does not creat a rowset
  1049. // so the custom classes are not loaded yet
  1050. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1051. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1052. 'Expected TestMyRow class not to be loaded (#2)');
  1053. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1054. 'Expected TestMyRowset class not to be loaded (#2)');
  1055. // creating a rowset makes the table load the rowset class
  1056. // and the rowset constructor loads the row class.
  1057. $bugs = $bugsTable->fetchAll();
  1058. $this->assertTrue(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1059. 'Expected TestMyRow class to be loaded (#3)');
  1060. $this->assertTrue(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1061. 'Expected TestMyRowset class to be loaded (#3)');
  1062. }
  1063. /**
  1064. * Ensures that Zend_Db_Table_Abstract::setDefaultMetadataCache() performs as expected
  1065. *
  1066. * @return void
  1067. */
  1068. public function testTableSetDefaultMetadataCache()
  1069. {
  1070. $cache = $this->_getCache();
  1071. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1072. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1073. Zend_Db_Table_Abstract::setDefaultMetadataCache();
  1074. $this->assertNull(Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1075. }
  1076. public function testTableSetDefaultMetadataCacheRegistry()
  1077. {
  1078. $cache = $this->_getCache();
  1079. Zend_Registry::set('registered_metadata_cache', $cache);
  1080. Zend_Db_Table_Abstract::setDefaultMetadataCache('registered_metadata_cache');
  1081. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1082. }
  1083. public function testTableMetadataCacheRegistry()
  1084. {
  1085. $cache = $this->_getCache();
  1086. Zend_Registry::set('registered_metadata_cache', $cache);
  1087. $tableBugsCustom1 = $this->_getTable(
  1088. 'My_ZendDbTable_TableBugsCustom',
  1089. array('metadataCache' => 'registered_metadata_cache')
  1090. );
  1091. $this->assertSame($cache, $tableBugsCustom1->getMetadataCache());
  1092. }
  1093. public function testTableSetDefaultMetadataCacheWriteAccess()
  1094. {
  1095. $cache = $this->_getCacheNowrite();
  1096. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1097. try {
  1098. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  1099. $primary = $bugsTable->info(Zend_Db_Table_Abstract::PRIMARY);
  1100. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1101. } catch (Zend_Exception $e) {
  1102. $this->assertType('Zend_Db_Table_Exception', $e);
  1103. $this->assertEquals('Failed saving metadata to metadataCache', $e->getMessage());
  1104. }
  1105. Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
  1106. }
  1107. /**
  1108. * Ensures that table metadata caching works as expected when the cache object
  1109. * is set in the configuration for a new table object.
  1110. *
  1111. * @return void
  1112. */
  1113. public function testTableMetadataCacheNew()
  1114. {
  1115. $cache = $this->_getCache();
  1116. $tableBugsCustom1 = $this->_getTable(
  1117. 'My_ZendDbTable_TableBugsCustom',
  1118. array('metadataCache' => $cache)
  1119. );
  1120. $this->assertType(
  1121. 'Zend_Cache_Core',
  1122. $tableBugsCustom1->getMetadataCache()
  1123. );
  1124. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1125. $tableBugsCustom1->setup();
  1126. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1127. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1128. $tableBugsCustom1->setup();
  1129. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache after cleaning');
  1130. }
  1131. /**
  1132. * Ensures that table metadata caching can be persistent in the object even
  1133. * after a flushed cache, if the setMetadataCacheInClass property is true.
  1134. *
  1135. * @group ZF-2510
  1136. * @return void
  1137. */
  1138. public function testTableMetadataCacheInClass()
  1139. {
  1140. $cache = $this->_getCache();
  1141. $tableBugsCustom1 = $this->_getTable(
  1142. 'My_ZendDbTable_TableBugsCustom',
  1143. array(
  1144. 'metadataCache' => $cache,
  1145. 'metadataCacheInClass' => true,
  1146. )
  1147. );
  1148. $this->assertType(
  1149. 'Zend_Cache_Core',
  1150. $tableBugsCustom1->getMetadataCache()
  1151. );
  1152. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1153. $tableBugsCustom1->setup();
  1154. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1155. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1156. $tableBugsCustom1->setup();
  1157. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache after cleaning');
  1158. }
  1159. /**
  1160. * Ensures that table metadata caching works as expected when the default cache object
  1161. * is set for the abstract table class.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testTableMetadataCacheClass()
  1166. {
  1167. $cache = $this->_getCache();
  1168. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1169. $tableBugsCustom1 = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1170. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1171. $this->assertType(
  1172. 'Zend_Cache_Core',
  1173. $tableBugsCustom1->getMetadataCache()
  1174. );
  1175. $tableBugsCustom1->setup();
  1176. $this->assertTrue($tableBugsCustom1->isMetadataFromCache);
  1177. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1178. $tableBugsCustom1->setup();
  1179. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1180. }
  1181. public function testTableSetDefaultMetadataCacheException()
  1182. {
  1183. try {
  1184. Zend_Db_Table_Abstract::setDefaultMetadataCache(new stdClass());
  1185. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1186. } catch (Zend_Exception $e) {
  1187. $this->assertType('Zend_Db_Table_Exception', $e,
  1188. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1189. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1190. }
  1191. try {
  1192. Zend_Db_Table_Abstract::setDefaultMetadataCache(327);
  1193. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1194. } catch (Exception $e) {
  1195. $this->assertType('Zend_Db_Table_Exception', $e,
  1196. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1197. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1198. }
  1199. }
  1200. public function testTableMetadataCacheException()
  1201. {
  1202. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  1203. /**
  1204. * options array points 'metadataCache' to integer scalar
  1205. */
  1206. try {
  1207. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 327));
  1208. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1209. } catch (Zend_Exception $e) {
  1210. $this->assertType('Zend_Db_Table_Exception', $e,
  1211. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1212. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1213. }
  1214. /**
  1215. * options array points 'metadataCache' to Registry key containing integer scalar
  1216. */
  1217. Zend_Registry::set('registered_metadata_cache', 327);
  1218. try {
  1219. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 'registered_metadata_cache'));
  1220. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1221. } catch (Zend_Exception $e) {
  1222. $this->assertType('Zend_Db_Table_Exception', $e,
  1223. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1224. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1225. }
  1226. }
  1227. public function testTableCascadeUpdate()
  1228. {
  1229. $table = $this->_table['products'];
  1230. $row1 = $table->find(1)->current();
  1231. $rows1 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1232. $product_id = $this->_db->foldCase('product_id');
  1233. $row1->$product_id = 999999;
  1234. $row1->save();
  1235. $rows2 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1236. $this->assertEquals(999999, $row1->$product_id);
  1237. $this->assertEquals(count($rows1), count($rows2));
  1238. // Test for 'false' value in cascade config
  1239. $bug_id = $this->_db->foldCase('bug_id');
  1240. $row2 = $rows2->current();
  1241. $row2->$bug_id = 999999;
  1242. $row2->save();
  1243. }
  1244. public function testTableCascadeDelete()
  1245. {
  1246. $table = $this->_table['products'];
  1247. $row1 = $table->find(2)->current();
  1248. $row1->delete();
  1249. // Test for 'false' value in cascade config
  1250. $table = $this->_table['bugs'];
  1251. $row2 = $table->find(1)->current();
  1252. $row2->delete();
  1253. $table = $this->_table['bugs_products'];
  1254. $product_id = $this->_db->quoteIdentifier('product_id', true);
  1255. $select = $table->select()
  1256. ->where($product_id . ' = ?', 2);
  1257. $rows = $table->fetchAll($select);
  1258. $this->assertEquals(0, count($rows));
  1259. }
  1260. public function testSerialiseTable()
  1261. {
  1262. $table = $this->_table['products'];
  1263. $this->assertType('string', serialize($table));
  1264. }
  1265. /**
  1266. * @group ZF-1343
  1267. */
  1268. public function testTableFetchallCanHandleWhereWithParameritizationCharacters()
  1269. {
  1270. $table = $this->_table['products'];
  1271. $where = $table->getAdapter()->quoteInto('product_name = ?', "some?product's");
  1272. $rows = $table->fetchAll($where);
  1273. $this->assertEquals(0, count($rows));
  1274. }
  1275. /**
  1276. * @group ZF-3486
  1277. */
  1278. public function testTableConcreteInstantiation()
  1279. {
  1280. Zend_Db_Table::setDefaultAdapter($this->_db);
  1281. $table = new Zend_Db_Table('zfbugs');
  1282. $rowset = $table->find(1);
  1283. $this->assertEquals(1, count($rowset));
  1284. Zend_Db_Table::setDefaultAdapter();
  1285. $table = new Zend_Db_Table(array(
  1286. 'name' => 'zfbugs',
  1287. 'db' => $this->_db
  1288. ));
  1289. $rowset = $table->find(1);
  1290. $this->assertEquals(1, count($rowset));
  1291. }
  1292. /**
  1293. * Returns a clean Zend_Cache_Core with File backend
  1294. *
  1295. * @return Zend_Cache_Core
  1296. */
  1297. protected function _getCache()
  1298. {
  1299. /**
  1300. * @see Zend_Cache
  1301. */
  1302. require_once 'Zend/Cache.php';
  1303. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'cachefiles';
  1304. $frontendOptions = array(
  1305. 'automatic_serialization' => true
  1306. );
  1307. $backendOptions = array(
  1308. 'cache_dir' => $folder,
  1309. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1310. );
  1311. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1312. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1313. return $cacheFrontend;
  1314. }
  1315. /**
  1316. * Returns a clean Zend_Cache_Core with File backend
  1317. *
  1318. * @return Zend_Cache_Core
  1319. */
  1320. protected function _getCacheNowrite()
  1321. {
  1322. /**
  1323. * @see Zend_Cache
  1324. */
  1325. require_once 'Zend/Cache.php';
  1326. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'nofiles';
  1327. if (!file_exists($folder)) {
  1328. mkdir($folder, 0777);
  1329. }
  1330. $frontendOptions = array(
  1331. 'automatic_serialization' => true
  1332. );
  1333. $backendOptions = array(
  1334. 'cache_dir' => $folder,
  1335. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1336. );
  1337. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1338. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1339. rmdir($folder);
  1340. return $cacheFrontend;
  1341. }
  1342. }