2
0

TestCommon.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_TestSetup
  24. */
  25. require_once 'Zend/Db/TestSetup.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. abstract class Zend_Db_Profiler_TestCommon extends Zend_Db_TestSetup
  35. {
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $this->_db->getProfiler()->setEnabled(true);
  40. }
  41. public function tearDown()
  42. {
  43. $this->_db->getProfiler()->setEnabled(false);
  44. parent::tearDown();
  45. }
  46. public function testProfilerPreparedStatement()
  47. {
  48. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  49. // prepare a query
  50. $select = $this->_db->select()
  51. ->from('zfbugs')
  52. ->where("$bug_id = 2");
  53. $stmt = $this->_db->prepare($select->__toString());
  54. // execute query a first time
  55. $stmt->execute();
  56. $results = $stmt->fetchAll();
  57. $this->assertType('array', $results);
  58. $this->assertEquals(2, $results[0]['bug_id']);
  59. // analyze query profiles
  60. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  61. $this->assertType('array', $profiles);
  62. $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
  63. $qp = $profiles[0];
  64. $this->assertType('Zend_Db_Profiler_Query', $qp);
  65. // execute query a second time
  66. $stmt->execute();
  67. $results = $stmt->fetchAll();
  68. $this->assertType('array', $results);
  69. $this->assertEquals(2, $results[0]['bug_id']);
  70. // analyze query profiles
  71. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  72. $this->assertType('array', $profiles);
  73. $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
  74. $qp = $profiles[1];
  75. $this->assertType('Zend_Db_Profiler_Query', $qp);
  76. $this->assertNotSame($profiles[0], $profiles[1]);
  77. $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
  78. }
  79. public function testProfilerPreparedStatementWithParams()
  80. {
  81. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  82. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  83. // prepare a query
  84. $select = $this->_db->select()
  85. ->from('zfbugs')
  86. ->where("$bug_id = ? AND $bug_status = ?");
  87. $stmt = $this->_db->prepare($select->__toString());
  88. // execute query a first time
  89. $stmt->execute(array(2, 'VERIFIED'));
  90. $results = $stmt->fetchAll();
  91. $this->assertType('array', $results);
  92. $this->assertEquals(2, $results[0]['bug_id']);
  93. // analyze query profiles
  94. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  95. $this->assertType('array', $profiles);
  96. $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
  97. $qp = $profiles[0];
  98. $this->assertType('Zend_Db_Profiler_Query', $qp);
  99. // analyze query in the profile
  100. $sql = $qp->getQuery();
  101. $this->assertContains(" = ?", $sql);
  102. $params = $qp->getQueryParams();
  103. $this->assertType('array', $params);
  104. $this->assertEquals(array(1 => 2, 2 => 'VERIFIED'), $params);
  105. // execute query a second time
  106. $stmt->execute(array(3, 'FIXED'));
  107. $results = $stmt->fetchAll();
  108. $this->assertType('array', $results);
  109. $this->assertEquals(3, $results[0]['bug_id']);
  110. // analyze query profiles
  111. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  112. $this->assertType('array', $profiles);
  113. $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
  114. $qp = $profiles[1];
  115. $this->assertType('Zend_Db_Profiler_Query', $qp);
  116. $this->assertNotSame($profiles[0], $profiles[1]);
  117. $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
  118. // analyze query in the profile
  119. $sql = $qp->getQuery();
  120. $this->assertContains(" = ?", $sql);
  121. $params = $qp->getQueryParams();
  122. $this->assertType('array', $params);
  123. $this->assertEquals(array(1 => 3, 2 => 'FIXED'), $params);
  124. $this->assertNotSame($profiles[0], $profiles[1]);
  125. $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
  126. }
  127. public function testProfilerPreparedStatementWithBoundParams()
  128. {
  129. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  130. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  131. // prepare a query
  132. $select = $this->_db->select()
  133. ->from('zfbugs')
  134. ->where("$bug_id = ? AND $bug_status = ?");
  135. $stmt = $this->_db->prepare($select->__toString());
  136. // execute query a first time
  137. $id = 1;
  138. $status = 'NEW';
  139. $this->assertTrue($stmt->bindParam(1, $id));
  140. $this->assertTrue($stmt->bindParam(2, $status));
  141. $id = 2;
  142. $status = 'VERIFIED';
  143. $stmt->execute();
  144. $results = $stmt->fetchAll();
  145. $this->assertType('array', $results);
  146. $this->assertEquals(2, $results[0]['bug_id']);
  147. // analyze query profiles
  148. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  149. $this->assertType('array', $profiles, 'Expected array, got '.gettype($profiles));
  150. $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
  151. $qp = $profiles[0];
  152. $this->assertType('Zend_Db_Profiler_Query', $qp);
  153. // analyze query in the profile
  154. $sql = $qp->getQuery();
  155. $this->assertContains(" = ?", $sql);
  156. $params = $qp->getQueryParams();
  157. $this->assertType('array', $params);
  158. $this->assertEquals(array(1 => 2, 2 => 'VERIFIED'), $params);
  159. // execute query a second time
  160. $id = 3;
  161. $status = 'FIXED';
  162. $stmt->execute();
  163. $results = $stmt->fetchAll();
  164. $this->assertType('array', $results);
  165. $this->assertEquals(3, $results[0]['bug_id']);
  166. // analyze query profiles
  167. $profiles = $this->_db->getProfiler()->getQueryProfiles();
  168. $this->assertType('array', $profiles, 'Expected array, got '.gettype($profiles));
  169. $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
  170. $qp = $profiles[1];
  171. $this->assertType('Zend_Db_Profiler_Query', $qp);
  172. // analyze query in the profile
  173. $sql = $qp->getQuery();
  174. $this->assertContains(" = ?", $sql);
  175. $params = $qp->getQueryParams();
  176. $this->assertType('array', $params);
  177. $this->assertEquals(array(1 => 3, 2 => 'FIXED'), $params);
  178. }
  179. /**
  180. * Ensures that setFilterQueryType() actually filters
  181. *
  182. * @return void
  183. */
  184. protected function _testProfilerSetFilterQueryTypeCommon($queryType)
  185. {
  186. $bugs = $this->_db->quoteIdentifier('zfbugs', true);
  187. $bug_status = $this->_db->quoteIdentifier('bug_status', true);
  188. $prof = $this->_db->getProfiler();
  189. $prof->setEnabled(true);
  190. $this->assertSame($prof->setFilterQueryType($queryType), $prof);
  191. $this->assertEquals($queryType, $prof->getFilterQueryType());
  192. $this->_db->query("SELECT * FROM $bugs");
  193. $this->_db->query("INSERT INTO $bugs ($bug_status) VALUES (?)", array('NEW'));
  194. $this->_db->query("DELETE FROM $bugs");
  195. $this->_db->query("UPDATE $bugs SET $bug_status = ?", array('FIXED'));
  196. $qps = $prof->getQueryProfiles();
  197. $this->assertType('array', $qps, 'Expecting some query profiles, got none');
  198. foreach ($qps as $qp) {
  199. $qtype = $qp->getQueryType();
  200. $this->assertEquals($queryType, $qtype,
  201. "Found query type $qtype, which should have been filtered out");
  202. }
  203. $prof->setEnabled(false);
  204. }
  205. public function testProfilerSetFilterQueryTypeInsert()
  206. {
  207. $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::INSERT);
  208. }
  209. public function testProfilerSetFilterQueryTypeUpdate()
  210. {
  211. $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::UPDATE);
  212. }
  213. public function testProfilerSetFilterQueryTypeDelete()
  214. {
  215. $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::DELETE);
  216. }
  217. public function testProfilerSetFilterQueryTypeSelect()
  218. {
  219. $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::SELECT);
  220. }
  221. }