2
0

MysqliTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/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-2009 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. class Zend_Db_Statement_MysqliTest extends Zend_Db_Statement_TestCommon
  34. {
  35. public function testStatementRowCount()
  36. {
  37. $products = $this->_db->quoteIdentifier('zfproducts');
  38. $product_id = $this->_db->quoteIdentifier('product_id');
  39. $stmt = $this->_db->prepare("DELETE FROM $products WHERE $product_id = 1");
  40. $n = $stmt->rowCount();
  41. $this->assertType('integer', $n);
  42. $this->assertEquals(-1, $n, 'Expecting row count to be -1 before executing query');
  43. $stmt->execute();
  44. $n = $stmt->rowCount();
  45. $stmt->closeCursor();
  46. $this->assertType('integer', $n);
  47. $this->assertEquals(1, $n, 'Expected row count to be one after executing query');
  48. }
  49. public function testStatementBindParamByName()
  50. {
  51. $products = $this->_db->quoteIdentifier('zfproducts');
  52. $product_id = $this->_db->quoteIdentifier('product_id');
  53. $product_name = $this->_db->quoteIdentifier('product_name');
  54. $productIdValue = 4;
  55. $productNameValue = 'AmigaOS';
  56. try {
  57. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  58. // test with colon prefix
  59. $this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
  60. // test with no colon prefix
  61. $this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
  62. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  63. } catch (Zend_Exception $e) {
  64. $this->assertType('Zend_Db_Statement_Exception', $e,
  65. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  66. $this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
  67. }
  68. }
  69. public function testStatementBindValueByName()
  70. {
  71. $products = $this->_db->quoteIdentifier('zfproducts');
  72. $product_id = $this->_db->quoteIdentifier('product_id');
  73. $product_name = $this->_db->quoteIdentifier('product_name');
  74. $productIdValue = 4;
  75. $productNameValue = 'AmigaOS';
  76. try {
  77. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  78. // test with colon prefix
  79. $this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
  80. // test with no colon prefix
  81. $this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
  82. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  83. } catch (Zend_Exception $e) {
  84. $this->assertType('Zend_Db_Statement_Exception', $e,
  85. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  86. $this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
  87. }
  88. }
  89. public function testStatementGetColumnMeta()
  90. {
  91. $this->markTestIncomplete($this->getDriver() . ' has not implemented getColumnMeta() yet [ZF-1424]');
  92. }
  93. /**
  94. * Tests ZF-3216, that the statement object throws exceptions that
  95. * contain the numerica MySQL SQLSTATE error code
  96. * @group ZF-3216
  97. */
  98. public function testStatementExceptionShouldContainErrorCode()
  99. {
  100. $sql = "SELECT * FROM *";
  101. try {
  102. $stmt = $this->_db->query($sql);
  103. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  104. } catch (Zend_Exception $e) {
  105. $this->assertType('int', $e->getCode());
  106. }
  107. }
  108. /**
  109. * @group ZF-7706
  110. */
  111. public function testStatementCanReturnDriverStatement()
  112. {
  113. $statement = parent::testStatementCanReturnDriverStatement();
  114. $this->assertType('mysqli_stmt', $statement->getDriverStatement());
  115. }
  116. /**
  117. * Tests that the statement returns FALSE when no records are found
  118. * @group ZF-5675
  119. */
  120. public function testStatementReturnsFalseOnEmpty()
  121. {
  122. $products = $this->_db->quoteIdentifier('zfproducts');
  123. $sql = 'SELECT * FROM ' . $products . ' WHERE 1=2';
  124. $stmt = $this->_db->query($sql);
  125. $result = $stmt->fetch();
  126. $this->assertFalse($result);
  127. }
  128. public function getDriver()
  129. {
  130. return 'Mysqli';
  131. }
  132. }