[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/test/ -> sfTestBrowser.class.php (source)

   1  <?php
   2  
   3  require_once(dirname(__FILE__).'/../vendor/lime/lime.php');
   4  
   5  /*
   6   * This file is part of the symfony package.
   7   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   8   *
   9   * For the full copyright and license information, please view the LICENSE
  10   * file that was distributed with this source code.
  11   */
  12  
  13  /**
  14   * sfTestBrowser simulates a fake browser which can test a symfony application.
  15   *
  16   * @package    symfony
  17   * @subpackage test
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @version    SVN: $Id: sfTestBrowser.class.php 3329 2007-01-23 08:29:34Z fabien $
  20   */
  21  class sfTestBrowser extends sfBrowser
  22  {
  23    protected
  24      $test = null;
  25  
  26    /**
  27     * Initializes the browser tester instance.
  28     *
  29     * @param string Hostname
  30     * @param string Remote IP address
  31     * @param array  Options
  32     */
  33    public function initialize($hostname = null, $remote = null, $options = array())
  34    {
  35      parent::initialize($hostname, $remote, $options);
  36  
  37      $output = isset($options['output']) ? $options['output'] : new lime_output_color();
  38  
  39      $this->test = new lime_test(null, $output);
  40    }
  41  
  42    /**
  43     * Retrieves the lime_test instance.
  44     *
  45     * @return sfTestBrowser The lime_test instance
  46     */
  47    public function test()
  48    {
  49      return $this->test;
  50    }
  51  
  52    /**
  53     * Retrieves and checks an action.
  54     *
  55     * @param string Module name
  56     * @param string Action name
  57     * @param string Url
  58     * @param string The expected return status code
  59     *
  60     * @return sfBrowser The current sfBrowser instance
  61     */
  62    public function getAndCheck($module, $action, $url = null, $code = 200)
  63    {
  64      return $this->
  65        get(null !== null ? $url : sprintf('/%s/%s', $module, $action))->
  66        isStatusCode($code)->
  67        isRequestParameter('module', $module)->
  68        isRequestParameter('action', $action)
  69      ;
  70    }
  71  
  72    /**
  73     * Calls a request.
  74     *
  75     * @return sfBrowser The current sfBrowser instance
  76     */
  77    public function call($uri, $method = 'get', $parameters = array(), $changeStack = true)
  78    {
  79      $uri = $this->fixUri($uri);
  80  
  81      $this->test->comment(sprintf('%s %s', strtolower($method), $uri));
  82  
  83      return parent::call($uri, $method, $parameters, $changeStack);
  84    }
  85  
  86    /**
  87     * Simulates the browser back button.
  88     *
  89     * @return sfBrowser The current sfBrowser instance
  90     */
  91    public function back()
  92    {
  93      $this->test->comment('back');
  94  
  95      return parent::back();
  96    }
  97  
  98    /**
  99     * Simulates the browser forward button.
 100     *
 101     * @return sfBrowser The current sfBrowser instance
 102     */
 103    public function forward()
 104    {
 105      $this->test->comment('forward');
 106  
 107      return parent::forward();
 108    }
 109  
 110    /**
 111     * Tests if the current request has been redirected.
 112     *
 113     * @param boolean Flag for redirection mode
 114     *
 115     * @return sfBrowser The current sfBrowser instance
 116     */
 117    public function isRedirected($boolean = true)
 118    {
 119      if ($location = $this->getContext()->getResponse()->getHttpHeader('location'))
 120      {
 121        $boolean ? $this->test->pass(sprintf('page redirected to "%s"', $location)) : $this->test->fail(sprintf('page redirected to "%s"', $location));
 122      }
 123      else
 124      {
 125        $boolean ? $this->test->fail('page redirected') : $this->test->pass('page not redirected');
 126      }
 127  
 128      return $this;
 129    }
 130  
 131    /**
 132     * Checks that the current response contains a given text.
 133     *
 134     * @param string Uniform resource identifier
 135     * @param string Text in the response
 136     *
 137     * @return sfBrowser The current sfBrowser instance
 138     */
 139    public function check($uri, $text = null)
 140    {
 141      $this->get($uri)->isStatusCode();
 142  
 143      if ($text !== null)
 144      {
 145        $this->responseContains($text);
 146      }
 147  
 148      return $this;
 149    }
 150  
 151    /**
 152     * Test an status code for the current test browser.
 153     *
 154     * @param string Status code to check, default 200
 155     *
 156     * @return sfBrowser The current sfBrowser instance
 157     */
 158    public function isStatusCode($statusCode = 200)
 159    {
 160      $this->test->is($this->getResponse()->getStatusCode(), $statusCode, sprintf('status code is "%s"', $statusCode));
 161  
 162      return $this;
 163    }
 164  
 165    /**
 166     * Tests whether or not a given string is in the response.
 167     *
 168     * @param string Text to check
 169     *
 170     * @return sfBrowser The current sfBrowser instance
 171     */
 172    public function responseContains($text)
 173    {
 174      $this->test->like($this->getResponse()->getContent(), '/'.preg_quote($text, '/').'/', sprintf('response contains "%s"', substr($text, 0, 40)));
 175  
 176      return $this;
 177    }
 178  
 179    /**
 180     * Tests whether or not a given key and value exists in the current request.
 181     *
 182     * @param string Key
 183     * @param string Value
 184     *
 185     * @return sfBrowser The current sfBrowser instance
 186     */
 187    public function isRequestParameter($key, $value)
 188    {
 189      $this->test->is($this->getRequest()->getParameter($key), $value, sprintf('request parameter "%s" is "%s"', $key, $value));
 190  
 191      return $this;
 192    }
 193  
 194    /**
 195     * Checks that the request is forwarded to a given module/action.
 196     *
 197     * @param string The module name
 198     * @param string The action name
 199     * @param mixed  The position in the action stack (default to the last entry)
 200     *
 201     * @return object this
 202     */
 203    public function isForwardedTo($moduleName, $actionName, $position = 'last')
 204    {
 205      $actionStack = $this->getContext()->getActionStack();
 206  
 207      switch ($position)
 208      {
 209        case 'first':
 210          $entry = $actionStack->getFirstEntry();
 211          break;
 212        case 'last':
 213          $entry = $actionStack->getLastEntry();
 214          break;
 215        default:
 216          $entry = $actionStack->getEntry($position);
 217      }
 218  
 219      $this->test->is($entry->getModuleName(), $moduleName, sprintf('request is forwarded to the "%s" module (%s)', $moduleName, $position));
 220      $this->test->is($entry->getActionName(), $actionName, sprintf('request is forwarded to the "%s" action (%s)', $actionName, $position));
 221  
 222      return $this;
 223    }
 224  
 225    /**
 226     * Tests for a response header.
 227     *
 228     * @param string Key
 229     * @param string Value
 230     *
 231     * @return sfBrowser The current sfBrowser instance
 232     */
 233    public function isResponseHeader($key, $value)
 234    {
 235      $headers = $this->getResponse()->getHttpHeader($key);
 236  
 237      $ok = false;
 238      foreach ($headers as $header)
 239      {
 240        if ($header == $value)
 241        {
 242          $ok = true;
 243          break;
 244        }
 245      }
 246  
 247      $this->test->ok($ok, sprintf('response header "%s" is "%s"', $key, $value));
 248  
 249      return $this;
 250    }
 251  
 252    /**
 253     * Tests that the current response matches a given CSS selector.
 254     *
 255     * @param string The response selector
 256     * @param string Flag for the selector
 257     * @param array Options for the current test
 258     *
 259     * @return sfBrowser The current sfBrowser instance
 260     */
 261    public function checkResponseElement($selector, $value = true, $options = array())
 262    {
 263      $texts = $this->getResponseDomCssSelector()->getTexts($selector);
 264  
 265      if (false === $value)
 266      {
 267        $this->test->is(count($texts), 0, sprintf('response selector "%s" does not exist', $selector));
 268      }
 269      else if (true === $value)
 270      {
 271        $this->test->cmp_ok(count($texts), '>', 0, sprintf('response selector "%s" exists', $selector));
 272      }
 273      else if (is_int($value))
 274      {
 275        $this->test->is(count($texts), $value, sprintf('response selector "%s" matches "%s" times', $selector, $value));
 276      }
 277      else if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
 278      {
 279        $position = isset($options['position']) ? $options['position'] : 0;
 280        if ($match[1] == '!')
 281        {
 282          $this->test->unlike(@$texts[$position], substr($value, 1), sprintf('response selector "%s" does not match regex "%s"', $selector, substr($value, 1)));
 283        }
 284        else
 285        {
 286          $this->test->like(@$texts[$position], $value, sprintf('response selector "%s" matches regex "%s"', $selector, $value));
 287        }
 288      }
 289      else
 290      {
 291        $position = isset($options['position']) ? $options['position'] : 0;
 292        $this->test->is(@$texts[$position], $value, sprintf('response selector "%s" matches "%s"', $selector, $value));
 293      }
 294  
 295      if (isset($options['count']))
 296      {
 297        $this->test->is(count($texts), $options['count'], sprintf('response selector "%s" matches "%s" times', $selector, $options['count']));
 298      }
 299  
 300      return $this;
 301    }
 302  
 303    /**
 304     * Tests if an exception is thrown by the latest request.
 305     *
 306     * @param string Class name
 307     * @param string Message name
 308     *
 309     * @return sfBrowser The current sfBrowser instance
 310     */
 311    public function throwsException($class = null, $message = null)
 312    {
 313      $e = $this->getCurrentException();
 314  
 315      if (null === $e)
 316      {
 317        $this->test->fail('response returns an exception');
 318      }
 319      else
 320      {
 321        if (null !== $class)
 322        {
 323          $this->test->ok($e instanceof $class, sprintf('response returns an exception of class "%s"', $class));
 324        }
 325  
 326        if (null !== $message && preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $message, $match))
 327        {
 328          if ($match[1] == '!')
 329          {
 330            $this->test->unlike($e->getMessage(), substr($message, 1), sprintf('response exception message does not match regex "%s"', $message));
 331          }
 332          else
 333          {
 334            $this->test->like($e->getMessage(), $message, sprintf('response exception message matches regex "%s"', $message));
 335          }
 336        }
 337        else if (null !== $message)
 338        {
 339          $this->test->is($e->getMessage(), $message, sprintf('response exception message matches regex "%s"', $message));
 340        }
 341      }
 342  
 343      return $this;
 344    }
 345  
 346    /**
 347     * Tests if the given uri is cached.
 348     *
 349     * @param boolean Flag for checking the cache
 350     * @param boolean If have or not layout
 351     *
 352     * @return sfBrowser The current sfBrowser instance
 353     */
 354    public function isCached($boolean, $with_layout = false)
 355    {
 356      return $this->isUriCached(sfRouting::getInstance()->getCurrentInternalUri(), $boolean, $with_layout);
 357    }
 358  
 359    /**
 360     * Tests if the given uri is cached.
 361     *
 362     * @param string Uniform resource identifier
 363     * @param boolean Flag for checking the cache
 364     * @param boolean If have or not layout
 365     *
 366     * @param sfTestBrowser Test browser instance
 367     */
 368    public function isUriCached($uri, $boolean, $with_layout = false)
 369    {
 370      $cacheManager = $this->getContext()->getViewCacheManager();
 371  
 372      // check that cache is enabled
 373      if (!$cacheManager)
 374      {
 375        $this->test->ok(!$boolean, 'cache is disabled');
 376  
 377        return $this;
 378      }
 379  
 380      if ($uri == sfRouting::getInstance()->getCurrentInternalUri())
 381      {
 382        $main = true;
 383        $type = $with_layout ? 'page' : 'action';
 384      }
 385      else
 386      {
 387        $main = false;
 388        $type = $uri;
 389      }
 390  
 391      // check layout configuration
 392      if ($cacheManager->withLayout($uri) && !$with_layout)
 393      {
 394        $this->test->fail('cache without layout');
 395        $this->test->skip('cache is not configured properly', 2);
 396      }
 397      else if (!$cacheManager->withLayout($uri) && $with_layout)
 398      {
 399        $this->test->fail('cache with layout');
 400        $this->test->skip('cache is not configured properly', 2);
 401      }
 402      else
 403      {
 404        $this->test->pass('cache is configured properly');
 405  
 406        // check page is cached
 407        $ret = $this->test->is($cacheManager->has($uri), $boolean, sprintf('"%s" %s in cache', $type, $boolean ? 'is' : 'is not'));
 408  
 409        // check that the content is ok in cache
 410        if ($boolean)
 411        {
 412          if (!$ret)
 413          {
 414            $this->test->fail('content in cache is ok');
 415          }
 416          else if ($with_layout)
 417          {
 418            $response = unserialize($cacheManager->get($uri));
 419            $content = $response->getContent();
 420            $this->test->ok($content == $this->getResponse()->getContent(), 'content in cache is ok');
 421          }
 422          else if (true === $main)
 423          {
 424            $ret = unserialize($cacheManager->get($uri));
 425            $content = $ret['content'];
 426            $this->test->ok(false !== strpos($this->getResponse()->getContent(), $content), 'content in cache is ok');
 427          }
 428          else
 429          {
 430            $content = $cacheManager->get($uri);
 431            $this->test->ok(false !== strpos($this->getResponse()->getContent(), $content), 'content in cache is ok');
 432          }
 433        }
 434      }
 435  
 436      return $this;
 437    }
 438  }
 439  
 440  /**
 441   * Error handler for the current test browser instance.
 442   *
 443   * @param mixed Error number
 444   * @param string Error message
 445   * @param string Error file
 446   * @param mixed Error line
 447   */
 448  function sfTestBrowserErrorHandler($errno, $errstr, $errfile, $errline)
 449  {
 450    if (($errno & error_reporting()) == 0)
 451    {
 452      return;
 453    }
 454  
 455    $msg = sprintf('PHP send a "%s" error at %s line %s (%s)', '%s', $errfile, $errline, $errstr);
 456    switch ($errno)
 457    {
 458      case E_WARNING:
 459        throw new Exception(sprintf($msg, 'warning'));
 460        break;
 461      case E_NOTICE:
 462        throw new Exception(sprintf($msg, 'notice'));
 463        break;
 464      case E_STRICT:
 465        throw new Exception(sprintf($msg, 'strict'));
 466        break;
 467    }
 468  }
 469  
 470  set_error_handler('sfTestBrowserErrorHandler');


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7