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