2
0

MysqliTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 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. /**
  129. * Test to verify valid report of issue
  130. *
  131. * @group ZF-8986
  132. */
  133. public function testNumberOfBoundParamsDoesNotMatchNumberOfTokens()
  134. {
  135. $this->_util->createTable('zf_objects', array(
  136. 'object_id' => 'INTEGER NOT NULL',
  137. 'object_type' => 'INTEGER NOT NULL',
  138. 'object_status' => 'INTEGER NOT NULL',
  139. 'object_lati' => 'REAL',
  140. 'object_long' => 'REAL',
  141. ));
  142. $tableName = $this->_util->getTableName('zf_objects');
  143. $numRows = $this->_db->insert($tableName, array (
  144. 'object_id' => 1,
  145. 'object_type' => 1,
  146. 'object_status' => 1,
  147. 'object_lati' => 1.12345,
  148. 'object_long' => 1.54321,
  149. ));
  150. $sql = 'SELECT object_id, object_type, object_status,'
  151. . ' object_lati, object_long FROM ' . $tableName
  152. . ' WHERE object_id = ?';
  153. try {
  154. $stmt = $this->_db->query($sql, 1);
  155. } catch (Exception $e) {
  156. $this->fail('Bounding params failed: ' . $e->getMessage());
  157. }
  158. $result = $stmt->fetch();
  159. $this->assertType('array', $result);
  160. $this->assertEquals(5, count($result));
  161. $this->assertEquals(1, $result['object_id']);
  162. $this->assertEquals(1, $result['object_type']);
  163. $this->assertEquals(1, $result['object_status']);
  164. $this->assertEquals(1.12345, $result['object_lati']);
  165. $this->assertEquals(1.54321, $result['object_long']);
  166. }
  167. public function getDriver()
  168. {
  169. return 'Mysqli';
  170. }
  171. }