2
0

TestCommon.php 64 KB

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