TestCommon.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. */
  21. require_once 'Zend/Db/TestSetup.php';
  22. require_once 'Zend/Db/Statement/Exception.php';
  23. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  24. abstract class Zend_Db_Statement_TestCommon extends Zend_Db_TestSetup
  25. {
  26. public function testStatementConstruct()
  27. {
  28. $statementClass = 'Zend_Db_Statement_' . $this->getDriver();
  29. $select = $this->_db->select()
  30. ->from('zfproducts');
  31. $sql = $select->__toString();
  32. $stmt = new $statementClass($this->_db, $sql);
  33. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  34. $stmt->closeCursor();
  35. }
  36. public function testStatementConstructWithSelectObject()
  37. {
  38. $statementClass = 'Zend_Db_Statement_' . $this->getDriver();
  39. $select = $this->_db->select()
  40. ->from('zfproducts');
  41. $stmt = new $statementClass($this->_db, $select);
  42. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  43. $stmt->closeCursor();
  44. }
  45. public function testStatementConstructFromPrepare()
  46. {
  47. $select = $this->_db->select()
  48. ->from('zfproducts');
  49. $stmt = $this->_db->prepare($select->__toString());
  50. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  51. $stmt->closeCursor();
  52. }
  53. public function testStatementConstructFromQuery()
  54. {
  55. $select = $this->_db->select()
  56. ->from('zfproducts');
  57. $stmt = $this->_db->query($select);
  58. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  59. $stmt->closeCursor();
  60. }
  61. public function testStatementConstructFromSelect()
  62. {
  63. $stmt = $this->_db->select()
  64. ->from('zfproducts')
  65. ->query();
  66. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  67. $stmt->closeCursor();
  68. }
  69. public function testStatementConstructExceptionBadSql()
  70. {
  71. $sql = "SELECT * FROM *";
  72. try {
  73. $stmt = $this->_db->query($sql);
  74. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  75. } catch (Zend_Exception $e) {
  76. $this->assertType('Zend_Db_Statement_Exception', $e,
  77. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  78. }
  79. }
  80. public function testStatementRowCount()
  81. {
  82. $products = $this->_db->quoteIdentifier('zfproducts');
  83. $product_id = $this->_db->quoteIdentifier('product_id');
  84. $stmt = $this->_db->prepare("DELETE FROM $products WHERE $product_id = 1");
  85. $n = $stmt->rowCount();
  86. $this->assertType('integer', $n);
  87. $this->assertEquals(0, $n, 'Expecting row count to be 0 before executing query');
  88. $stmt->execute();
  89. $n = $stmt->rowCount();
  90. $stmt->closeCursor();
  91. $this->assertType('integer', $n);
  92. $this->assertEquals(1, $n, 'Expected row count to be one after executing query');
  93. }
  94. public function testStatementColumnCountForSelect()
  95. {
  96. $select = $this->_db->select()
  97. ->from('zfproducts');
  98. $stmt = $this->_db->prepare($select->__toString());
  99. $n = $stmt->columnCount();
  100. $this->assertEquals(0, $n, 'Expecting column count to be 0 before executing query');
  101. $stmt->execute();
  102. $n = $stmt->columnCount();
  103. $stmt->closeCursor();
  104. $this->assertType('integer', $n);
  105. $this->assertEquals(2, $n);
  106. }
  107. public function testStatementColumnCountForDelete()
  108. {
  109. $products = $this->_db->quoteIdentifier('zfproducts');
  110. $product_id = $this->_db->quoteIdentifier('product_id');
  111. $stmt = $this->_db->prepare("DELETE FROM $products WHERE $product_id = 1");
  112. $n = $stmt->columnCount();
  113. $this->assertEquals(0, $n, 'Expecting column count to be 0 before executing query');
  114. $stmt->execute();
  115. $n = $stmt->columnCount();
  116. $this->assertEquals(0, $n, 'Expecting column count to be null after executing query');
  117. }
  118. public function testStatementExecuteWithParams()
  119. {
  120. $products = $this->_db->quoteIdentifier('zfproducts');
  121. $product_id = $this->_db->quoteIdentifier('product_id');
  122. $product_name = $this->_db->quoteIdentifier('product_name');
  123. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (?, ?)");
  124. $stmt->execute(array(4, 'Solaris'));
  125. $select = $this->_db->select()
  126. ->from('zfproducts')
  127. ->where("$product_id = 4");
  128. $result = $this->_db->fetchAll($select);
  129. $stmt->closeCursor();
  130. $this->assertEquals(array(array('product_id'=>4, 'product_name'=>'Solaris')), $result);
  131. }
  132. public function testStatementErrorCodeKeyViolation()
  133. {
  134. $products = $this->_db->quoteIdentifier('zfproducts');
  135. $product_id = $this->_db->quoteIdentifier('product_id');
  136. $product_name = $this->_db->quoteIdentifier('product_name');
  137. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (?, ?)");
  138. try {
  139. // INSERT a value that results in a key violation
  140. $retval = $stmt->execute(array(1, 'Solaris'));
  141. if ($retval === false) {
  142. throw new Zend_Db_Statement_Exception('dummy');
  143. }
  144. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  145. } catch (Zend_Exception $e) {
  146. $this->assertType('Zend_Db_Statement_Exception', $e,
  147. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  148. }
  149. $code = $stmt->errorCode();
  150. // @todo what to assert here?
  151. }
  152. public function testStatementErrorInfoKeyViolation()
  153. {
  154. $products = $this->_db->quoteIdentifier('zfproducts');
  155. $product_id = $this->_db->quoteIdentifier('product_id');
  156. $product_name = $this->_db->quoteIdentifier('product_name');
  157. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (?, ?)");
  158. try {
  159. // INSERT a value that results in a key violation
  160. $retval = $stmt->execute(array(1, 'Solaris'));
  161. if ($retval === false) {
  162. throw new Zend_Db_Statement_Exception('dummy');
  163. }
  164. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  165. } catch (Zend_Exception $e) {
  166. $this->assertType('Zend_Db_Statement_Exception', $e,
  167. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  168. }
  169. $code = $stmt->errorCode();
  170. $info = $stmt->errorInfo();
  171. $this->assertEquals($code, $info[0]);
  172. // @todo what to assert here?
  173. }
  174. public function testStatementSetFetchModeAssoc()
  175. {
  176. $products = $this->_db->quoteIdentifier('zfproducts');
  177. $product_id = $this->_db->quoteIdentifier('product_id');
  178. // set the adapter fetch mode to something different
  179. $this->_db->setFetchMode(Zend_Db::FETCH_BOTH);
  180. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  181. $stmt->setFetchMode(Zend_Db::FETCH_ASSOC);
  182. $result = $stmt->fetchAll();
  183. $this->assertEquals(2, count($result));
  184. $this->assertEquals(2, count($result[0]));
  185. // check for FETCH_ASSOC entries
  186. $this->assertEquals(2, $result[0]['product_id']);
  187. $this->assertEquals('Linux', $result[0]['product_name']);
  188. // check FETCH_NUM entries
  189. $this->assertFalse(isset($result[0][0]));
  190. $this->assertFalse(isset($result[0][1]));
  191. }
  192. public function testStatementSetFetchModeNum()
  193. {
  194. $products = $this->_db->quoteIdentifier('zfproducts');
  195. $product_id = $this->_db->quoteIdentifier('product_id');
  196. // set the adapter fetch mode to something different
  197. $this->_db->setFetchMode(Zend_Db::FETCH_BOTH);
  198. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  199. $stmt->setFetchMode(Zend_Db::FETCH_NUM);
  200. $result = $stmt->fetchAll();
  201. $this->assertEquals(2, count($result));
  202. $this->assertEquals(2, count($result[0]));
  203. // check for FETCH_ASSOC entries
  204. $this->assertFalse(isset($result[0]['product_id']));
  205. $this->assertFalse(isset($result[0]['product_name']));
  206. // check FETCH_NUM entries
  207. $this->assertEquals(2, $result[0][0]);
  208. $this->assertEquals('Linux', $result[0][1]);
  209. }
  210. public function testStatementSetFetchModeBoth()
  211. {
  212. $products = $this->_db->quoteIdentifier('zfproducts');
  213. $product_id = $this->_db->quoteIdentifier('product_id');
  214. // set the adapter fetch mode to something different
  215. $this->_db->setFetchMode(Zend_Db::FETCH_ASSOC);
  216. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  217. $stmt->setFetchMode(Zend_Db::FETCH_BOTH);
  218. $result = $stmt->fetchAll();
  219. $this->assertEquals(2, count($result));
  220. $this->assertEquals(4, count($result[0]));
  221. // check for FETCH_ASSOC entries
  222. $this->assertEquals(2, $result[0]['product_id']);
  223. $this->assertEquals('Linux', $result[0]['product_name']);
  224. // check FETCH_NUM entries
  225. $this->assertEquals(2, $result[0][0]);
  226. $this->assertEquals('Linux', $result[0][1]);
  227. }
  228. public function testStatementSetFetchModeObj()
  229. {
  230. $products = $this->_db->quoteIdentifier('zfproducts');
  231. $product_id = $this->_db->quoteIdentifier('product_id');
  232. // set the adapter fetch mode to something different
  233. $this->_db->setFetchMode(Zend_Db::FETCH_BOTH);
  234. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  235. $stmt->setFetchMode(Zend_Db::FETCH_OBJ);
  236. $result = $stmt->fetchAll();
  237. $this->assertEquals(2, count($result));
  238. $this->assertType('stdClass', $result[0]);
  239. // check for FETCH_OBJ entries
  240. $this->assertEquals(2, $result[0]->product_id);
  241. $this->assertEquals('Linux', $result[0]->product_name);
  242. }
  243. public function testStatementSetFetchModeInvalidException()
  244. {
  245. $products = $this->_db->quoteIdentifier('zfproducts');
  246. $product_id = $this->_db->quoteIdentifier('product_id');
  247. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  248. try {
  249. // invalid value
  250. $stmt->setFetchMode(-999);
  251. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  252. } catch (Zend_Exception $e) {
  253. $this->assertType('Zend_Db_Statement_Exception', $e,
  254. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  255. $this->assertContains('invalid fetch mode', $e->getMessage());
  256. }
  257. }
  258. public function testStatementFetchAll()
  259. {
  260. $products = $this->_db->quoteIdentifier('zfproducts');
  261. $product_id = $this->_db->quoteIdentifier('product_id');
  262. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  263. $result = $stmt->fetchAll();
  264. $this->assertEquals(2, count($result));
  265. $this->assertEquals(2, count($result[0]));
  266. $this->assertEquals(2, $result[0]['product_id']);
  267. $this->assertFalse(isset($result[0][0]));
  268. }
  269. public function testStatementFetchAllStyleNum()
  270. {
  271. $products = $this->_db->quoteIdentifier('zfproducts');
  272. $product_id = $this->_db->quoteIdentifier('product_id');
  273. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  274. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  275. $this->assertEquals(2, count($result));
  276. $this->assertEquals(2, count($result[0]));
  277. $this->assertEquals(2, $result[0][0]);
  278. $this->assertEquals('Linux', $result[0][1]);
  279. $this->assertFalse(isset($result[0]['product_id']));
  280. }
  281. public function testStatementFetchAllStyleAssoc()
  282. {
  283. $products = $this->_db->quoteIdentifier('zfproducts');
  284. $product_id = $this->_db->quoteIdentifier('product_id');
  285. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  286. $result = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  287. $this->assertEquals(2, count($result));
  288. $this->assertEquals(2, count($result[0]));
  289. $this->assertEquals(2, $result[0]['product_id']);
  290. $this->assertFalse(isset($result[0][0]));
  291. }
  292. public function testStatementFetchAllStyleBoth()
  293. {
  294. $products = $this->_db->quoteIdentifier('zfproducts');
  295. $product_id = $this->_db->quoteIdentifier('product_id');
  296. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  297. $result = $stmt->fetchAll(Zend_Db::FETCH_BOTH);
  298. $this->assertEquals(2, count($result));
  299. $this->assertEquals(4, count($result[0]));
  300. $this->assertEquals(2, $result[0][0]);
  301. $this->assertEquals('Linux', $result[0][1]);
  302. $this->assertEquals(2, $result[0]['product_id']);
  303. $this->assertEquals('Linux', $result[0]['product_name']);
  304. }
  305. public function testStatementFetchAllStyleObj()
  306. {
  307. $products = $this->_db->quoteIdentifier('zfproducts');
  308. $product_id = $this->_db->quoteIdentifier('product_id');
  309. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  310. $result = $stmt->fetchAll(Zend_Db::FETCH_OBJ);
  311. $this->assertEquals(2, count($result));
  312. $this->assertType('stdClass', $result[0]);
  313. $this->assertEquals(2, $result[0]->product_id);
  314. }
  315. public function testStatementFetchAllStyleColumn()
  316. {
  317. $products = $this->_db->quoteIdentifier('zfproducts');
  318. $product_id = $this->_db->quoteIdentifier('product_id');
  319. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  320. $result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
  321. $this->assertEquals(2, count($result));
  322. $this->assertEquals(2, $result[0]);
  323. $this->assertEquals(3, $result[1]);
  324. }
  325. public function testStatementFetchAllStyleColumnWithArg()
  326. {
  327. $products = $this->_db->quoteIdentifier('zfproducts');
  328. $product_id = $this->_db->quoteIdentifier('product_id');
  329. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  330. $result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN, 1);
  331. $this->assertEquals(2, count($result));
  332. $this->assertType('string', $result[0]);
  333. $this->assertEquals('Linux', $result[0]);
  334. $this->assertEquals('OS X', $result[1]);
  335. }
  336. public function testStatementFetchAllStyleException()
  337. {
  338. $products = $this->_db->quoteIdentifier('zfproducts');
  339. $product_id = $this->_db->quoteIdentifier('product_id');
  340. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  341. try {
  342. $result = $stmt->fetchAll(-99);
  343. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  344. } catch (Zend_Exception $e) {
  345. $this->assertType('Zend_Db_Statement_Exception', $e,
  346. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  347. }
  348. $stmt->closeCursor();
  349. }
  350. public function testStatementFetchColumn()
  351. {
  352. $products = $this->_db->quoteIdentifier('zfproducts');
  353. $product_id = $this->_db->quoteIdentifier('product_id');
  354. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  355. $result = $stmt->fetchColumn();
  356. $this->assertEquals(2, $result);
  357. $result = $stmt->fetchColumn();
  358. $this->assertEquals(3, $result);
  359. $stmt->closeCursor();
  360. }
  361. public function testStatementFetchColumnEmptyResult()
  362. {
  363. $products = $this->_db->quoteIdentifier('zfproducts');
  364. $product_id = $this->_db->quoteIdentifier('product_id');
  365. // query that is known to return zero rows
  366. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id < 1 ORDER BY $product_id ASC");
  367. $result = $stmt->fetchColumn();
  368. $stmt->closeCursor();
  369. $this->assertFalse($result);
  370. }
  371. public function testStatementFetchColumnWithArg()
  372. {
  373. $products = $this->_db->quoteIdentifier('zfproducts');
  374. $product_id = $this->_db->quoteIdentifier('product_id');
  375. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  376. $result = $stmt->fetchColumn(1);
  377. $this->assertEquals('Linux', $result);
  378. $result = $stmt->fetchColumn(1);
  379. $this->assertEquals('OS X', $result);
  380. $stmt->closeCursor();
  381. }
  382. public function testStatementFetchObject()
  383. {
  384. $products = $this->_db->quoteIdentifier('zfproducts');
  385. $product_id = $this->_db->quoteIdentifier('product_id');
  386. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  387. $result = $stmt->fetchObject();
  388. $stmt->closeCursor();
  389. $this->assertType('stdClass', $result,
  390. 'Expecting object of type stdClass, got '.get_class($result));
  391. $this->assertEquals('Linux', $result->product_name);
  392. }
  393. public function testStatementFetchObjectEmptyResult()
  394. {
  395. $products = $this->_db->quoteIdentifier('zfproducts');
  396. $product_id = $this->_db->quoteIdentifier('product_id');
  397. // query that is known to return zero rows
  398. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id < 1 ORDER BY $product_id ASC");
  399. $result = $stmt->fetchObject();
  400. $stmt->closeCursor();
  401. $this->assertFalse($result);
  402. }
  403. public function testStatementFetchStyleNum()
  404. {
  405. $products = $this->_db->quoteIdentifier('zfproducts');
  406. $product_id = $this->_db->quoteIdentifier('product_id');
  407. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  408. $result = $stmt->fetch(Zend_Db::FETCH_NUM);
  409. $stmt->closeCursor();
  410. $this->assertType('array', $result);
  411. $this->assertEquals('Linux', $result[1]);
  412. $this->assertFalse(isset($result['product_name']));
  413. }
  414. public function testStatementFetchStyleAssoc()
  415. {
  416. $products = $this->_db->quoteIdentifier('zfproducts');
  417. $product_id = $this->_db->quoteIdentifier('product_id');
  418. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  419. $result = $stmt->fetch(Zend_Db::FETCH_ASSOC);
  420. $stmt->closeCursor();
  421. $this->assertType('array', $result);
  422. $this->assertEquals('Linux', $result['product_name']);
  423. $this->assertFalse(isset($result[1]));
  424. }
  425. public function testStatementFetchStyleBoth()
  426. {
  427. $products = $this->_db->quoteIdentifier('zfproducts');
  428. $product_id = $this->_db->quoteIdentifier('product_id');
  429. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  430. $result = $stmt->fetch(Zend_Db::FETCH_BOTH);
  431. $stmt->closeCursor();
  432. $this->assertType('array', $result);
  433. $this->assertEquals('Linux', $result[1]);
  434. $this->assertEquals('Linux', $result['product_name']);
  435. }
  436. public function testStatementFetchStyleObj()
  437. {
  438. $products = $this->_db->quoteIdentifier('zfproducts');
  439. $product_id = $this->_db->quoteIdentifier('product_id');
  440. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  441. $result = $stmt->fetch(Zend_Db::FETCH_OBJ);
  442. $stmt->closeCursor();
  443. $this->assertType('stdClass', $result,
  444. 'Expecting object of type stdClass, got '.get_class($result));
  445. $this->assertEquals('Linux', $result->product_name);
  446. }
  447. public function testStatementFetchStyleException()
  448. {
  449. $products = $this->_db->quoteIdentifier('zfproducts');
  450. $product_id = $this->_db->quoteIdentifier('product_id');
  451. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  452. try {
  453. $result = $stmt->fetch(-99);
  454. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  455. } catch (Zend_Exception $e) {
  456. $this->assertType('Zend_Db_Statement_Exception', $e,
  457. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  458. }
  459. $stmt->closeCursor();
  460. }
  461. public function testStatementBindParamByPosition()
  462. {
  463. $products = $this->_db->quoteIdentifier('zfproducts');
  464. $product_id = $this->_db->quoteIdentifier('product_id');
  465. $product_name = $this->_db->quoteIdentifier('product_name');
  466. $productIdValue = 4;
  467. $productNameValue = 'AmigaOS';
  468. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (?, ?)");
  469. $this->assertTrue($stmt->bindParam(1, $productIdValue), 'Expected bindParam(1) to return true');
  470. $this->assertTrue($stmt->bindParam(2, $productNameValue), 'Expected bindParam(2) to return true');
  471. // we should be able to set the values after binding them
  472. $productIdValue = 4;
  473. $productNameValue = 'Solaris';
  474. // no params as args to execute()
  475. $this->assertTrue($stmt->execute(), 'Expected execute() to return true');
  476. $select = $this->_db->select()
  477. ->from('zfproducts')
  478. ->where("$product_id = 4");
  479. $result = $this->_db->fetchAll($select);
  480. $this->assertEquals(array(array('product_id' => $productIdValue, 'product_name' => $productNameValue)), $result);
  481. }
  482. public function testStatementBindParamByName()
  483. {
  484. $products = $this->_db->quoteIdentifier('zfproducts');
  485. $product_id = $this->_db->quoteIdentifier('product_id');
  486. $product_name = $this->_db->quoteIdentifier('product_name');
  487. $productIdValue = 4;
  488. $productNameValue = 'AmigaOS';
  489. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  490. // test with colon prefix
  491. $this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
  492. // test with no colon prefix
  493. $this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
  494. // we should be able to set the values after binding them
  495. $productIdValue = 4;
  496. $productNameValue = 'Solaris';
  497. // no params as args to execute()
  498. $this->assertTrue($stmt->execute(), 'Expected execute() to return true');
  499. $select = $this->_db->select()
  500. ->from('zfproducts')
  501. ->where("$product_id = 4");
  502. $result = $this->_db->fetchAll($select);
  503. $stmt->closeCursor();
  504. $this->assertEquals(array(array('product_id' => $productIdValue, 'product_name' => $productNameValue)), $result);
  505. }
  506. public function testStatementBindValueByPosition()
  507. {
  508. $products = $this->_db->quoteIdentifier('zfproducts');
  509. $product_id = $this->_db->quoteIdentifier('product_id');
  510. $product_name = $this->_db->quoteIdentifier('product_name');
  511. $productIdValue = 4;
  512. $productNameValue = 'AmigaOS';
  513. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (?, ?)");
  514. $this->assertTrue($stmt->bindValue(1, $productIdValue), 'Expected bindValue(1) to return true');
  515. $this->assertTrue($stmt->bindValue(2, $productNameValue), 'Expected bindValue(2) to return true');
  516. // we should be able to change the values without changing what gets inserted
  517. $productIdValue = 5;
  518. $productNameValue = 'Solaris';
  519. // no params as args to execute()
  520. $this->assertTrue($stmt->execute(), 'Expected execute() to return true');
  521. $select = $this->_db->select()
  522. ->from('zfproducts')
  523. ->where("$product_id >= 4");
  524. $result = $this->_db->fetchAll($select);
  525. $stmt->closeCursor();
  526. $this->assertEquals(array(array('product_id' => '4', 'product_name' => 'AmigaOS')), $result);
  527. }
  528. public function testStatementBindValueByName()
  529. {
  530. $products = $this->_db->quoteIdentifier('zfproducts');
  531. $product_id = $this->_db->quoteIdentifier('product_id');
  532. $product_name = $this->_db->quoteIdentifier('product_name');
  533. $productIdValue = 4;
  534. $productNameValue = 'AmigaOS';
  535. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  536. // test with colon prefix
  537. $this->assertTrue($stmt->bindValue(':id', $productIdValue), 'Expected bindValue(\':id\') to return true');
  538. // test with no colon prefix
  539. $this->assertTrue($stmt->bindValue('name', $productNameValue), 'Expected bindValue(\'name\') to return true');
  540. // we should be able to change the values without changing what gets inserted
  541. $productIdValue = 5;
  542. $productNameValue = 'Solaris';
  543. // no params as args to execute()
  544. $this->assertTrue($stmt->execute(), 'Expected execute() to return true');
  545. $select = $this->_db->select()
  546. ->from('zfproducts')
  547. ->where("$product_id >= 4");
  548. $result = $this->_db->fetchAll($select);
  549. $stmt->closeCursor();
  550. $this->assertEquals(array(array('product_id' => '4', 'product_name' => 'AmigaOS')), $result);
  551. }
  552. public function testStatementBindColumnByPosition()
  553. {
  554. $products = $this->_db->quoteIdentifier('zfproducts');
  555. $product_id = $this->_db->quoteIdentifier('product_id');
  556. $prodIdValue = -99;
  557. $prodNameValue = 'AmigaOS';
  558. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  559. $this->assertTrue($stmt->bindColumn(1, $prodIdValue),
  560. 'Expected bindColumn(product_id) to return true');
  561. $this->assertTrue($stmt->bindColumn(2, $prodNameValue),
  562. 'Expected bindColumn(product_name) to return true');
  563. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  564. 'Expected fetch() call 1 to return true');
  565. $this->assertEquals(2, $prodIdValue);
  566. $this->assertEquals('Linux', $prodNameValue);
  567. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  568. 'Expected fetch() call 2 to return true');
  569. $this->assertEquals(3, $prodIdValue);
  570. $this->assertEquals('OS X', $prodNameValue);
  571. $stmt->closeCursor();
  572. }
  573. public function testStatementBindColumnByName()
  574. {
  575. $products = $this->_db->quoteIdentifier('zfproducts');
  576. $product_id = $this->_db->quoteIdentifier('product_id');
  577. $prodIdValue = -99;
  578. $prodNameValue = 'AmigaOS';
  579. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  580. $this->assertTrue($stmt->bindColumn('product_id', $prodIdValue),
  581. 'Expected bindColumn(product_id) to return true');
  582. $this->assertTrue($stmt->bindColumn('product_name', $prodNameValue),
  583. 'Expected bindColumn(product_name) to return true');
  584. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  585. 'Expected fetch() call 1 to return true');
  586. $this->assertEquals(2, $prodIdValue);
  587. $this->assertEquals('Linux', $prodNameValue);
  588. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  589. 'Expected fetch() call 2 to return true');
  590. $this->assertEquals(3, $prodIdValue);
  591. $this->assertEquals('OS X', $prodNameValue);
  592. $stmt->closeCursor();
  593. }
  594. public function testStatementBindColumnByPositionAndName()
  595. {
  596. $products = $this->_db->quoteIdentifier('zfproducts');
  597. $product_id = $this->_db->quoteIdentifier('product_id');
  598. $prodIdValue = -99;
  599. $prodNameValue = 'AmigaOS';
  600. $stmt = $this->_db->query("SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC");
  601. $this->assertTrue($stmt->bindColumn(1, $prodIdValue),
  602. 'Expected bindColumn(1) to return true');
  603. $this->assertTrue($stmt->bindColumn('product_name', $prodNameValue),
  604. 'Expected bindColumn(product_name) to return true');
  605. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  606. 'Expected fetch() call 1 to return true');
  607. $this->assertEquals(2, $prodIdValue);
  608. $this->assertEquals('Linux', $prodNameValue);
  609. $this->assertTrue($stmt->fetch(Zend_Db::FETCH_BOUND),
  610. 'Expected fetch() call 2 to return true');
  611. $this->assertEquals(3, $prodIdValue);
  612. $this->assertEquals('OS X', $prodNameValue);
  613. $stmt->closeCursor();
  614. }
  615. protected $_getColumnMetaKeys = array(
  616. 'native_type', 'flags', 'table', 'name', 'len', 'precision', 'pdo_type'
  617. );
  618. public function testStatementGetColumnMeta()
  619. {
  620. $select = $this->_db->select()
  621. ->from('zfbugs');
  622. $stmt = $this->_db->prepare($select->__toString());
  623. $stmt->execute();
  624. for ($i = 0; $i < $stmt->columnCount(); ++$i) {
  625. $meta = $stmt->getColumnMeta($i);
  626. $this->assertType('array', $meta);
  627. foreach ($this->_getColumnMetaKeys as $key) {
  628. if ($key == 'table' && version_compare(PHP_VERSION, '5.2.0', '<')) {
  629. continue;
  630. }
  631. $this->assertContains($key, array_keys($meta));
  632. }
  633. }
  634. }
  635. public function testStatementNextRowset()
  636. {
  637. $select = $this->_db->select()
  638. ->from('zfproducts');
  639. $stmt = $this->_db->prepare($select->__toString());
  640. try {
  641. $stmt->nextRowset();
  642. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  643. } catch (Zend_Exception $e) {
  644. $this->assertType('Zend_Db_Statement_Exception', $e,
  645. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  646. $this->assertEquals('nextRowset() is not implemented', $e->getMessage());
  647. }
  648. $stmt->closeCursor();
  649. }
  650. public function testStatementGetSetAttribute()
  651. {
  652. $select = $this->_db->select()
  653. ->from('zfproducts');
  654. $stmt = $this->_db->prepare($select->__toString());
  655. $value = 'value';
  656. try {
  657. $stmt->setAttribute(1234, $value);
  658. } catch (Zend_Exception $e) {
  659. $this->assertContains('This driver doesn\'t support setting attributes', $e->getMessage());
  660. }
  661. try {
  662. $this->assertEquals($value, $stmt->getAttribute(1234), "Expected '$value' #1");
  663. } catch (Zend_Exception $e) {
  664. $this->assertContains('This driver doesn\'t support getting attributes', $e->getMessage());
  665. return;
  666. }
  667. $valueArray = array('value1', 'value2');
  668. $stmt->setAttribute(1235, $valueArray);
  669. $this->assertEquals($valueArray, $stmt->getAttribute(1235), "Expected array #1");
  670. $this->assertEquals($value, $stmt->getAttribute(1234), "Expected '$value' #2");
  671. $valueObject = new stdClass();
  672. $stmt->setAttribute(1236, $valueObject);
  673. $this->assertSame($valueObject, $stmt->getAttribute(1236), "Expected object");
  674. $this->assertEquals($valueArray, $stmt->getAttribute(1235), "Expected array #2");
  675. $this->assertEquals($value, $stmt->getAttribute(1234), "Expected '$value' #2");
  676. }
  677. }