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