TestCommon.php 56 KB

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