1. sfBrowser.class.php
  2. /** * sfBrowser simulates a browser which can surf a symfony application. * * @package symfony * @subpackage util * @author Fabien Potencier * @version SVN: $Id: sfBrowser.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfBrowser extends sfBrowserBase
  4. {
  5. protected
  6. $listeners = array(),
  7. $context = null,
  8. $currentException = null;
  9. /**
  10. * Calls a request to a uri.
  11. */
  12. protected function doCall()
  13. {
  14. // recycle our context object
  15. $this->context = $this->getContext(true);
  16. sfConfig::set('sf_test', true);
  17. // we register a fake rendering filter
  18. sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
  19. $this->resetCurrentException();
  20. // dispatch our request
  21. ob_start();
  22. $this->context->getController()->dispatch();
  23. $retval = ob_get_clean();
  24. // append retval to the response content
  25. $this->context->getResponse()->setContent($retval);
  26. // manually shutdown user to save current session data
  27. if ($this->context->getUser())
  28. {
  29. $this->context->getUser()->shutdown();
  30. $this->context->getStorage()->shutdown();
  31. }
  32. }
  33. /**
  34. * Returns the current application context.
  35. *
  36. * @param bool $forceReload true to force context reload, false otherwise
  37. *
  38. * @return sfContext
  39. */
  40. public function getContext($forceReload = false)
  41. {
  42. if (null === $this->context || $forceReload)
  43. {
  44. $isContextEmpty = null === $this->context;
  45. $context = $isContextEmpty ? sfContext::getInstance() : $this->context;
  46. // create configuration
  47. $currentConfiguration = $context->getConfiguration();
  48. $configuration = ProjectConfiguration::getApplicationConfiguration($currentConfiguration->getApplication(), $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug());
  49. // connect listeners
  50. $configuration->getEventDispatcher()->connect('application.throw_exception', array($this, 'listenToException'));
  51. foreach ($this->listeners as $name => $listener)
  52. {
  53. $configuration->getEventDispatcher()->connect($name, $listener);
  54. }
  55. // create context
  56. $this->context = sfContext::createInstance($configuration);
  57. unset($currentConfiguration);
  58. if (!$isContextEmpty)
  59. {
  60. sfConfig::clear();
  61. sfConfig::add($this->rawConfiguration);
  62. }
  63. else
  64. {
  65. $this->rawConfiguration = sfConfig::getAll();
  66. }
  67. }
  68. return $this->context;
  69. }
  70. public function addListener($name, $listener)
  71. {
  72. $this->listeners[$name] = $listener;
  73. }
  74. /**
  75. * Gets response.
  76. *
  77. * @return sfWebResponse
  78. */
  79. public function getResponse()
  80. {
  81. return $this->context->getResponse();
  82. }
  83. /**
  84. * Gets request.
  85. *
  86. * @return sfWebRequest
  87. */
  88. public function getRequest()
  89. {
  90. return $this->context->getRequest();
  91. }
  92. /**
  93. * Gets user.
  94. *
  95. * @return sfUser
  96. */
  97. public function getUser()
  98. {
  99. return $this->context->getUser();
  100. }
  101. /**
  102. * Shutdown function to clean up and remove sessions
  103. *
  104. * @return void
  105. */
  106. public function shutdown()
  107. {
  108. parent::shutdown();
  109. // we remove all session data
  110. sfToolkit::clearDirectory(sfConfig::get('sf_test_cache_dir').'/sessions');
  111. }
  112. /**
  113. * Listener for exceptions
  114. *
  115. * @param sfEvent $event The event to handle
  116. *
  117. * @return void
  118. */
  119. public function listenToException(sfEvent $event)
  120. {
  121. $this->setCurrentException($event->getSubject());
  122. }
  123. }

Debug toolbar