StaticTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. class Zend_Db_Profiler_StaticTest extends Zend_Db_TestSetup
  37. {
  38. /**
  39. * @return void
  40. */
  41. public function testProfilerFactoryFalse()
  42. {
  43. $db = Zend_Db::factory('Static',
  44. array(
  45. 'dbname' => 'dummy',
  46. 'profiler' => false
  47. )
  48. );
  49. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  50. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  51. $prof = $db->getProfiler();
  52. $this->assertType('Zend_Db_Profiler', $prof,
  53. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  54. $this->assertFalse($prof->getEnabled());
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function testProfilerFactoryTrue()
  60. {
  61. $db = Zend_Db::factory('Static',
  62. array(
  63. 'dbname' => 'dummy',
  64. 'profiler' => true
  65. )
  66. );
  67. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  68. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  69. $prof = $db->getProfiler();
  70. $this->assertType('Zend_Db_Profiler', $prof,
  71. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  72. $this->assertTrue($prof->getEnabled());
  73. }
  74. /**
  75. * Ensures that passing the 'profiler' option as a string continues to work as prior to SVN r6172.
  76. *
  77. * E.g., 'profiler' => 'true', 'profiler' => '1' results in default profiler enabled.
  78. *
  79. * @return void
  80. */
  81. public function testProfilerFactoryString()
  82. {
  83. $profilerStrings = array(
  84. 'true',
  85. '1',
  86. 'Zend_Db_Profiler_ProfilerCustom'
  87. );
  88. foreach ($profilerStrings as $profilerString) {
  89. $db = Zend_Db::factory('Static',
  90. array(
  91. 'dbname' => 'dummy',
  92. 'profiler' => $profilerString
  93. )
  94. );
  95. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  96. 'Expected object of type Zend_Db_Adapter_Abstract, got ' . get_class($db));
  97. $prof = $db->getProfiler();
  98. $this->assertType('Zend_Db_Profiler', $prof,
  99. 'Expected object of type Zend_Db_Profiler, got ' . get_class($prof));
  100. $this->assertTrue($prof->getEnabled());
  101. }
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testProfilerFactoryInstance()
  107. {
  108. require_once 'Zend/Db/Profiler/ProfilerCustom.php';
  109. $profiler = new Zend_Db_Profiler_ProfilerCustom();
  110. $db = Zend_Db::factory('Static',
  111. array(
  112. 'dbname' => 'dummy',
  113. 'profiler' => $profiler
  114. )
  115. );
  116. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  117. 'Expected object of type Zend_Db_Adapter_Abstract, got ' . get_class($db));
  118. $prof = $db->getProfiler();
  119. $this->assertType('Zend_Db_Profiler', $prof,
  120. 'Expected object of type Zend_Db_Profiler, got ' . get_class($prof));
  121. $this->assertType('Zend_Db_Profiler_ProfilerCustom', $prof,
  122. 'Expected object of type Zend_Db_Profiler_ProfilerCustom, got ' . get_class($prof));
  123. $this->assertFalse($prof->getEnabled());
  124. }
  125. /**
  126. * @return void
  127. */
  128. public function testProfilerFactoryArrayEnabled()
  129. {
  130. $db = Zend_Db::factory('Static',
  131. array(
  132. 'dbname' => 'dummy',
  133. 'profiler' => array(
  134. 'enabled' => true
  135. )
  136. )
  137. );
  138. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  139. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  140. $prof = $db->getProfiler();
  141. $this->assertType('Zend_Db_Profiler', $prof,
  142. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  143. $this->assertTrue($prof->getEnabled());
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function testProfilerFactoryArrayClass()
  149. {
  150. $db = Zend_Db::factory('Static',
  151. array(
  152. 'dbname' => 'dummy',
  153. 'profiler' => array(
  154. 'class' => 'Zend_Db_Profiler_ProfilerCustom'
  155. )
  156. )
  157. );
  158. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  159. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  160. $prof = $db->getProfiler();
  161. $this->assertType('Zend_Db_Profiler', $prof,
  162. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  163. $this->assertType('Zend_Db_Profiler_ProfilerCustom', $prof,
  164. 'Expected object of type Zend_Db_Profiler_ProfilerCustom, got '.get_class($prof));
  165. $this->assertFalse($prof->getEnabled());
  166. }
  167. /**
  168. * @return void
  169. */
  170. public function testProfilerFactoryArrayInstance()
  171. {
  172. require_once 'Zend/Db/Profiler/ProfilerCustom.php';
  173. $profiler = new Zend_Db_Profiler_ProfilerCustom();
  174. $db = Zend_Db::factory('Static',
  175. array(
  176. 'dbname' => 'dummy',
  177. 'profiler' => array(
  178. 'instance' => $profiler
  179. )
  180. )
  181. );
  182. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  183. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  184. $prof = $db->getProfiler();
  185. $this->assertType('Zend_Db_Profiler', $prof,
  186. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  187. $this->assertType('Zend_Db_Profiler_ProfilerCustom', $prof,
  188. 'Expected object of type Zend_Db_Profiler_ProfilerCustom, got '.get_class($prof));
  189. $this->assertFalse($prof->getEnabled());
  190. }
  191. /**
  192. * @return void
  193. */
  194. public function testProfilerFactoryConfig()
  195. {
  196. require_once 'Zend/Config.php';
  197. $config = new Zend_Config(array(
  198. 'class' => 'Zend_Db_Profiler_ProfilerCustom'
  199. ));
  200. $db = Zend_Db::factory('Static',
  201. array(
  202. 'dbname' => 'dummy',
  203. 'profiler' => $config
  204. )
  205. );
  206. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  207. 'Expected object of type Zend_Db_Adapter_Abstract, got '.get_class($db));
  208. $prof = $db->getProfiler();
  209. $this->assertType('Zend_Db_Profiler', $prof,
  210. 'Expected object of type Zend_Db_Profiler, got '.get_class($prof));
  211. $this->assertType('Zend_Db_Profiler_ProfilerCustom', $prof,
  212. 'Expected object of type Zend_Db_Profiler_ProfilerCustom, got '.get_class($prof));
  213. $this->assertFalse($prof->getEnabled());
  214. }
  215. /**
  216. * Ensures that setting the profiler does not affect whether the profiler is enabled.
  217. *
  218. * @return void
  219. */
  220. public function testProfilerFactoryEnabledUnaffected()
  221. {
  222. require_once 'Zend/Db/Profiler/ProfilerCustom.php';
  223. $profiler = new Zend_Db_Profiler_ProfilerCustom();
  224. $profiler->setEnabled(true);
  225. $db = Zend_Db::factory('Static',
  226. array(
  227. 'dbname' => 'dummy',
  228. 'profiler' => $profiler
  229. )
  230. );
  231. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  232. 'Expected object of type Zend_Db_Adapter_Abstract, got ' . get_class($db));
  233. $profiler2 = $db->getProfiler();
  234. $this->assertSame($profiler, $profiler2);
  235. $this->assertTrue($profiler->getEnabled());
  236. }
  237. /**
  238. * Ensures that an instance of an invalid profiler class results in an exception
  239. *
  240. * @return void
  241. */
  242. public function testProfilerFactoryInvalidClass()
  243. {
  244. $profilerInvalid = new stdClass();
  245. try {
  246. $db = Zend_Db::factory('Static',
  247. array(
  248. 'dbname' => 'dummy',
  249. 'profiler' => $profilerInvalid
  250. )
  251. );
  252. $this->fail('Expected Zend_Db_Profiler_Exception not thrown');
  253. } catch (Zend_Db_Profiler_Exception $e) {
  254. $this->assertContains('Profiler argument must be an instance of', $e->getMessage());
  255. }
  256. }
  257. /**
  258. * Ensures that the factory can handle an instance of Zend_Config having a profiler instance
  259. *
  260. * @return void
  261. */
  262. public function testProfilerFactoryConfigInstance()
  263. {
  264. require_once 'Zend/Db/Profiler/ProfilerCustom.php';
  265. $profiler = new Zend_Db_Profiler_ProfilerCustom();
  266. require_once 'Zend/Config.php';
  267. $config = new Zend_Config(array('instance' => $profiler));
  268. $db = Zend_Db::factory('Static',
  269. array(
  270. 'dbname' => 'dummy',
  271. 'profiler' => $config
  272. )
  273. );
  274. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  275. 'Expected object of type Zend_Db_Adapter_Abstract, got ' . get_class($db));
  276. $profiler2 = $db->getProfiler();
  277. $this->assertSame($profiler, $profiler2);
  278. $this->assertFalse($profiler->getEnabled());
  279. }
  280. /**
  281. * Ensures that a provided instance overrides a class definition
  282. *
  283. * @return void
  284. */
  285. public function testProfilerFactoryInstanceOverridesClass()
  286. {
  287. require_once 'Zend/Db/Profiler/ProfilerCustom.php';
  288. $profiler = new Zend_Db_Profiler_ProfilerCustom();
  289. $db = Zend_Db::factory('Static',
  290. array(
  291. 'dbname' => 'dummy',
  292. 'profiler' => array(
  293. 'instance' => $profiler,
  294. 'class' => 'stdClass'
  295. )
  296. )
  297. );
  298. $this->assertType('Zend_Db_Adapter_Abstract', $db,
  299. 'Expected object of type Zend_Db_Adapter_Abstract, got ' . get_class($db));
  300. $profiler2 = $db->getProfiler();
  301. $this->assertSame($profiler, $profiler2);
  302. $this->assertFalse($profiler->getEnabled());
  303. }
  304. /**
  305. * Ensures that setEnabled() behaves as expected.
  306. *
  307. * @return void
  308. */
  309. public function testProfilerSetEnabled()
  310. {
  311. $prof = $this->_db->getProfiler();
  312. $this->assertSame($prof->setEnabled(true), $prof);
  313. $this->assertTrue($prof->getEnabled());
  314. $this->assertSame($prof->setEnabled(false), $prof);
  315. $this->assertFalse($prof->getEnabled());
  316. }
  317. /**
  318. * Ensures that setFilterElapsedSecs() behaves as expected.
  319. *
  320. * @return void
  321. */
  322. public function testProfilerSetFilterElapsedSecs()
  323. {
  324. $prof = $this->_db->getProfiler();
  325. $this->assertSame($prof->setFilterElapsedSecs(), $prof);
  326. $this->assertNull($prof->getFilterElapsedSecs());
  327. $this->assertSame($prof->setFilterElapsedSecs(null), $prof);
  328. $this->assertNull($prof->getFilterElapsedSecs());
  329. $this->assertSame($prof->setFilterElapsedSecs(3), $prof);
  330. $this->assertEquals(3, $prof->getFilterElapsedSecs());
  331. }
  332. /**
  333. * Ensures that setFilterQueryType() remembers the setting.
  334. *
  335. * @return void
  336. */
  337. public function testProfilerSetFilterQueryType()
  338. {
  339. $prof = $this->_db->getProfiler();
  340. $this->assertSame($prof->setFilterQueryType(), $prof);
  341. $this->assertNull($prof->getFilterQueryType());
  342. $this->assertSame($prof->setFilterQueryType(null), $prof);
  343. $this->assertNull($prof->getFilterQueryType());
  344. $queryTypes = Zend_Db_Profiler::DELETE;
  345. $this->assertSame($prof->setFilterQueryType($queryTypes), $prof);
  346. $this->assertEquals($queryTypes, $prof->getFilterQueryType());
  347. }
  348. /**
  349. * Ensures that clear() behaves as expected
  350. *
  351. * @return void
  352. */
  353. public function testProfilerClear()
  354. {
  355. $prof = $this->_db->getProfiler()
  356. ->setEnabled(true);
  357. $this->assertSame($prof->clear(), $prof);
  358. $this->assertFalse($prof->getQueryProfiles());
  359. }
  360. /**
  361. * Ensures that queryStart() behaves as expected.
  362. *
  363. * @return void
  364. */
  365. public function testProfilerQueryStart()
  366. {
  367. $prof = $this->_db->getProfiler();
  368. $this->assertNull($prof->queryStart('sqlDisabled1'));
  369. $prof->setEnabled(true);
  370. $queries = array(
  371. array(
  372. 'sql' => '',
  373. 'typeGiven' => null,
  374. 'typeExpected' => Zend_Db_Profiler::QUERY
  375. ),
  376. array(
  377. 'sql' => '',
  378. 'typeGiven' => Zend_Db_Profiler::QUERY,
  379. 'typeExpected' => Zend_Db_Profiler::QUERY
  380. ),
  381. array(
  382. 'sql' => 'something',
  383. 'typeGiven' => null,
  384. 'typeExpected' => Zend_Db_Profiler::QUERY
  385. ),
  386. array(
  387. 'sql' => 'INSERT',
  388. 'typeGiven' => null,
  389. 'typeExpected' => Zend_Db_Profiler::INSERT
  390. ),
  391. array(
  392. 'sql' => 'sqlInsert',
  393. 'typeGiven' => Zend_Db_Profiler::INSERT,
  394. 'typeExpected' => Zend_Db_Profiler::INSERT
  395. ),
  396. array(
  397. 'sql' => 'INSERT',
  398. 'typeGiven' => Zend_Db_Profiler::UPDATE,
  399. 'typeExpected' => Zend_Db_Profiler::UPDATE
  400. ),
  401. array(
  402. 'sql' => 'UPDATE',
  403. 'typeGiven' => null,
  404. 'typeExpected' => Zend_Db_Profiler::UPDATE
  405. ),
  406. array(
  407. 'sql' => 'sqlUpdate',
  408. 'typeGiven' => Zend_Db_Profiler::UPDATE,
  409. 'typeExpected' => Zend_Db_Profiler::UPDATE
  410. ),
  411. array(
  412. 'sql' => 'UPDATE',
  413. 'typeGiven' => Zend_Db_Profiler::DELETE,
  414. 'typeExpected' => Zend_Db_Profiler::DELETE
  415. ),
  416. array(
  417. 'sql' => 'DELETE',
  418. 'typeGiven' => null,
  419. 'typeExpected' => Zend_Db_Profiler::DELETE
  420. ),
  421. array(
  422. 'sql' => 'sqlDelete',
  423. 'typeGiven' => Zend_Db_Profiler::DELETE,
  424. 'typeExpected' => Zend_Db_Profiler::DELETE
  425. ),
  426. array(
  427. 'sql' => 'DELETE',
  428. 'typeGiven' => Zend_Db_Profiler::SELECT,
  429. 'typeExpected' => Zend_Db_Profiler::SELECT
  430. ),
  431. array(
  432. 'sql' => 'SELECT',
  433. 'typeGiven' => null,
  434. 'typeExpected' => Zend_Db_Profiler::SELECT
  435. ),
  436. array(
  437. 'sql' => 'sqlSelect',
  438. 'typeGiven' => Zend_Db_Profiler::SELECT,
  439. 'typeExpected' => Zend_Db_Profiler::SELECT
  440. ),
  441. array(
  442. 'sql' => 'SELECT',
  443. 'typeGiven' => Zend_Db_Profiler::INSERT,
  444. 'typeExpected' => Zend_Db_Profiler::INSERT
  445. )
  446. );
  447. foreach ($queries as $key => $query) {
  448. $this->assertEquals($key, $prof->queryStart($query['sql'], $query['typeGiven']));
  449. }
  450. $prof->setEnabled(false);
  451. $this->assertNull($prof->queryStart('sqlDisabled2'));
  452. $queryProfiles = $prof->getQueryProfiles(null, true);
  453. $this->assertEquals(count($queries), count($queryProfiles));
  454. foreach ($queryProfiles as $queryId => $queryProfile) {
  455. $this->assertTrue(isset($queries[$queryId]));
  456. $this->assertEquals($queries[$queryId]['sql'], $queryProfile->getQuery());
  457. $this->assertEquals($queries[$queryId]['typeExpected'], $queryProfile->getQueryType());
  458. }
  459. }
  460. /**
  461. * Ensures that queryEnd() behaves as expected when given invalid query handle.
  462. *
  463. * @return void
  464. */
  465. public function testProfilerQueryEndHandleInvalid()
  466. {
  467. $prof = $this->_db->getProfiler();
  468. $prof->queryEnd('invalid');
  469. $prof->setEnabled(true);
  470. try {
  471. $prof->queryEnd('invalid');
  472. $this->fail('Expected Zend_Db_Profiler_Exception not thrown');
  473. } catch (Zend_Db_Profiler_Exception $e) {
  474. $this->assertContains('no query with handle', $e->getMessage());
  475. }
  476. }
  477. /**
  478. * Ensures that queryEnd() throws an exception when the query has already ended.
  479. *
  480. * @return void
  481. */
  482. public function testProfilerQueryEndAlreadyEnded()
  483. {
  484. $prof = $this->_db->getProfiler()
  485. ->setEnabled(true);
  486. $queryId = $prof->queryStart('sql');
  487. $prof->queryEnd($queryId);
  488. try {
  489. $prof->queryEnd($queryId);
  490. $this->fail('Expected Zend_Db_Profiler_Exception not thrown');
  491. } catch (Zend_Db_Profiler_Exception $e) {
  492. $this->assertContains('has already ended', $e->getMessage());
  493. }
  494. }
  495. /**
  496. * Ensures that queryEnd() does not keep the query profile if the elapsed time is less than
  497. * the minimum time allowed by the elapsed seconds filter.
  498. *
  499. * @return void
  500. */
  501. public function testProfilerQueryEndFilterElapsedSecs()
  502. {
  503. $prof = $this->_db->getProfiler()
  504. ->setEnabled(true)
  505. ->setFilterElapsedSecs(1);
  506. $prof->queryEnd($prof->queryStart('sqlTimeShort1'));
  507. $this->_db->query('sqlTimeShort2');
  508. $this->_db->setOnQuerySleep(2);
  509. $this->_db->query('sqlTimeLong');
  510. $this->_db->setOnQuerySleep(0);
  511. $this->_db->query('sqlTimeShort3');
  512. $this->assertEquals(1, count($queryProfiles = $prof->getQueryProfiles()));
  513. $this->assertTrue(isset($queryProfiles[2]));
  514. $this->assertEquals('sqlTimeLong', $queryProfiles[2]->getQuery());
  515. }
  516. /**
  517. * Ensures that queryEnd() does not keep the query profile if the query type is not allowed
  518. * by the query type filter.
  519. *
  520. * @return void
  521. */
  522. public function testProfilerQueryEndFilterQueryType()
  523. {
  524. $prof = $this->_db->getProfiler()
  525. ->setEnabled(true)
  526. ->setFilterQueryType(Zend_Db_Profiler::UPDATE);
  527. $this->_db->query('INSERT');
  528. $this->_db->query('UPDATE');
  529. $this->_db->query('DELETE');
  530. $this->_db->query('SELECT');
  531. $this->_db->query('UPDATE');
  532. $this->_db->query('DELETE');
  533. $this->assertEquals(2, count($prof->getQueryProfiles()));
  534. }
  535. /**
  536. * Ensures that getQueryProfile() throws an exception when given an invalid query handle.
  537. *
  538. * @return void
  539. */
  540. public function testProfilerGetQueryProfileHandleInvalid()
  541. {
  542. try {
  543. $this->_db->getProfiler()->getQueryProfile('invalid');
  544. $this->fail('Expected Zend_Db_Profiler_Exception not thrown');
  545. } catch (Zend_Db_Profiler_Exception $e) {
  546. $this->assertContains('not found', $e->getMessage());
  547. }
  548. }
  549. /**
  550. * Ensures that getQueryProfile() behaves as expected when provided a valid handle.
  551. *
  552. * @return void
  553. */
  554. public function testProfilerGetQueryProfileHandleValid()
  555. {
  556. $prof = $this->_db->getProfiler()
  557. ->setEnabled(true);
  558. $queryId = $prof->queryStart('sql');
  559. $this->assertType('Zend_Db_Profiler_Query',
  560. $prof->getQueryProfile($queryId));
  561. }
  562. /**
  563. * Ensures that getQueryProfiles() returns only those queries of the specified type(s).
  564. *
  565. * @return void
  566. */
  567. public function testProfilerGetQueryProfilesFilterQueryType()
  568. {
  569. $prof = $this->_db->getProfiler()
  570. ->setEnabled(true);
  571. $queries = array(
  572. 'INSERT',
  573. 'UPDATE',
  574. 'DELETE',
  575. 'SELECT'
  576. );
  577. foreach ($queries as $query) {
  578. $this->_db->query($query);
  579. }
  580. foreach ($queries as $queryId => $query) {
  581. $queryProfiles = $prof->getQueryProfiles(constant("Zend_Db_Profiler::$query"));
  582. $this->assertEquals(1, count($queryProfiles));
  583. $this->assertTrue(isset($queryProfiles[$queryId]));
  584. $this->assertEquals($query, $queryProfiles[$queryId]->getQuery());
  585. }
  586. }
  587. /**
  588. * Ensures that getTotalElapsedSecs() behaves as expected for basic usage.
  589. *
  590. * @return void
  591. */
  592. public function testProfilerGetTotalElapsedSecsBasic()
  593. {
  594. $prof = $this->_db->getProfiler()
  595. ->setEnabled(true);
  596. $this->_db->setOnQuerySleep(1);
  597. $numQueries = 3;
  598. for ($queryCount = 0; $queryCount < $numQueries; ++$queryCount) {
  599. $this->_db->query("sql $queryCount");
  600. }
  601. $this->assertThat(
  602. $prof->getTotalElapsedSecs(),
  603. $this->greaterThan($numQueries - 0.1)
  604. );
  605. }
  606. /**
  607. * Ensures that getTotalElapsedSecs() only counts queries of the specified type(s).
  608. *
  609. * @return void
  610. */
  611. public function testProfilerGetTotalElapsedSecsFilterQueryType()
  612. {
  613. $prof = $this->_db->getProfiler()
  614. ->setEnabled(true);
  615. $this->_db->query('SELECT');
  616. $this->_db->query('SELECT');
  617. $this->_db->setOnQuerySleep(1);
  618. $this->_db->query('UPDATE');
  619. $this->_db->setOnQuerySleep(0);
  620. $this->_db->query('SELECT');
  621. $this->_db->setOnQuerySleep(1);
  622. $this->_db->query('UPDATE');
  623. $this->_db->setOnQuerySleep(0);
  624. $this->_db->query('SELECT');
  625. $this->assertThat(
  626. $prof->getTotalElapsedSecs(Zend_Db_Profiler::SELECT),
  627. $this->lessThan(1)
  628. );
  629. $this->assertThat(
  630. $prof->getTotalElapsedSecs(Zend_Db_Profiler::UPDATE),
  631. $this->greaterThan(1.9)
  632. );
  633. }
  634. /**
  635. * Ensures that getTotalNumQueries() behaves as expected for basic usage.
  636. *
  637. * @return void
  638. */
  639. public function testProfilerGetTotalNumQueries()
  640. {
  641. $prof = $this->_db->getProfiler()
  642. ->setEnabled(true);
  643. $numQueries = 3;
  644. for ($queryCount = 0; $queryCount < $numQueries; ++$queryCount) {
  645. $this->_db->query("sql $queryCount");
  646. }
  647. $this->assertEquals($numQueries, $prof->getTotalNumQueries());
  648. }
  649. /**
  650. * Ensures that getTotalNumQueries() only counts queries of the specified type(s).
  651. *
  652. * @return void
  653. */
  654. public function testProfilerGetTotalNumQueriesFilterQueryType()
  655. {
  656. $prof = $this->_db->getProfiler()
  657. ->setEnabled(true);
  658. $queries = array(
  659. 'INSERT' => 2,
  660. 'UPDATE' => 5,
  661. 'DELETE' => 1,
  662. 'SELECT' => 3
  663. );
  664. foreach ($queries as $querySql => $queryCount) {
  665. for ($i = 0; $i < $queryCount; ++$i) {
  666. $this->_db->query("$querySql $i");
  667. }
  668. }
  669. foreach ($queries as $querySql => $queryCount) {
  670. $this->assertEquals($queryCount, $prof->getTotalNumQueries(constant("Zend_Db_Profiler::$querySql")));
  671. }
  672. }
  673. /**
  674. * Ensures that getLastQueryProfile() returns false when no queries have been profiled.
  675. *
  676. * @return void
  677. */
  678. public function testProfilerGetLastQueryProfileFalse()
  679. {
  680. $prof = $this->_db->getProfiler()
  681. ->setEnabled(true);
  682. $this->assertFalse($prof->getLastQueryProfile());
  683. }
  684. /**
  685. * Ensures that getLastQueryProfile() returns the last query profiled, regardless whether it has ended.
  686. *
  687. * @return void
  688. */
  689. public function testProfilerGetLastQueryProfile()
  690. {
  691. $prof = $this->_db->getProfiler()
  692. ->setEnabled(true);
  693. $this->_db->query('sql 1');
  694. $prof->queryStart('sql 2');
  695. $this->assertType('Zend_Db_Profiler_Query',
  696. $queryProfile = $prof->getLastQueryProfile());
  697. $this->assertEquals('sql 2', $queryProfile->getQuery());
  698. $this->assertFalse($queryProfile->getElapsedSecs());
  699. }
  700. public function getDriver()
  701. {
  702. return 'Static';
  703. }
  704. }