2
0

MysqliTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. class Zend_Db_Statement_MysqliTest extends Zend_Db_Statement_TestCommon
  33. {
  34. public function testStatementRowCount()
  35. {
  36. $products = $this->_db->quoteIdentifier('zfproducts');
  37. $product_id = $this->_db->quoteIdentifier('product_id');
  38. $stmt = $this->_db->prepare("DELETE FROM $products WHERE $product_id = 1");
  39. $n = $stmt->rowCount();
  40. $this->assertType('integer', $n);
  41. $this->assertEquals(-1, $n, 'Expecting row count to be -1 before executing query');
  42. $stmt->execute();
  43. $n = $stmt->rowCount();
  44. $stmt->closeCursor();
  45. $this->assertType('integer', $n);
  46. $this->assertEquals(1, $n, 'Expected row count to be one after executing query');
  47. }
  48. public function testStatementBindParamByName()
  49. {
  50. $products = $this->_db->quoteIdentifier('zfproducts');
  51. $product_id = $this->_db->quoteIdentifier('product_id');
  52. $product_name = $this->_db->quoteIdentifier('product_name');
  53. $productIdValue = 4;
  54. $productNameValue = 'AmigaOS';
  55. try {
  56. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  57. // test with colon prefix
  58. $this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
  59. // test with no colon prefix
  60. $this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
  61. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  62. } catch (Zend_Exception $e) {
  63. $this->assertType('Zend_Db_Statement_Exception', $e,
  64. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  65. $this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
  66. }
  67. }
  68. public function testStatementBindValueByName()
  69. {
  70. $products = $this->_db->quoteIdentifier('zfproducts');
  71. $product_id = $this->_db->quoteIdentifier('product_id');
  72. $product_name = $this->_db->quoteIdentifier('product_name');
  73. $productIdValue = 4;
  74. $productNameValue = 'AmigaOS';
  75. try {
  76. $stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
  77. // test with colon prefix
  78. $this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
  79. // test with no colon prefix
  80. $this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
  81. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  82. } catch (Zend_Exception $e) {
  83. $this->assertType('Zend_Db_Statement_Exception', $e,
  84. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  85. $this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
  86. }
  87. }
  88. public function testStatementGetColumnMeta()
  89. {
  90. $this->markTestIncomplete($this->getDriver() . ' has not implemented getColumnMeta() yet [ZF-1424]');
  91. }
  92. /**
  93. * Tests ZF-3216, that the statement object throws exceptions that
  94. * contain the numerica MySQL SQLSTATE error code
  95. * @group ZF-3216
  96. */
  97. public function testStatementExceptionShouldContainErrorCode()
  98. {
  99. $sql = "SELECT * FROM *";
  100. try {
  101. $stmt = $this->_db->query($sql);
  102. $this->fail('Expected to catch Zend_Db_Statement_Exception');
  103. } catch (Zend_Exception $e) {
  104. $this->assertType('int', $e->getCode());
  105. }
  106. }
  107. /**
  108. * @group ZF-7706
  109. */
  110. public function testStatementCanReturnDriverStatement()
  111. {
  112. $statement = parent::testStatementCanReturnDriverStatement();
  113. $this->assertType('mysqli_stmt', $statement->getDriverStatement());
  114. }
  115. /**
  116. * Tests that the statement returns FALSE when no records are found
  117. * @group ZF-5675
  118. */
  119. public function testStatementReturnsFalseOnEmpty()
  120. {
  121. $products = $this->_db->quoteIdentifier('zfproducts');
  122. $sql = 'SELECT * FROM ' . $products . ' WHERE 1=2';
  123. $stmt = $this->_db->query($sql);
  124. $result = $stmt->fetch();
  125. $this->assertFalse($result);
  126. }
  127. /**
  128. * Test to verify valid report of issue
  129. *
  130. * @group ZF-8986
  131. */
  132. public function testNumberOfBoundParamsDoesNotMatchNumberOfTokens()
  133. {
  134. $this->_util->createTable('zf_objects', array(
  135. 'object_id' => 'INTEGER NOT NULL',
  136. 'object_type' => 'INTEGER NOT NULL',
  137. 'object_status' => 'INTEGER NOT NULL',
  138. 'object_lati' => 'REAL',
  139. 'object_long' => 'REAL',
  140. ));
  141. $tableName = $this->_util->getTableName('zf_objects');
  142. $numRows = $this->_db->insert($tableName, array (
  143. 'object_id' => 1,
  144. 'object_type' => 1,
  145. 'object_status' => 1,
  146. 'object_lati' => 1.12345,
  147. 'object_long' => 1.54321,
  148. ));
  149. $sql = 'SELECT object_id, object_type, object_status,'
  150. . ' object_lati, object_long FROM ' . $tableName
  151. . ' WHERE object_id = ?';
  152. try {
  153. $stmt = $this->_db->query($sql, 1);
  154. } catch (Exception $e) {
  155. $this->fail('Bounding params failed: ' . $e->getMessage());
  156. }
  157. $result = $stmt->fetch();
  158. $this->assertType('array', $result);
  159. $this->assertEquals(5, count($result));
  160. $this->assertEquals(1, $result['object_id']);
  161. $this->assertEquals(1, $result['object_type']);
  162. $this->assertEquals(1, $result['object_status']);
  163. $this->assertEquals(1.12345, $result['object_lati']);
  164. $this->assertEquals(1.54321, $result['object_long']);
  165. }
  166. public function getDriver()
  167. {
  168. return 'Mysqli';
  169. }
  170. }