FirebugTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2015 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. /** Zend_Db */
  23. require_once 'Zend/Db.php';
  24. /** Zend_Db_Profiler_Firebug */
  25. require_once 'Zend/Db/Profiler/Firebug.php';
  26. /** Zend_Wildfire_Plugin_FirePhp */
  27. require_once 'Zend/Wildfire/Plugin/FirePhp.php';
  28. /** Zend_Wildfire_Channel_HttpHeaders */
  29. require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
  30. /** Zend_Controller_Request_Http */
  31. require_once 'Zend/Controller/Request/Http.php';
  32. /** Zend_Controller_Response_Http */
  33. require_once 'Zend/Controller/Response/Http.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Db
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Db
  41. * @group Zend_Db_Profiler
  42. */
  43. class Zend_Db_Profiler_FirebugTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $_controller = null;
  46. protected $_request = null;
  47. protected $_response = null;
  48. protected $_profiler = null;
  49. protected $_db = null;
  50. /**
  51. * Runs the test methods of this class.
  52. *
  53. * @access public
  54. * @static
  55. */
  56. public static function main()
  57. {
  58. $suite = new PHPUnit_Framework_TestSuite("Zend_Db_Profiler_FirebugTest");
  59. $result = PHPUnit_TextUI_TestRunner::run($suite);
  60. }
  61. public function setUp()
  62. {
  63. if (!extension_loaded('pdo_sqlite')) {
  64. $this->markTestSkipped('Requires PDO_Sqlite extension');
  65. }
  66. date_default_timezone_set('America/Los_Angeles');
  67. $this->_request = new Zend_Db_Profiler_FirebugTest_Request();
  68. $this->_response = new Zend_Db_Profiler_FirebugTest_Response();
  69. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  70. $channel->setRequest($this->_request);
  71. $channel->setResponse($this->_response);
  72. $this->_profiler = new Zend_Db_Profiler_Firebug();
  73. $this->_db = Zend_Db::factory('PDO_SQLITE',
  74. array('dbname' => ':memory:',
  75. 'profiler' => $this->_profiler));
  76. $this->_db->getConnection()->exec('CREATE TABLE foo (
  77. id INTEGNER NOT NULL,
  78. col1 VARCHAR(10) NOT NULL
  79. )');
  80. }
  81. public function tearDown()
  82. {
  83. if (extension_loaded('pdo_sqlite')) {
  84. $this->_db->getConnection()->exec('DROP TABLE foo');
  85. }
  86. Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
  87. Zend_Wildfire_Plugin_FirePhp::destroyInstance();
  88. }
  89. public function testEnable()
  90. {
  91. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  92. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  93. $this->_db->insert('foo', array('id'=>1,'col1'=>'original'));
  94. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  95. $this->assertFalse($protocol->getMessages());
  96. $this->_profiler->setEnabled(true);
  97. $this->_db->insert('foo', array('id'=>1,'col1'=>'original'));
  98. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  99. $messages = $protocol->getMessages();
  100. $this->assertEquals(substr($messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  101. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0],0,55),
  102. '[{"Type":"TABLE","Label":"Zend_Db_Profiler_Firebug (1 @');
  103. }
  104. public function testDisable()
  105. {
  106. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  107. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  108. $this->_profiler->setEnabled(true);
  109. $this->_db->insert('foo', array('id'=>1,'col1'=>'original'));
  110. $this->_profiler->setEnabled(false);
  111. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  112. $this->assertFalse($protocol->getMessages());
  113. }
  114. public function testCustomLabel()
  115. {
  116. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  117. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  118. $this->_profiler = new Zend_Db_Profiler_Firebug('Label 1');
  119. $this->_profiler->setEnabled(true);
  120. $this->_db->setProfiler($this->_profiler);
  121. $this->_db->insert('foo', array('id'=>1,'col1'=>'original'));
  122. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  123. $messages = $protocol->getMessages();
  124. $this->assertEquals(substr($messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  125. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0],0,38),
  126. '[{"Type":"TABLE","Label":"Label 1 (1 @');
  127. }
  128. public function testNoQueries()
  129. {
  130. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  131. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  132. $this->_profiler->setEnabled(true);
  133. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  134. $messages = $protocol->getMessages();
  135. $this->assertFalse($messages);
  136. }
  137. /**
  138. * @group ZF-6395
  139. */
  140. public function testNoQueriesAfterFiltering()
  141. {
  142. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  143. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  144. $profiler = $this->_profiler->setEnabled(true);
  145. $profiler->setFilterQueryType(Zend_Db_Profiler::INSERT | Zend_Db_Profiler::UPDATE);
  146. $this->_db->fetchAll('select * from foo');
  147. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  148. $messages = $protocol->getMessages();
  149. $this->assertFalse($messages);
  150. }
  151. }
  152. class Zend_Db_Profiler_FirebugTest_Request extends Zend_Controller_Request_Http
  153. {
  154. public function getHeader($header)
  155. {
  156. if ($header == 'User-Agent') {
  157. return 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14 FirePHP/0.1.0';
  158. }
  159. }
  160. }
  161. class Zend_Db_Profiler_FirebugTest_Response extends Zend_Controller_Response_Http
  162. {
  163. public function canSendHeaders($throw = false)
  164. {
  165. return true;
  166. }
  167. }