TestCommon.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Statement/TestCommon.php';
  23. /*
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Db
  30. * @group Zend_Db_Statement
  31. */
  32. abstract class Zend_Db_Statement_Pdo_TestCommon extends Zend_Db_Statement_TestCommon
  33. {
  34. public function testStatementConstruct()
  35. {
  36. $select = $this->_db->select()
  37. ->from('zfproducts');
  38. $sql = $select->__toString();
  39. $stmt = new Zend_Db_Statement_Pdo($this->_db, $sql);
  40. $this->assertTrue($stmt instanceof Zend_Db_Statement_Pdo);
  41. $stmt->closeCursor();
  42. }
  43. public function testStatementConstructWithSelectObject()
  44. {
  45. $select = $this->_db->select()
  46. ->from('zfproducts');
  47. $stmt = new Zend_Db_Statement_Pdo($this->_db, $select);
  48. $this->assertTrue($stmt instanceof Zend_Db_Statement_Interface);
  49. $stmt->closeCursor();
  50. }
  51. public function testStatementNextRowset()
  52. {
  53. $select = $this->_db->select()
  54. ->from('zfproducts');
  55. $stmt = $this->_db->prepare($select->__toString());
  56. try {
  57. $stmt->nextRowset();
  58. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  59. } catch (Zend_Exception $e) {
  60. $this->assertTrue($e instanceof Zend_Db_Statement_Exception,
  61. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  62. $this->assertEquals('SQLSTATE[IM001]: Driver does not support this function: driver does not support multiple rowsets', $e->getMessage());
  63. }
  64. $stmt->closeCursor();
  65. }
  66. /**
  67. * @group ZF-4486
  68. */
  69. public function testStatementIsIterableThroughtForeach()
  70. {
  71. $select = $this->_db->select()->from('zfproducts');
  72. $stmt = $this->_db->query($select);
  73. $stmt->setFetchMode(Zend_Db::FETCH_OBJ);
  74. foreach ($stmt as $test) {
  75. $this->assertTrue($test instanceof stdClass);
  76. }
  77. $this->assertTrue(is_int(iterator_count($stmt)));
  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. $this->assertTrue($e->hasChainedException(), 'Missing Chained Exception');
  89. $this->assertTrue($e->getChainedException() instanceof PDOException, 'Wrong type of Exception');
  90. }
  91. }
  92. /**
  93. *
  94. * @group ZF-5868
  95. */
  96. public function testStatementWillPersistBindParamsInQueryProfilerAfterExecute()
  97. {
  98. $this->_db->getProfiler()->setEnabled(true);
  99. $products = $this->_db->quoteIdentifier('zfproducts');
  100. $product_id = $this->_db->quoteIdentifier('product_id');
  101. $sql = "SELECT * FROM $products WHERE $product_id > :product_id ORDER BY $product_id ASC";
  102. $stmt = $this->_db->prepare($sql);
  103. $stmt->bindValue('product_id', 1);
  104. $stmt->execute();
  105. $params = $this->_db->getProfiler()->getLastQueryProfile()->getQueryParams();
  106. $target = array(':product_id' => 1);
  107. $this->assertEquals($target, $params);
  108. }
  109. }