2
0

FirebugTest.php 6.4 KB

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