TestCommon.php 57 KB

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