TestCommon.php 33 KB

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