DbAdapterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_Test
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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/Test/DbAdapter.php";
  23. require_once "Zend/Test/DbStatement.php";
  24. /**
  25. * @category Zend
  26. * @package Zend_Test
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Test
  31. */
  32. class Zend_Test_DbAdapterTest extends PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * @var Zend_Test_DbAdapter
  36. */
  37. private $_adapter = null;
  38. public function setUp()
  39. {
  40. $this->_adapter = new Zend_Test_DbAdapter();
  41. }
  42. public function testAppendStatementToStack()
  43. {
  44. $stmt1 = Zend_Test_DbStatement::createSelectStatement( array() );
  45. $this->_adapter->appendStatementToStack($stmt1);
  46. $stmt2 = Zend_Test_DbStatement::createSelectStatement( array() );
  47. $this->_adapter->appendStatementToStack($stmt2);
  48. $this->assertSame($stmt2, $this->_adapter->query("foo"));
  49. $this->assertSame($stmt1, $this->_adapter->query("foo"));
  50. }
  51. public function testAppendLastInsertId()
  52. {
  53. $this->_adapter->appendLastInsertIdToStack(1);
  54. $this->_adapter->appendLastInsertIdToStack(2);
  55. $this->assertEquals(2, $this->_adapter->lastInsertId());
  56. $this->assertEquals(1, $this->_adapter->lastInsertId());
  57. }
  58. public function testLastInsertIdDefault()
  59. {
  60. $this->assertFalse($this->_adapter->lastInsertId());
  61. }
  62. public function testListTablesDefault()
  63. {
  64. $this->assertEquals(array(), $this->_adapter->listTables());
  65. }
  66. public function testSetListTables()
  67. {
  68. $this->_adapter->setListTables(array("foo", "bar"));
  69. $this->assertEquals(array("foo", "bar"), $this->_adapter->listTables());
  70. }
  71. public function testDescribeTableDefault()
  72. {
  73. $this->assertEquals(array(), $this->_adapter->describeTable("foo"));
  74. }
  75. public function testDescribeTable()
  76. {
  77. $this->_adapter->setDescribeTable("foo", array("bar"));
  78. $this->assertEquals(array("bar"), $this->_adapter->describeTable("foo"));
  79. }
  80. public function testConnect()
  81. {
  82. $this->assertFalse($this->_adapter->isConnected());
  83. $this->_adapter->query("foo");
  84. $this->assertTrue($this->_adapter->isConnected());
  85. $this->_adapter->closeConnection();
  86. $this->assertFalse($this->_adapter->isConnected());
  87. }
  88. public function testAppendLimitToSql()
  89. {
  90. $sql = $this->_adapter->limit("foo", 10, 20);
  91. $this->assertEquals(
  92. "foo LIMIT 20,10", $sql
  93. );
  94. }
  95. public function testQueryProfiler_EnabledByDefault()
  96. {
  97. $this->assertTrue($this->_adapter->getProfiler()->getEnabled());
  98. }
  99. public function testQueryPRofiler_PrepareStartsQueryProfiler()
  100. {
  101. $stmt = $this->_adapter->prepare("SELECT foo");
  102. $this->assertEquals(1, $this->_adapter->getProfiler()->getTotalNumQueries());
  103. $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
  104. /* @var $qp Zend_Db_Profiler_Query */
  105. $this->assertFalse($qp->hasEnded());
  106. }
  107. public function testQueryProfiler_QueryStartEndsQueryProfiler()
  108. {
  109. $stmt = $this->_adapter->query("SELECT foo");
  110. $this->assertEquals(1, $this->_adapter->getProfiler()->getTotalNumQueries());
  111. $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
  112. /* @var $qp Zend_Db_Profiler_Query */
  113. $this->assertTrue($qp->hasEnded());
  114. }
  115. public function testQueryProfiler_QueryBindWithParams()
  116. {
  117. $stmt = $this->_adapter->query("SELECT * FROM foo WHERE bar = ?", array(1234));
  118. $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
  119. /* @var $qp Zend_Db_Profiler_Query */
  120. $this->assertEquals(array(1 => 1234), $qp->getQueryParams());
  121. $this->assertEquals("SELECT * FROM foo WHERE bar = ?", $qp->getQuery());
  122. }
  123. public function testQueryProfiler_PrepareBindExecute()
  124. {
  125. $var = 1234;
  126. $stmt = $this->_adapter->prepare("SELECT * FROM foo WHERE bar = ?");
  127. $stmt->bindParam(1, $var);
  128. $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
  129. /* @var $qp Zend_Db_Profiler_Query */
  130. $this->assertEquals(array(1 => 1234), $qp->getQueryParams());
  131. $this->assertEquals("SELECT * FROM foo WHERE bar = ?", $qp->getQuery());
  132. }
  133. public function testGetSetQuoteIdentifierSymbol()
  134. {
  135. $this->assertEquals('', $this->_adapter->getQuoteIdentifierSymbol());
  136. $this->_adapter->setQuoteIdentifierSymbol('`');
  137. $this->assertEquals('`', $this->_adapter->getQuoteIdentifierSymbol());
  138. }
  139. }