TestCommon.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-2010 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. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  24. /*
  25. * @category Zend
  26. * @package Zend_Db
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2010 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_Pdo_TestCommon extends Zend_Db_Statement_TestCommon
  34. {
  35. public function testStatementConstruct()
  36. {
  37. $select = $this->_db->select()
  38. ->from('zfproducts');
  39. $sql = $select->__toString();
  40. $stmt = new Zend_Db_Statement_Pdo($this->_db, $sql);
  41. $this->assertType('Zend_Db_Statement_Pdo', $stmt);
  42. $stmt->closeCursor();
  43. }
  44. public function testStatementConstructWithSelectObject()
  45. {
  46. $select = $this->_db->select()
  47. ->from('zfproducts');
  48. $stmt = new Zend_Db_Statement_Pdo($this->_db, $select);
  49. $this->assertType('Zend_Db_Statement_Interface', $stmt);
  50. $stmt->closeCursor();
  51. }
  52. public function testStatementNextRowset()
  53. {
  54. $select = $this->_db->select()
  55. ->from('zfproducts');
  56. $stmt = $this->_db->prepare($select->__toString());
  57. try {
  58. $stmt->nextRowset();
  59. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  60. } catch (Zend_Exception $e) {
  61. $this->assertType('Zend_Db_Statement_Exception', $e,
  62. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  63. $this->assertEquals('SQLSTATE[IM001]: Driver does not support this function: driver does not support multiple rowsets', $e->getMessage());
  64. }
  65. $stmt->closeCursor();
  66. }
  67. /**
  68. * @group ZF-4486
  69. */
  70. public function testStatementIsIterableThroughtForeach()
  71. {
  72. $select = $this->_db->select()->from('zfproducts');
  73. $stmt = $this->_db->query($select);
  74. $stmt->setFetchMode(Zend_Db::FETCH_OBJ);
  75. foreach ($stmt as $test) {
  76. $this->assertTrue($test instanceof stdClass);
  77. }
  78. $this->assertType('int', iterator_count($stmt));
  79. }
  80. public function testStatementConstructExceptionBadSql()
  81. {
  82. $sql = "SELECT * FROM *";
  83. try {
  84. $stmt = $this->_db->query($sql);
  85. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  86. } catch (Zend_Exception $e) {
  87. $this->assertType('Zend_Db_Statement_Exception', $e,
  88. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  89. $this->assertTrue($e->hasChainedException(), 'Missing Chained Exception');
  90. $this->assertType('PDOException', $e->getChainedException(), 'Wrong type of Exception');
  91. }
  92. }
  93. /**
  94. *
  95. * @group ZF-5868
  96. */
  97. public function testStatementWillPersistBindParamsInQueryProfilerAfterExecute()
  98. {
  99. $this->_db->getProfiler()->setEnabled(true);
  100. $products = $this->_db->quoteIdentifier('zfproducts');
  101. $product_id = $this->_db->quoteIdentifier('product_id');
  102. $sql = "SELECT * FROM $products WHERE $product_id > :product_id ORDER BY $product_id ASC";
  103. $stmt = $this->_db->prepare($sql);
  104. $stmt->bindValue('product_id', 1);
  105. $stmt->execute();
  106. $params = $this->_db->getProfiler()->getLastQueryProfile()->getQueryParams();
  107. $target = array(':product_id' => 1);
  108. $this->assertEquals($target, $params);
  109. }
  110. }