OracleTest.php 6.6 KB

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