[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/test/helpers/ -> lifetypetestcase.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/test/PHPUnit/TestCase.php");
   4      lt_include( PLOG_CLASS_PATH."class/test/helpers/uiscriptrunner.class.php");    
   5      lt_include( PLOG_CLASS_PATH."class/net/http/httpclient.class.php" );
   6      
   7      /**
   8       * \ingroup Test
   9       *
  10       * Extends the PHPUnit's TestCase class to provide support for using asserts
  11       * with HTTP requests. All test suites in LifeType must extend this class instead of
  12       * PHPUnit_TestCase.
  13       *
  14       * Please see http://wiki.lifetype.net/index.php/Unit_Testing_in_LifeType for more
  15       * information on how unit testing has been implemented in LifeType and how to create your
  16       * own test cases.
  17       */
  18      class LifeTypeTestCase extends PHPUnit_TestCase
  19      {        
  20          /** 
  21           * An HTTP client
  22           */
  23          var $c;
  24          
  25          /**
  26           * makes the given HTTP request and checks that the HTTP response code
  27           * matches with the expected once
  28           *
  29           * @param url
  30           * @param expected
  31           * @param message
  32           */
  33  		function assertHTTPResponseCodeEquals( $url, $expected, $message = '' )
  34          {
  35              $c = new HTTPClient();
  36              $result = $c->fetch( $url );
  37              
  38              // check i page loaded correctly
  39              $this->assertTrue( $result, "Could not fetch $url" );
  40              
  41              // get the code from the response and see if the expected one
  42              // is even there            
  43              if( !strpos( $c->response_code, $expected )) {
  44                  $message = $message." expected $expected, actual '".$c->response_code."'";
  45                  $this->fail( $message );
  46              }
  47          }
  48          
  49          /**
  50           * Finds the expected string in the content of the response after fetching the given
  51           * url, and raises an error if not there. 
  52           *
  53           * @param url
  54           * @param expected
  55           * @param message
  56           */
  57  		function assertHTTPResponseContains( $url, $expected, $message = '' )
  58          {
  59              // fetch the url
  60              $c = new HTTPClient();
  61              $result = $c->fetch( $url );        
  62              
  63              $this->assertTrue( $result, "Could not fetch $url" );
  64              
  65              // see if the string we're looking for is in the contents of the page
  66              if( !strstr( $c->results, $expected )) {
  67                  $message = $message." expecting '$expected'";
  68                  return( $this->fail( $message ));
  69              }
  70          }
  71          
  72          /**
  73           * Returns a list with all the available test methods in the class (a test method is 
  74           * a method whose name starst with "test")
  75           *
  76           * @return An array with all the test methods available
  77           */
  78  		function getTestMethods()
  79          {
  80              $testMethods = Array();
  81              
  82              foreach( get_class_methods( $this ) as $method ) {
  83                  if( substr( $method, 0, strlen( "test" )) == "test" ) {
  84                      $testMethods[] = $method;
  85                  }
  86              }
  87              
  88              return( $testMethods );
  89          }
  90          
  91          /**
  92           * Returns a string identifying the suite
  93           *
  94           * @return A string
  95           */
  96  		function getSuiteName()
  97          {
  98              return( str_replace( "_test", "", get_class( $this )));
  99          }
 100  
 101          /**
 102           * Finds the expected string in the content of the response after fetching the given
 103           * url via a POST request
 104           *
 105           * @param url
 106           * @param form An array that will be used as the variables of the form to be POSTed
 107           * @param expected
 108           * @param message
 109           */
 110  		function assertHTTPPostResponseContains( $url, $form, $expected, $message = '' )
 111          {            
 112              // fetch the url
 113              $c = new HTTPClient();
 114              $result = $c->submit( $url, $form );
 115              
 116              $this->assertTrue( $result, "Could not fetch $url" );
 117              
 118              // see if the string we're looking for is in the contents of the page
 119              if( !strstr( $c->results, $expected )) {
 120                  $message = $message." expecting '$expected'";
 121                  return( $this->fail( $message ));
 122              }
 123          }
 124  
 125          /**
 126           * Returns the admin url
 127           */
 128  		function getAdminUrl()
 129          {
 130              $config =& Config::getConfig();
 131              return( $config->getValue( "base_url" )."/admin.php" );
 132          }
 133          
 134          /**
 135           * Assert method that executes the given UI script and assert an error if the
 136           * script was not successfully executed
 137           *
 138           * @param script A UI script
 139           * @see UIScriptRunner
 140           */
 141  		function assertUIScript( $script )
 142          {    
 143              // keep this object as static so that its state is carried accross calls to this method
 144              static $ui;
 145              if(!isset($ui)) {
 146                  $ui = new UIScriptRunner();
 147              }
 148              
 149              $result = $ui->run( $script );            
 150                          
 151              if( !$result ) {
 152                  $message = "Error executing step ".$ui->getFailedStep().": ".$ui->getFailedStepErrorMessage();
 153                  return( $this->fail( $message ));
 154              }
 155              
 156              return( true );
 157          }
 158      }
 159  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics