Development

/branches/1.2/lib/util/sfBrowser.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/util/sfBrowser.class.php

Revision 15726, 3.8 kB (checked in by fabien, 5 years ago)

[1.1, 1.2, 1.3] fixed sf_culture being set automatically when testing, but not when using the site (closes #5852)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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 (is_null($this->context) || $forceReload)
67     {
68       $isContextEmpty = is_null($this->context);
69       $context = $isContextEmpty ? sfContext::getInstance() : $this->context;
70
71       $currentConfiguration = $context->getConfiguration();
72       $configuration = ProjectConfiguration::getApplicationConfiguration($currentConfiguration->getApplication(), $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug());
73       $this->context = sfContext::createInstance($configuration);
74       unset($currentConfiguration);
75
76       if (!$isContextEmpty)
77       {
78         sfConfig::clear();
79         sfConfig::add($this->rawConfiguration);
80       }
81       else
82       {
83         $this->rawConfiguration = sfConfig::getAll();
84       }
85
86       $this->context->getEventDispatcher()->connect('application.throw_exception', array($this, 'ListenToException'));
87       foreach ($this->listeners as $name => $listener)
88       {
89         $this->context->getEventDispatcher()->connect($name, $listener);
90       }
91     }
92
93     return $this->context;
94   }
95
96   public function addListener($name, $listener)
97   {
98     $this->listeners[$name] = $listener;
99   }
100
101   /**
102    * Gets response.
103    *
104    * @return sfWebResponse
105    */
106   public function getResponse()
107   {
108     return $this->context->getResponse();
109   }
110
111   /**
112    * Gets request.
113    *
114    * @return sfWebRequest
115    */
116   public function getRequest()
117   {
118     return $this->context->getRequest();
119   }
120
121   /**
122    * Gets user.
123    *
124    * @return sfUser
125    */
126   public function getUser()
127   {
128     return $this->context->getUser();
129   }
130
131   /**
132    * Shutdown function to clean up and remove sessions
133    *
134    * @return void
135    */
136   public function shutdown()
137   {
138     parent::shutdown();
139
140     // we remove all session data
141     sfToolkit::clearDirectory(sfConfig::get('sf_test_cache_dir').'/sessions');
142   }
143
144   /**
145    * Listener for exceptions
146    *
147    * @param  sfEvent $event  The event to handle
148    *
149    * @return void
150    */
151   public function listenToException(sfEvent $event)
152   {
153     $this->setCurrentException($event->getSubject());
154   }
155 }
156
157 class sfFakeRenderingFilter extends sfFilter
158 {
159   public function execute($filterChain)
160   {
161     $filterChain->execute();
162
163     $this->context->getResponse()->sendContent();
164   }
165 }
166
Note: See TracBrowser for help on using the browser.