TestCommon.php 9.3 KB

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