StaticTest.php 25 KB

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