TestCommon.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Table_TestSetup
  24. */
  25. require_once 'Zend/Db/Table/TestSetup.php';
  26. /**
  27. * @see Zend_Registry
  28. */
  29. require_once 'Zend/Registry.php';
  30. /**
  31. * @see Zend_Db_Table
  32. */
  33. require_once 'Zend/Db/Table.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Db
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. abstract class Zend_Db_Table_TestCommon extends Zend_Db_Table_TestSetup
  42. {
  43. public function testTableConstructor()
  44. {
  45. $bugs = $this->_table['bugs'];
  46. $info = $bugs->info();
  47. $config = array('db' => $this->_db,
  48. 'schema' => $info['schema'],
  49. 'name' => $info['name'],
  50. 'primary' => $info['primary'],
  51. 'cols' => $info['cols'],
  52. 'metadata' => $info['metadata'],
  53. 'metadataCache' => null,
  54. 'rowClass' => $info['rowClass'],
  55. 'rowsetClass' => $info['rowsetClass'],
  56. 'referenceMap' => $info['referenceMap'],
  57. 'dependentTables' => $info['dependentTables'],
  58. 'sequence' => $info['sequence'],
  59. 'unknownKey' => 'testValue');
  60. $table = new My_ZendDbTable_TableBugs($config);
  61. }
  62. // ZF-2379
  63. public function testAddReference()
  64. {
  65. $expectedReferences = array(
  66. 'columns' => array('reported_by'),
  67. 'refTableClass' => 'My_ZendDbTable_TableAccounts',
  68. 'refColumns' => array('account_name')
  69. );
  70. $products = $this->_table['products'];
  71. $products->addReference('Reporter', 'reported_by',
  72. 'My_ZendDbTable_TableAccounts', 'account_name');
  73. $references = $products->getReference('My_ZendDbTable_TableAccounts');
  74. $this->assertEquals($expectedReferences, $references);
  75. }
  76. // ZF-2666
  77. public function testIsIdentity()
  78. {
  79. $bugs = $this->_table['bugs'];
  80. $this->assertTrue($bugs->isIdentity('bug_id'));
  81. }
  82. /**
  83. * @group ZF-2510
  84. */
  85. public function testMetadataCacheInClassFlagShouldBeEnabledByDefault()
  86. {
  87. $bugs = $this->_table['bugs'];
  88. $this->assertTrue($bugs->metadataCacheInClass());
  89. }
  90. /**
  91. * @group ZF-2510
  92. */
  93. public function testMetadataCacheInClassFlagShouldBeMutable()
  94. {
  95. $bugs = $this->_table['bugs'];
  96. $this->assertTrue($bugs->metadataCacheInClass());
  97. $bugs->setMetadataCacheInClass(false);
  98. $this->assertFalse($bugs->metadataCacheInClass());
  99. }
  100. public function testTableInfo()
  101. {
  102. $bugs = $this->_table['bugs'];
  103. $this->assertTrue($bugs instanceof Zend_Db_Table_Abstract);
  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->assertTrue($e instanceof Zend_Db_Table_Exception);
  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->assertTrue($table instanceof Zend_Db_Table_Abstract);
  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->assertTrue($e instanceof Zend_Db_Table_Exception);
  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->assertTrue($e instanceof Zend_Db_Table_Exception);
  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->assertTrue($e instanceof Zend_Db_Table_Exception);
  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->assertTrue($e instanceof Zend_Db_Table_Exception);
  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->assertTrue($table instanceof Zend_Db_Table_Abstract);
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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 for table '{$tableName}'", $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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($e instanceof Zend_Db_Table_Exception,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  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->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  569. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  570. $this->assertEquals(0, count($rowset));
  571. }
  572. public function testTableInsert()
  573. {
  574. $table = $this->_table['bugs'];
  575. $row = array (
  576. 'bug_id' => null,
  577. 'bug_description' => 'New bug',
  578. 'bug_status' => 'NEW',
  579. 'created_on' => '2007-04-02',
  580. 'updated_on' => '2007-04-02',
  581. 'reported_by' => 'micky',
  582. 'assigned_to' => 'goofy',
  583. 'verified_by' => 'dduck'
  584. );
  585. $insertResult = $table->insert($row);
  586. $lastInsertId = $this->_db->lastInsertId();
  587. $this->assertEquals($insertResult, $lastInsertId);
  588. $this->assertEquals(5, $lastInsertId);
  589. }
  590. /**
  591. * @group ZF-3837
  592. */
  593. public function testTableInsertWhenAutoIncrementFieldIsAnEmptyStringShouldProduceNewAutoIncrementValue()
  594. {
  595. $table = $this->_table['bugs'];
  596. $row = array (
  597. 'bug_id' => '',
  598. 'bug_description' => 'New bug',
  599. 'bug_status' => 'NEW',
  600. 'created_on' => '2007-04-02',
  601. 'updated_on' => '2007-04-02',
  602. 'reported_by' => 'micky',
  603. 'assigned_to' => 'goofy',
  604. 'verified_by' => 'dduck'
  605. );
  606. $insertResult = $table->insert($row);
  607. $lastInsertId = $this->_db->lastInsertId();
  608. $this->assertEquals($insertResult, $lastInsertId);
  609. $this->assertEquals(5, $lastInsertId);
  610. }
  611. public function testTableInsertWithSchema()
  612. {
  613. $schemaName = $this->_util->getSchema();
  614. $tableName = 'zfbugs';
  615. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  616. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  617. array('name' => $tableName, 'schema' => $schemaName)
  618. );
  619. $row = array (
  620. 'bug_description' => 'New bug',
  621. 'bug_status' => 'NEW',
  622. 'created_on' => '2007-04-02',
  623. 'updated_on' => '2007-04-02',
  624. 'reported_by' => 'micky',
  625. 'assigned_to' => 'goofy',
  626. 'verified_by' => 'dduck'
  627. );
  628. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  629. $this->_db->getProfiler()->setEnabled(true)->setFilterQueryType(Zend_Db_Profiler::INSERT);
  630. $insertResult = $table->insert($row);
  631. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  632. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  633. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  634. $this->assertContains("INSERT INTO $tableSpec ", $qp->getQuery());
  635. }
  636. public function testTableInsertSequence()
  637. {
  638. $table = $this->_getTable('My_ZendDbTable_TableProducts',
  639. array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
  640. $row = array (
  641. 'product_name' => 'Solaris'
  642. );
  643. $insertResult = $table->insert($row);
  644. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  645. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  646. $this->assertEquals($insertResult, $lastInsertId);
  647. $this->assertEquals($insertResult, $lastSequenceId);
  648. $this->assertEquals(4, $insertResult);
  649. }
  650. public function testTableInsertNaturalCompound()
  651. {
  652. $table = $this->_table['bugs_products'];
  653. $row = array(
  654. 'bug_id' => 2,
  655. 'product_id' => 1
  656. );
  657. $primary = $table->insert($row);
  658. $this->assertTrue(is_array($primary));
  659. $this->assertEquals(2, count($primary));
  660. $this->assertEquals(array(2, 1), array_values($primary));
  661. }
  662. /**
  663. * @todo
  664. *
  665. public function testTableInsertNaturalExceptionKeyViolation()
  666. {
  667. $table = $this->_table['bugs'];
  668. $row = array (
  669. 'bug_id' => 1,
  670. 'bug_description' => 'New bug',
  671. 'bug_status' => 'NEW',
  672. 'created_on' => '2007-04-02',
  673. 'updated_on' => '2007-04-02',
  674. 'reported_by' => 'micky',
  675. 'assigned_to' => 'goofy'
  676. );
  677. try {
  678. $insertResult = $table->insert($row);
  679. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  680. } catch (Zend_Exception $e) {
  681. echo "*** caught ".get_class($e)."\n";
  682. echo "*** ".$e->getMessage()."\n";
  683. $this->assertEquals('xxx', $e->getMessage());
  684. }
  685. }
  686. */
  687. /**
  688. * @todo
  689. *
  690. public function testTableInsertNaturalCompoundExceptionKeyViolation()
  691. {
  692. $table = $this->_table['bugs_products'];
  693. $row = array(
  694. 'bug_id' => 1,
  695. 'product_id' => 1
  696. );
  697. try {
  698. $table->insert($row);
  699. $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
  700. } catch (Zend_Exception $e) {
  701. echo "*** caught ".get_class($e)."\n";
  702. echo "*** ".$e->getMessage()."\n";
  703. $this->assertEquals('xxx', $e->getMessage());
  704. }
  705. }
  706. */
  707. /**
  708. * See ZF-1739 in our issue tracker.
  709. * @group ZF-1739
  710. */
  711. public function testTableInsertWithHighMemoryUsage()
  712. {
  713. $this->markTestSkipped('Very slow test inserts thousands of rows');
  714. $table = $this->_table['products'];
  715. // insert one row to prime the pump
  716. $table->insert(array('product_name' => "product0"));
  717. // measure current memory usage
  718. $mem1 = memory_get_usage();
  719. // insert a lot of rows
  720. $n = 100000;
  721. for ($i = 1; $i <= $n; $i++)
  722. {
  723. $table->insert(array('product_name' => "product$i"));
  724. if ($i % 1000 == 0) {
  725. echo '.';
  726. }
  727. }
  728. // measure new memory usage
  729. $mem2 = memory_get_usage();
  730. // compare new memory usage to original
  731. $mem_delta = $mem2-$mem1;
  732. $this->assertThat($mem_delta, $this->lessThan(513));
  733. }
  734. /**
  735. * @group ZF-2953
  736. */
  737. public function testTableInsertWithEmptyValueAsPrimaryKey()
  738. {
  739. $table = $this->_table['bugs'];
  740. $row = array (
  741. 'bug_description' => 'New bug',
  742. 'bug_status' => 'NEW',
  743. 'created_on' => '2007-04-02',
  744. 'updated_on' => '2007-04-02',
  745. 'reported_by' => 'micky',
  746. 'assigned_to' => 'goofy',
  747. 'verified_by' => 'dduck'
  748. );
  749. // empty string
  750. $row['bug_id'] = '';
  751. $insertResult = $table->insert($row);
  752. $this->assertTrue(is_numeric($insertResult), 'Empty string did not return assigned primary key');
  753. // false (bool)
  754. $row['bug_id'] = false;
  755. $insertResult = $table->insert($row);
  756. $this->assertTrue(is_numeric($insertResult), 'Bool false did not return assigned primary key');
  757. // empty array
  758. $row['bug_id'] = array();
  759. $insertResult = $table->insert($row);
  760. $this->assertTrue(is_numeric($insertResult), 'Empty array did not return assigned primary key');
  761. // zero '0'
  762. $row['bug_id'] = '0';
  763. $table->delete('bug_id > 0'); // clear table
  764. $insertResult = $table->insert($row);
  765. $this->assertEquals('0', $insertResult, 'Zero string did not return assigned primary key');
  766. // zero 0
  767. $row['bug_id'] = 0;
  768. $table->delete('bug_id > 0'); // clear table
  769. $insertResult = $table->insert($row);
  770. $this->assertEquals('0', $insertResult, 'Zero int did not return assigned primary key');
  771. }
  772. public function testTableUpdate()
  773. {
  774. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  775. $bug_description = $this->_db->foldCase('bug_description');
  776. $bug_status = $this->_db->foldCase('bug_status');
  777. $data = array(
  778. $bug_description => 'Implement Do What I Mean function',
  779. $bug_status => 'INCOMPLETE'
  780. );
  781. $table = $this->_table['bugs'];
  782. $result = $table->update($data, "$bug_id = 2");
  783. $this->assertEquals(1, $result);
  784. // Query the row to see if we have the new values.
  785. $rowset = $table->find(2);
  786. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  787. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  788. $this->assertEquals(1, count($rowset), "Expecting rowset count to be 1");
  789. $row = $rowset->current();
  790. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  791. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  792. $bug_id = $this->_db->foldCase('bug_id');
  793. $this->assertEquals(2, $row->$bug_id, "Expecting row->bug_id to be 2");
  794. $this->assertEquals($data[$bug_description], $row->$bug_description);
  795. $this->assertEquals($data[$bug_status], $row->$bug_status);
  796. }
  797. public function testTableUpdateWithSchema()
  798. {
  799. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  800. $bug_description = $this->_db->foldCase('bug_description');
  801. $bug_status = $this->_db->foldCase('bug_status');
  802. $schemaName = $this->_util->getSchema();
  803. $tableName = 'zfbugs';
  804. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  805. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  806. array('name' => $tableName, 'schema' => $schemaName)
  807. );
  808. $data = array(
  809. $bug_description => 'Implement Do What I Mean function',
  810. $bug_status => 'INCOMPLETE'
  811. );
  812. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  813. $this->_db->getProfiler()->setEnabled(true);
  814. $result = $table->update($data, "$bug_id = 2");
  815. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  816. $this->assertEquals(1, $result);
  817. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  818. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  819. $this->assertContains("UPDATE $tableSpec ", $qp->getQuery());
  820. }
  821. public function testTableUpdateWhereArray()
  822. {
  823. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  824. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  825. $bug_description = $this->_db->foldCase('bug_description');
  826. $data = array(
  827. $bug_description => 'Synesthesia',
  828. );
  829. $where = array(
  830. "$bug_id IN (1, 3)",
  831. "$bug_status != 'UNKNOWN'"
  832. );
  833. $this->assertEquals(2, $this->_table['bugs']->update($data, $where));
  834. $count = 0;
  835. foreach ($this->_table['bugs']->find(array(1, 3)) as $row) {
  836. $this->assertEquals($data[$bug_description], $row->$bug_description);
  837. ++$count;
  838. }
  839. $this->assertEquals(2, $count);
  840. }
  841. public function testTableDelete()
  842. {
  843. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  844. $table = $this->_table['bugs'];
  845. $rowset = $table->find(array(1, 2));
  846. $this->assertEquals(2, count($rowset));
  847. $table->delete("$bug_id = 2");
  848. $rowset = $table->find(array(1, 2));
  849. $this->assertEquals(1, count($rowset));
  850. }
  851. public function testTableDeleteWithSchema()
  852. {
  853. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  854. $schemaName = $this->_util->getSchema();
  855. $tableName = 'zfbugs';
  856. $identifier = join('.', array_filter(array($schemaName, $tableName)));
  857. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  858. array('name' => $tableName, 'schema' => $schemaName)
  859. );
  860. $profilerEnabled = $this->_db->getProfiler()->getEnabled();
  861. $this->_db->getProfiler()->setEnabled(true);
  862. $result = $table->delete("$bug_id = 2");
  863. $this->_db->getProfiler()->setEnabled($profilerEnabled);
  864. $qp = $this->_db->getProfiler()->getLastQueryProfile();
  865. $tableSpec = $this->_db->quoteIdentifier($identifier, true);
  866. $this->assertContains("DELETE FROM $tableSpec ", $qp->getQuery());
  867. }
  868. public function testTableDeleteWhereArray()
  869. {
  870. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  871. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  872. $where = array(
  873. "$bug_id IN (1, 3)",
  874. "$bug_status != 'UNKNOWN'"
  875. );
  876. $this->assertEquals(2, $this->_table['bugs']->delete($where));
  877. $this->assertEquals(0, count($this->_table['bugs']->find(array(1, 3))));
  878. }
  879. public function testTableCreateRow()
  880. {
  881. $table = $this->_table['bugs'];
  882. $row = $table->createRow();
  883. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  884. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  885. $this->assertTrue(isset($row->bug_description));
  886. $this->assertEquals($row, $table->fetchNew());
  887. }
  888. public function testTableCreateRowWithData()
  889. {
  890. $table = $this->_table['bugs'];
  891. $data = array (
  892. 'bug_description' => 'New bug',
  893. 'bug_status' => 'NEW',
  894. 'created_on' => '2007-04-02',
  895. 'updated_on' => '2007-04-02',
  896. 'reported_by' => 'micky',
  897. 'assigned_to' => 'goofy'
  898. );
  899. $row = $table->createRow($data);
  900. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  901. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  902. $this->assertTrue(isset($row->bug_description));
  903. $this->assertEquals('New bug', $row->bug_description);
  904. }
  905. /**
  906. * @group ZF-8944
  907. * @group ZF-10598
  908. * @group ZF-11253
  909. */
  910. public function testTableFetchRowOffset()
  911. {
  912. $reported_by = $this->_db->quoteIdentifier('reported_by', true);
  913. $table = $this->_table['bugs'];
  914. $row = $table->fetchRow(array("$reported_by = ?" => 'goofy'), null, 1);
  915. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  916. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  917. $bug_id = $this->_db->foldCase('bug_id');
  918. $this->assertEquals(2, $row->$bug_id);
  919. }
  920. /**
  921. * @group ZF-8944
  922. * @group ZF-10598
  923. * @group ZF-11253
  924. */
  925. public function testTableFetchRowOffsetSelect()
  926. {
  927. $reported_by = $this->_db->quoteIdentifier('reported_by', true);
  928. $table = $this->_table['bugs'];
  929. $select = $table->select()
  930. ->where("$reported_by = ?", 'goofy')
  931. ->limit(1, 1);
  932. $row = $table->fetchRow($select);
  933. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  934. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  935. $bug_id = $this->_db->foldCase('bug_id');
  936. $this->assertEquals(2, $row->$bug_id);
  937. }
  938. public function testTableFetchRow()
  939. {
  940. $table = $this->_table['bugs'];
  941. $bug_description = $this->_db->foldCase('bug_description');
  942. $row = $table->fetchRow();
  943. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  944. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  945. $this->assertTrue(isset($row->$bug_description));
  946. }
  947. public function testTableFetchRowWhere()
  948. {
  949. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  950. $table = $this->_table['bugs'];
  951. $row = $table->fetchRow("$bug_id = 2");
  952. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  953. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  954. $bug_id = $this->_db->foldCase('bug_id');
  955. $this->assertEquals(2, $row->$bug_id);
  956. }
  957. public function testTableFetchRowWhereArray()
  958. {
  959. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  960. $table = $this->_table['bugs'];
  961. $row = $table->fetchRow(array("$bug_id = ?" => 2));
  962. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  963. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  964. $bug_id = $this->_db->foldCase('bug_id');
  965. $this->assertEquals(2, $row->$bug_id);
  966. }
  967. public function testTableFetchRowWhereSelect()
  968. {
  969. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  970. $table = $this->_table['bugs'];
  971. $select = $table->select()
  972. ->where("$bug_id = ?", 2);
  973. $row = $table->fetchRow($select);
  974. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  975. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  976. $bug_id = $this->_db->foldCase('bug_id');
  977. $this->assertEquals(2, $row->$bug_id);
  978. }
  979. public function testTableFetchRowOrderAsc()
  980. {
  981. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  982. $table = $this->_table['bugs'];
  983. $row = $table->fetchRow("$bug_id > 1", "bug_id ASC");
  984. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  985. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  986. $bug_id = $this->_db->foldCase('bug_id');
  987. $this->assertEquals(2, $row->$bug_id);
  988. }
  989. public function testTableFetchRowOrderSelectAsc()
  990. {
  991. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  992. $table = $this->_table['bugs'];
  993. $select = $table->select()
  994. ->where("$bug_id > ?", 1)
  995. ->order("bug_id ASC");
  996. $row = $table->fetchRow($select);
  997. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  998. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  999. $bug_id = $this->_db->foldCase('bug_id');
  1000. $this->assertEquals(2, $row->$bug_id);
  1001. }
  1002. public function testTableFetchRowOrderDesc()
  1003. {
  1004. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1005. $table = $this->_table['bugs'];
  1006. $row = $table->fetchRow(null, "bug_id DESC");
  1007. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  1008. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  1009. $bug_id = $this->_db->foldCase('bug_id');
  1010. $this->assertEquals(4, $row->$bug_id);
  1011. }
  1012. public function testTableFetchRowOrderSelectDesc()
  1013. {
  1014. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1015. $table = $this->_table['bugs'];
  1016. $select = $table->select()
  1017. ->where("$bug_id > ?", 1)
  1018. ->order("bug_id DESC");
  1019. $row = $table->fetchRow($select);
  1020. $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
  1021. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
  1022. $bug_id = $this->_db->foldCase('bug_id');
  1023. $this->assertEquals(4, $row->$bug_id);
  1024. }
  1025. public function testTableFetchRowEmpty()
  1026. {
  1027. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1028. $table = $this->_table['bugs'];
  1029. $row = $table->fetchRow("$bug_id = -1");
  1030. $this->assertEquals(null, $row,
  1031. 'Expecting null result for non-existent row');
  1032. }
  1033. public function testTableFetchAll()
  1034. {
  1035. $table = $this->_table['bugs'];
  1036. $rowset = $table->fetchAll();
  1037. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  1038. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  1039. $this->assertEquals(4, count($rowset));
  1040. $row1 = $rowset->current();
  1041. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  1042. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  1043. }
  1044. public function testTableFetchAllWhere()
  1045. {
  1046. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1047. $table = $this->_table['bugs'];
  1048. $rowset = $table->fetchAll("$bug_id = 2");
  1049. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  1050. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  1051. $this->assertEquals(1, count($rowset));
  1052. $row1 = $rowset->current();
  1053. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  1054. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  1055. $bug_id = $this->_db->foldCase('bug_id');
  1056. $this->assertEquals(2, $row1->$bug_id);
  1057. }
  1058. public function testTableFetchAllWhereSelect()
  1059. {
  1060. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1061. $table = $this->_table['bugs'];
  1062. $select = $table->select()
  1063. ->where("$bug_id = ?", 2);
  1064. $rowset = $table->fetchAll($select);
  1065. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  1066. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  1067. $this->assertEquals(1, count($rowset));
  1068. $row1 = $rowset->current();
  1069. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  1070. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  1071. $bug_id = $this->_db->foldCase('bug_id');
  1072. $this->assertEquals(2, $row1->$bug_id);
  1073. }
  1074. public function testTableFetchAllOrder()
  1075. {
  1076. $table = $this->_table['bugs'];
  1077. $rowset = $table->fetchAll(null, 'bug_id DESC');
  1078. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  1079. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  1080. $this->assertEquals(4, count($rowset));
  1081. $row1 = $rowset->current();
  1082. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  1083. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  1084. $bug_id = $this->_db->foldCase('bug_id');
  1085. $this->assertEquals(4, $row1->$bug_id);
  1086. }
  1087. public function testTableFetchAllOrderSelect()
  1088. {
  1089. $table = $this->_table['bugs'];
  1090. $select = $table->select()
  1091. ->order('bug_id DESC');
  1092. $rowset = $table->fetchAll($select);
  1093. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
  1094. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
  1095. $this->assertEquals(4, count($rowset));
  1096. $row1 = $rowset->current();
  1097. $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
  1098. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
  1099. $bug_id = $this->_db->foldCase('bug_id');
  1100. $this->assertEquals(4, $row1->$bug_id);
  1101. }
  1102. public function testTableFetchAllOrderExpr()
  1103. {
  1104. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1105. $table = $this->_table['bugs'];
  1106. $rowset = $table->fetchAll(null, new Zend_Db_Expr("$bug_id + 1 DESC"));
  1107. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
  1108. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1109. $this->assertEquals(4, count($rowset));
  1110. $row1 = $rowset->current();
  1111. $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
  1112. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1113. $bug_id = $this->_db->foldCase('bug_id');
  1114. $this->assertEquals(4, $row1->$bug_id);
  1115. }
  1116. public function testTableFetchAllLimit()
  1117. {
  1118. $table = $this->_table['bugs'];
  1119. $rowset = $table->fetchAll(null, 'bug_id ASC', 2, 1);
  1120. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
  1121. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1122. $this->assertEquals(2, count($rowset));
  1123. $row1 = $rowset->current();
  1124. $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
  1125. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1126. $bug_id = $this->_db->foldCase('bug_id');
  1127. $this->assertEquals(2, $row1->$bug_id);
  1128. }
  1129. public function testTableFetchAllLimitSelect()
  1130. {
  1131. $table = $this->_table['bugs'];
  1132. $select = $table->select()
  1133. ->order('bug_id ASC')
  1134. ->limit(2, 1);
  1135. $rowset = $table->fetchAll($select);
  1136. $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
  1137. 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
  1138. $this->assertEquals(2, count($rowset));
  1139. $row1 = $rowset->current();
  1140. $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
  1141. 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
  1142. $bug_id = $this->_db->foldCase('bug_id');
  1143. $this->assertEquals(2, $row1->$bug_id);
  1144. }
  1145. public function testTableFetchAllEmpty()
  1146. {
  1147. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  1148. $table = $this->_table['bugs'];
  1149. $rowset = $table->fetchAll("$bug_id = -1");
  1150. $this->assertEquals(0, count($rowset));
  1151. }
  1152. public function testTableLoadsCustomRowClass()
  1153. {
  1154. $this->_useMyIncludePath();
  1155. if (class_exists('My_ZendDbTable_Row_TestMyRow')) {
  1156. $this->markTestSkipped("Cannot test loading the custom Row class because it is already loaded");
  1157. return;
  1158. }
  1159. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1160. 'Expected TestMyRow class not to be loaded (#1)');
  1161. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1162. 'Expected TestMyRowset class not to be loaded (#1)');
  1163. // instantiating the table does not creat a rowset
  1164. // so the custom classes are not loaded yet
  1165. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1166. $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1167. 'Expected TestMyRow class not to be loaded (#2)');
  1168. $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1169. 'Expected TestMyRowset class not to be loaded (#2)');
  1170. // creating a rowset makes the table load the rowset class
  1171. // and the rowset constructor loads the row class.
  1172. $bugs = $bugsTable->fetchAll();
  1173. $this->assertTrue(class_exists('My_ZendDbTable_Row_TestMyRow', false),
  1174. 'Expected TestMyRow class to be loaded (#3)');
  1175. $this->assertTrue(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
  1176. 'Expected TestMyRowset class to be loaded (#3)');
  1177. }
  1178. /**
  1179. * Ensures that Zend_Db_Table_Abstract::setDefaultMetadataCache() performs as expected
  1180. *
  1181. * @return void
  1182. */
  1183. public function testTableSetDefaultMetadataCache()
  1184. {
  1185. $cache = $this->_getCache();
  1186. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1187. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1188. Zend_Db_Table_Abstract::setDefaultMetadataCache();
  1189. $this->assertNull(Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1190. }
  1191. public function testTableSetDefaultMetadataCacheRegistry()
  1192. {
  1193. $cache = $this->_getCache();
  1194. Zend_Registry::set('registered_metadata_cache', $cache);
  1195. Zend_Db_Table_Abstract::setDefaultMetadataCache('registered_metadata_cache');
  1196. $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
  1197. }
  1198. public function testTableMetadataCacheRegistry()
  1199. {
  1200. $cache = $this->_getCache();
  1201. Zend_Registry::set('registered_metadata_cache', $cache);
  1202. $tableBugsCustom1 = $this->_getTable(
  1203. 'My_ZendDbTable_TableBugsCustom',
  1204. array('metadataCache' => 'registered_metadata_cache')
  1205. );
  1206. $this->assertSame($cache, $tableBugsCustom1->getMetadataCache());
  1207. }
  1208. public function testTableSetDefaultMetadataCacheWriteAccess()
  1209. {
  1210. $cache = $this->_getCacheNowrite();
  1211. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1212. try {
  1213. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  1214. $primary = $bugsTable->info(Zend_Db_Table_Abstract::PRIMARY);
  1215. $this->fail('Expected to catch PHPUnit_Framework_Error');
  1216. } catch (PHPUnit_Framework_Error $e) {
  1217. $this->assertEquals(E_USER_NOTICE, $e->getCode(), 'Error type not E_USER_NOTICE');
  1218. $this->assertEquals('Failed saving metadata to metadataCache', $e->getMessage());
  1219. }
  1220. Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
  1221. }
  1222. /**
  1223. * Ensures that table metadata caching works as expected when the cache object
  1224. * is set in the configuration for a new table object.
  1225. *
  1226. * @return void
  1227. */
  1228. public function testTableMetadataCacheNew()
  1229. {
  1230. $cache = $this->_getCache();
  1231. $tableBugsCustom1 = $this->_getTable(
  1232. 'My_ZendDbTable_TableBugsCustom',
  1233. array('metadataCache' => $cache)
  1234. );
  1235. $this->assertTrue(
  1236. $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
  1237. );
  1238. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1239. $tableBugsCustom1->setup();
  1240. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1241. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1242. $tableBugsCustom1->setup();
  1243. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache after cleaning');
  1244. }
  1245. /**
  1246. * Ensures that table metadata caching can be persistent in the object even
  1247. * after a flushed cache, if the setMetadataCacheInClass property is true.
  1248. *
  1249. * @group ZF-2510
  1250. * @return void
  1251. */
  1252. public function testTableMetadataCacheInClass()
  1253. {
  1254. $cache = $this->_getCache();
  1255. $tableBugsCustom1 = $this->_getTable(
  1256. 'My_ZendDbTable_TableBugsCustom',
  1257. array(
  1258. 'metadataCache' => $cache,
  1259. 'metadataCacheInClass' => true,
  1260. )
  1261. );
  1262. $this->assertTrue(
  1263. $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
  1264. );
  1265. $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
  1266. $tableBugsCustom1->setup();
  1267. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
  1268. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1269. $tableBugsCustom1->setup();
  1270. $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache after cleaning');
  1271. }
  1272. /**
  1273. * Ensures that table metadata caching works as expected when the default cache object
  1274. * is set for the abstract table class.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testTableMetadataCacheClass()
  1279. {
  1280. $cache = $this->_getCache();
  1281. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1282. $tableBugsCustom1 = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  1283. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1284. $this->assertTrue(
  1285. $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
  1286. );
  1287. $tableBugsCustom1->setup();
  1288. $this->assertTrue($tableBugsCustom1->isMetadataFromCache);
  1289. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  1290. $tableBugsCustom1->setup();
  1291. $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
  1292. }
  1293. public function testTableSetDefaultMetadataCacheException()
  1294. {
  1295. try {
  1296. Zend_Db_Table_Abstract::setDefaultMetadataCache(new stdClass());
  1297. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1298. } catch (Zend_Exception $e) {
  1299. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  1300. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1301. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1302. }
  1303. try {
  1304. Zend_Db_Table_Abstract::setDefaultMetadataCache(327);
  1305. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1306. } catch (Exception $e) {
  1307. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  1308. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1309. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1310. }
  1311. }
  1312. public function testTableMetadataCacheException()
  1313. {
  1314. Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
  1315. /**
  1316. * options array points 'metadataCache' to integer scalar
  1317. */
  1318. try {
  1319. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 327));
  1320. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1321. } catch (Zend_Exception $e) {
  1322. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  1323. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1324. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1325. }
  1326. /**
  1327. * options array points 'metadataCache' to Registry key containing integer scalar
  1328. */
  1329. Zend_Registry::set('registered_metadata_cache', 327);
  1330. try {
  1331. $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 'registered_metadata_cache'));
  1332. $this->fail('Expected to catch Zend_Db_Table_Exception');
  1333. } catch (Zend_Exception $e) {
  1334. $this->assertTrue($e instanceof Zend_Db_Table_Exception,
  1335. 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
  1336. $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
  1337. }
  1338. }
  1339. public function testTableCascadeUpdate()
  1340. {
  1341. $table = $this->_table['products'];
  1342. $row1 = $table->find(1)->current();
  1343. $rows1 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1344. $product_id = $this->_db->foldCase('product_id');
  1345. $row1->$product_id = 999999;
  1346. $row1->save();
  1347. $rows2 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  1348. $this->assertEquals(999999, $row1->$product_id);
  1349. $this->assertEquals(count($rows1), count($rows2));
  1350. // Test for 'false' value in cascade config
  1351. $bug_id = $this->_db->foldCase('bug_id');
  1352. $row2 = $rows2->current();
  1353. $row2->$bug_id = 999999;
  1354. $row2->save();
  1355. }
  1356. public function testTableCascadeDelete()
  1357. {
  1358. $table = $this->_table['products'];
  1359. $row1 = $table->find(2)->current();
  1360. $row1->delete();
  1361. // Test for 'false' value in cascade config
  1362. $table = $this->_table['bugs'];
  1363. $row2 = $table->find(1)->current();
  1364. $row2->delete();
  1365. $table = $this->_table['bugs_products'];
  1366. $product_id = $this->_db->quoteIdentifier('product_id', true);
  1367. $select = $table->select()
  1368. ->where($product_id . ' = ?', 2);
  1369. $rows = $table->fetchAll($select);
  1370. $this->assertEquals(0, count($rows));
  1371. }
  1372. /**
  1373. * @group ZF-1103
  1374. */
  1375. public function testTableCascadeRecurseDelete()
  1376. {
  1377. $tblRecursive = $this->_getTable('My_ZendDbTable_TableCascadeRecursive');
  1378. // Enforce initial table structure
  1379. $parentRow = $tblRecursive->find(1)->current();
  1380. $this->assertTrue($parentRow instanceof Zend_Db_Table_Row);
  1381. $childRows = $parentRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
  1382. $this->assertTrue($childRows instanceof Zend_Db_Table_Rowset);
  1383. $this->assertEquals(2, count($childRows));
  1384. foreach ( $childRows as $childRow ) {
  1385. $this->assertTrue($childRow instanceof Zend_Db_Table_Row);
  1386. $subChildRows = $childRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
  1387. $this->assertTrue($subChildRows instanceof Zend_Db_Table_Rowset);
  1388. $this->assertEquals( $childRow['item_id'] == 3 ? 2 : 0 , count($subChildRows));
  1389. }
  1390. // Perform the delete
  1391. $parentRow->delete();
  1392. // Assert that all children of #1 (2,3,4,5) are removed recursively
  1393. $this->assertNull($tblRecursive->find(1)->current());
  1394. $this->assertNull($tblRecursive->find(2)->current());
  1395. $this->assertNull($tblRecursive->find(3)->current());
  1396. $this->assertNull($tblRecursive->find(4)->current());
  1397. $this->assertNull($tblRecursive->find(5)->current());
  1398. //... but #6 remains
  1399. $this->assertTrue($tblRecursive->find(6)->current() instanceof Zend_Db_Table_Row);
  1400. }
  1401. /**
  1402. * @group ZF-11810
  1403. */
  1404. public function testTableCascadeRecurseDeleteUsingTableDeleteMethod()
  1405. {
  1406. $tblRecursive = $this->_getTable('My_ZendDbTable_TableCascadeRecursive');
  1407. // Enforce initial table structure
  1408. $parentRow = $tblRecursive->find(1)->current();
  1409. $this->assertTrue($parentRow instanceof Zend_Db_Table_Row);
  1410. $childRows = $parentRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
  1411. $this->assertTrue($childRows instanceof Zend_Db_Table_Rowset);
  1412. $this->assertEquals(2, count($childRows));
  1413. foreach ( $childRows as $childRow ) {
  1414. $this->assertTrue($childRow instanceof Zend_Db_Table_Row);
  1415. $subChildRows = $childRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
  1416. $this->assertTrue($subChildRows instanceof Zend_Db_Table_Rowset);
  1417. $this->assertEquals( $childRow['item_id'] == 3 ? 2 : 0 , count($subChildRows));
  1418. }
  1419. // Perform the delete
  1420. $tblRecursive->delete($tblRecursive->getAdapter()->quoteInto('item_id = ?', 1));
  1421. // Assert that all children of #1 (2,3,4,5) are removed recursively
  1422. $this->assertNull($tblRecursive->find(1)->current());
  1423. $this->assertNull($tblRecursive->find(2)->current());
  1424. $this->assertNull($tblRecursive->find(3)->current());
  1425. $this->assertNull($tblRecursive->find(4)->current());
  1426. $this->assertNull($tblRecursive->find(5)->current());
  1427. //... but #6 remains
  1428. $this->assertTrue($tblRecursive->find(6)->current() instanceof Zend_Db_Table_Row);
  1429. }
  1430. public function testSerialiseTable()
  1431. {
  1432. $table = $this->_table['products'];
  1433. $this->assertTrue(is_string(serialize($table)));
  1434. }
  1435. /**
  1436. * @group ZF-1343
  1437. */
  1438. public function testTableFetchallCanHandleWhereWithParameritizationCharacters()
  1439. {
  1440. $product_name = $this->_db->quoteIdentifier('product_name');
  1441. $table = $this->_table['products'];
  1442. $where = $table->getAdapter()->quoteInto("$product_name = ?", "some?product's");
  1443. $rows = $table->fetchAll($where);
  1444. $this->assertEquals(0, count($rows));
  1445. }
  1446. /**
  1447. * @group ZF-3486
  1448. */
  1449. public function testTableConcreteInstantiation()
  1450. {
  1451. Zend_Db_Table::setDefaultAdapter($this->_db);
  1452. $table = new Zend_Db_Table('zfbugs');
  1453. $rowset = $table->find(1);
  1454. $this->assertEquals(1, count($rowset));
  1455. Zend_Db_Table::setDefaultAdapter();
  1456. $table = new Zend_Db_Table(array(
  1457. 'name' => 'zfbugs',
  1458. 'db' => $this->_db
  1459. ));
  1460. $rowset = $table->find(1);
  1461. $this->assertEquals(1, count($rowset));
  1462. }
  1463. /**
  1464. * Returns a clean Zend_Cache_Core with File backend
  1465. *
  1466. * @return Zend_Cache_Core
  1467. */
  1468. protected function _getCache()
  1469. {
  1470. /**
  1471. * @see Zend_Cache
  1472. */
  1473. require_once 'Zend/Cache.php';
  1474. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'cachefiles';
  1475. $frontendOptions = array(
  1476. 'automatic_serialization' => true
  1477. );
  1478. $backendOptions = array(
  1479. 'cache_dir' => $folder,
  1480. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1481. );
  1482. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1483. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1484. return $cacheFrontend;
  1485. }
  1486. /**
  1487. * Returns a clean Zend_Cache_Core with File backend
  1488. *
  1489. * @return Zend_Cache_Core
  1490. */
  1491. protected function _getCacheNowrite()
  1492. {
  1493. /**
  1494. * @see Zend_Cache
  1495. */
  1496. require_once 'Zend/Cache.php';
  1497. $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'nofiles';
  1498. if (!file_exists($folder)) {
  1499. mkdir($folder, 0777);
  1500. }
  1501. $frontendOptions = array(
  1502. 'automatic_serialization' => true
  1503. );
  1504. $backendOptions = array(
  1505. 'cache_dir' => $folder,
  1506. 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
  1507. );
  1508. $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
  1509. $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
  1510. rmdir($folder);
  1511. return $cacheFrontend;
  1512. }
  1513. /**
  1514. * @group ZF-5674
  1515. */
  1516. public function testTableAndIdentityWithVeryLongName()
  1517. {
  1518. Zend_Db_Table::setDefaultAdapter($this->_db);
  1519. // create test table using no identifier quoting
  1520. $this->_util->createTable('thisisaveryverylongtablename', array(
  1521. 'thisisalongtablenameidentity' => 'IDENTITY',
  1522. 'stuff' => 'VARCHAR(32)'
  1523. ));
  1524. $tableName = $this->_util->getTableName('thisisaveryverylongtablename');
  1525. $table = new Zend_Db_Table('thisisaveryverylongtablename');
  1526. $row = $table->createRow($this->_getRowForTableAndIdentityWithVeryLongName());
  1527. $row->save();
  1528. $rowset = $table->find(1);
  1529. $this->assertEquals(1, count($rowset));
  1530. $this->_util->dropTable('thisisaveryverylongtablename');
  1531. }
  1532. protected function _getRowForTableAndIdentityWithVeryLongName()
  1533. {
  1534. return array('stuff' => 'information');
  1535. }
  1536. /**
  1537. * @group ZF-7042
  1538. * @group ZF-10778
  1539. */
  1540. public function testCacheIdGeneratedToMetadata()
  1541. {
  1542. /**
  1543. * @see Zend_Cache
  1544. */
  1545. require_once 'Zend/Cache.php';
  1546. /**
  1547. * @see Zend_Cache_Backend_BlackHole
  1548. */
  1549. require_once 'Zend/Cache/Backend/BlackHole.php';
  1550. Zend_Db_Table::setDefaultAdapter($this->_db);
  1551. $dbConfig = $this->_db->getConfig();
  1552. $cacheId = md5(
  1553. (isset($dbConfig['port']) ? ':'.$dbConfig['port'] : null)
  1554. . (isset($dbConfig['host']) ? ':'.$dbConfig['host'] : null)
  1555. . '/'.$dbConfig['dbname'].':.cache_metadata'
  1556. );
  1557. $metadata = array('id' => array('PRIMARY' => true));
  1558. $cacheBackend = $this->getMock('Zend_Cache_Backend_BlackHole');
  1559. $cacheBackend->expects($this->any())
  1560. ->method('load')
  1561. ->with($this->equalTo($cacheId))
  1562. ->will($this->returnValue($metadata));
  1563. $cache = Zend_Cache::factory('Core', $cacheBackend, array('automatic_serialization' => false));
  1564. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  1565. $this->_util->createTable('cache_metadata', array(
  1566. 'id' => 'IDENTITY',
  1567. 'name' => 'VARCHAR(32)'
  1568. ));
  1569. $configTable = array(
  1570. 'name' => 'cache_metadata',
  1571. 'primary' => 'id'
  1572. );
  1573. $table = new Zend_Db_Table($configTable);
  1574. $table->info(Zend_Db_Table::METADATA);
  1575. $this->_util->dropTable('cache_metadata');
  1576. Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
  1577. }
  1578. }