TestCommon.php 9.3 KB

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