TestCommon.php 33 KB

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