DbAdapterTest.php 5.1 KB

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