123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfBrowser simulates a browser which can surf a symfony application.
- *
- * @package symfony
- * @subpackage util
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfBrowser.class.php 15726 2009-02-23 15:56:12Z fabien $
- */
- class sfBrowser extends sfBrowserBase
- {
- protected
- $listeners = array(),
- $context = null,
- $currentException = null;
- /**
- * Calls a request to a uri.
- */
- protected function doCall()
- {
- // recycle our context object
- $this->context = $this->getContext(true);
- sfConfig::set('sf_test', true);
- // we register a fake rendering filter
- sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
- $this->resetCurrentException();
- // dispatch our request
- ob_start();
- $this->context->getController()->dispatch();
- $retval = ob_get_clean();
- // append retval to the response content
- $this->context->getResponse()->setContent($retval);
- // manually shutdown user to save current session data
- if ($this->context->getUser())
- {
- $this->context->getUser()->shutdown();
- $this->context->getStorage()->shutdown();
- }
- }
- /**
- * Returns the current application context.
- *
- * @param bool $forceReload true to force context reload, false otherwise
- *
- * @return sfContext
- */
- public function getContext($forceReload = false)
- {
- if (is_null($this->context) || $forceReload)
- {
- $isContextEmpty = is_null($this->context);
- $context = $isContextEmpty ? sfContext::getInstance() : $this->context;
- $currentConfiguration = $context->getConfiguration();
- $configuration = ProjectConfiguration::getApplicationConfiguration($currentConfiguration->getApplication(), $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug());
- $this->context = sfContext::createInstance($configuration);
- unset($currentConfiguration);
- if (!$isContextEmpty)
- {
- sfConfig::clear();
- sfConfig::add($this->rawConfiguration);
- }
- else
- {
- $this->rawConfiguration = sfConfig::getAll();
- }
- $this->context->getEventDispatcher()->connect('application.throw_exception', array($this, 'ListenToException'));
- foreach ($this->listeners as $name => $listener)
- {
- $this->context->getEventDispatcher()->connect($name, $listener);
- }
- }
- return $this->context;
- }
- public function addListener($name, $listener)
- {
- $this->listeners[$name] = $listener;
- }
- /**
- * Gets response.
- *
- * @return sfWebResponse
- */
- public function getResponse()
- {
- return $this->context->getResponse();
- }
- /**
- * Gets request.
- *
- * @return sfWebRequest
- */
- public function getRequest()
- {
- return $this->context->getRequest();
- }
- /**
- * Gets user.
- *
- * @return sfUser
- */
- public function getUser()
- {
- return $this->context->getUser();
- }
- /**
- * Shutdown function to clean up and remove sessions
- *
- * @return void
- */
- public function shutdown()
- {
- parent::shutdown();
- // we remove all session data
- sfToolkit::clearDirectory(sfConfig::get('sf_test_cache_dir').'/sessions');
- }
- /**
- * Listener for exceptions
- *
- * @param sfEvent $event The event to handle
- *
- * @return void
- */
- public function listenToException(sfEvent $event)
- {
- $this->setCurrentException($event->getSubject());
- }
- }
- class sfFakeRenderingFilter extends sfFilter
- {
- public function execute($filterChain)
- {
- $filterChain->execute();
- $this->context->getResponse()->sendContent();
- }
- }
|