OracleTest.php 6.5 KB

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