TestCommon.php 33 KB

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