TestCommon.php 61 KB

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