TestCommon.php 68 KB

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