sfBrowser.class.php 3.8 KB

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