Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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