FirebugTest.php 7.2 KB

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