OracleTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /**
  23. * @see Zend_Db_Profiler_TestCommon
  24. */
  25. require_once 'Zend/Db/Profiler/TestCommon.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Db
  33. * @group Zend_Db_Profiler
  34. */
  35. class Zend_Db_Profiler_OracleTest extends Zend_Db_Profiler_TestCommon
  36. {
  37. public function testProfilerPreparedStatementWithParams()
  38. {
  39. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  40. // prepare a query
  41. $select = $this->_db->select()
  42. ->from('zfbugs')
  43. ->where("$bug_id = :bug_id");
  44. $stmt = $this->_db->prepare($select->__toString());
  45. // execute query a first time
  46. $stmt->execute(array(':bug_id' => 2));
  47. $results = $stmt->fetchAll();
  48. $this->assertTrue(is_array($results));
  49. $this->assertEquals(2, $results[0]['bug_id']);
  50. // analyze query profiles
  51. $profiles = $this->_db->getProfiler()->getQueryProfiles(null, true);
  52. $this->assertTrue(is_array($profiles), 'Expected array, got '.gettype($profiles));
  53. $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
  54. $qp = $profiles[0];
  55. $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
  56. // analyze query in the profile
  57. $sql = $qp->getQuery();
  58. $this->assertContains(" = :bug_id", $sql);
  59. $params = $qp->getQueryParams();
  60. $this->assertTrue(is_array($params));
  61. $this->assertEquals(array(':bug_id' => 2), $params);
  62. // execute query a second time
  63. $stmt->execute(array(':bug_id' => 3));
  64. $results = $stmt->fetchAll();
  65. $this->assertTrue(is_array($results));
  66. $this->assertEquals(3, $results[0]['bug_id']);
  67. // analyze query profiles
  68. $profiles = $this->_db->getProfiler()->getQueryProfiles(null, true);
  69. $this->assertTrue(is_array($profiles), 'Expected array, got '.gettype($profiles));
  70. $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
  71. $qp = $profiles[1];
  72. $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
  73. // analyze query in the profile
  74. $sql = $qp->getQuery();
  75. $this->assertContains(" = :bug_id", $sql);
  76. $params = $qp->getQueryParams();
  77. $this->assertTrue(is_array($params));
  78. $this->assertEquals(array(':bug_id' => 3), $params);
  79. }
  80. public function testProfilerPreparedStatementWithBoundParams()
  81. {
  82. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  83. // prepare a query
  84. $select = $this->_db->select()
  85. ->from('zfbugs')
  86. ->where("$bug_id = :bug_id");
  87. $stmt = $this->_db->prepare($select->__toString());
  88. // execute query a first time
  89. $id = 1;
  90. $this->assertTrue($stmt->bindParam(':bug_id', $id));
  91. $id = 2;
  92. $stmt->execute();
  93. $results = $stmt->fetchAll();
  94. $this->assertTrue(is_array($results));
  95. $this->assertEquals(2, $results[0]['bug_id']);
  96. // analyze query profiles
  97. $profiles = $this->_db->getProfiler()->getQueryProfiles(null, true);
  98. $this->assertTrue(is_array($profiles));
  99. $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
  100. $qp = $profiles[0];
  101. $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
  102. // analyze query in the profile
  103. $sql = $qp->getQuery();
  104. $this->assertContains(" = :bug_id", $sql);
  105. $params = $qp->getQueryParams();
  106. $this->assertTrue(is_array($params));
  107. $this->assertEquals(array(':bug_id' => 2), $params);
  108. // execute query a first time
  109. $id = 3;
  110. $stmt->execute();
  111. $results = $stmt->fetchAll();
  112. $this->assertTrue(is_array($results));
  113. $this->assertEquals(3, $results[0]['bug_id']);
  114. // analyze query profiles
  115. $profiles = $this->_db->getProfiler()->getQueryProfiles(null, true);
  116. $this->assertTrue(is_array($profiles));
  117. $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
  118. $qp = $profiles[1];
  119. $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
  120. // analyze query in the profile
  121. $sql = $qp->getQuery();
  122. $this->assertContains(" = :bug_id", $sql);
  123. $params = $qp->getQueryParams();
  124. $this->assertTrue(is_array($params));
  125. $this->assertEquals(array(':bug_id' => 3), $params);
  126. }
  127. /**
  128. * Ensures that setFilterQueryType() actually filters
  129. *
  130. * @return void
  131. */
  132. protected function _testProfilerSetFilterQueryTypeCommon($queryType)
  133. {
  134. $bugs = $this->_db->quoteIdentifier('zfbugs', true);
  135. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  136. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  137. $prof = $this->_db->getProfiler();
  138. $prof->setEnabled(true);
  139. $this->assertSame($prof->setFilterQueryType($queryType), $prof);
  140. $this->assertEquals($queryType, $prof->getFilterQueryType());
  141. $this->_db->query("SELECT * FROM $bugs");
  142. $this->_db->query("INSERT INTO $bugs ($bug_id, $bug_status) VALUES (:id, :status)", array(':id' => 100,':status' => 'NEW'));
  143. $this->_db->query("DELETE FROM $bugs");
  144. $this->_db->query("UPDATE $bugs SET $bug_status = :status", array(':status'=>'FIXED'));
  145. $qps = $prof->getQueryProfiles();
  146. $this->assertTrue(is_array($qps), 'Expecting some query profiles, got none');
  147. foreach ($qps as $qp) {
  148. $qtype = $qp->getQueryType();
  149. $this->assertEquals($queryType, $qtype,
  150. "Found query type $qtype, which should have been filtered out");
  151. }
  152. $prof->setEnabled(false);
  153. }
  154. public function getDriver()
  155. {
  156. return 'Oracle';
  157. }
  158. }