2
0

FirebugTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @version $Id$
  43. */
  44. class Zend_Db_Profiler_FirebugTest extends PHPUnit_Framework_TestCase
  45. {
  46. protected $_controller = null;
  47. protected $_request = null;
  48. protected $_response = null;
  49. protected $_profiler = null;
  50. protected $_db = null;
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @access public
  55. * @static
  56. */
  57. public static function main()
  58. {
  59. require_once "PHPUnit/TextUI/TestRunner.php";
  60. $suite = new PHPUnit_Framework_TestSuite("Zend_Db_Profiler_FirebugTest");
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. public function setUp()
  64. {
  65. if (!extension_loaded('pdo_sqlite')) {
  66. $this->markTestSkipped('Requires PDO_Sqlite extension');
  67. }
  68. date_default_timezone_set('America/Los_Angeles');
  69. $this->_request = new Zend_Db_Profiler_FirebugTest_Request();
  70. $this->_response = new Zend_Db_Profiler_FirebugTest_Response();
  71. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  72. $channel->setRequest($this->_request);
  73. $channel->setResponse($this->_response);
  74. $this->_profiler = new Zend_Db_Profiler_Firebug();
  75. $this->_db = Zend_Db::factory('PDO_SQLITE',
  76. array('dbname' => ':memory:',
  77. 'profiler' => $this->_profiler));
  78. $this->_db->getConnection()->exec('CREATE TABLE foo (
  79. id INTEGNER NOT NULL,
  80. col1 VARCHAR(10) NOT NULL
  81. )');
  82. }
  83. public function tearDown()
  84. {
  85. $this->_db->getConnection()->exec('DROP TABLE foo');
  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. class Zend_Db_Profiler_FirebugTest_Request extends Zend_Controller_Request_Http
  139. {
  140. public function getHeader($header)
  141. {
  142. if ($header == 'User-Agent') {
  143. 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';
  144. }
  145. }
  146. }
  147. class Zend_Db_Profiler_FirebugTest_Response extends Zend_Controller_Response_Http
  148. {
  149. public function canSendHeaders($throw = false)
  150. {
  151. return true;
  152. }
  153. }