FirebugTest.php 7.1 KB

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