TestCommon.php 59 KB

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