[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/t/lib/ -> testcase-request.php (source)

   1  <?php
   2  
   3  class GP_UnitTestCase_Request extends GP_UnitTestCase {
   4      var $body = null;
   5  
   6      function get( $uri, $get_vars = array() ) {
   7          $this->request( $uri, 'GET', $get_vars );
   8      }
   9  
  10      function post( $uri, $get_vars = array() ) {
  11          $this->request( $uri, 'POST', $get_vars );
  12      }
  13  
  14      private function request( $uri, $method, $vars ) {
  15          $tmp_file_name = tempnam( sys_get_temp_dir(), 'gp-test-request-config' );
  16          if ( !$tmp_file_name) {
  17              return false;
  18          }
  19          $config_vars = array(
  20              'upper_method' => strtoupper( $method ),
  21              'vars' => $vars,
  22              'uri' => $uri,
  23              'gp_config_path' => dirname( __FILE__ ) . '/../unittests-config.php',
  24          );
  25          extract( array_map( create_function( '$value', 'return var_export( $value, true );' ), $config_vars ) );
  26          $config_php_code = <<<CONFIG
  27  <?php
  28  \$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
  29  \$_SERVER['REQUEST_URI'] = $uri;
  30  \$GLOBALS['_'.$upper_method] = $vars;
  31  \$_SERVER['REQUEST_METHOD'] = $upper_method;
  32  define( 'GP_CONFIG_FILE', $gp_config_path );
  33  CONFIG;
  34          file_put_contents( $tmp_file_name, $config_php_code );
  35          ob_start();
  36          /* We need to start a new PHP process, because header() doesn't like previous output and we have plenty */
  37          system('php '.escapeshellarg( dirname( __FILE__ ) . '/../bin/request.php' ).' '.escapeshellarg( $tmp_file_name ) );
  38          /* We can't get the headers, because there is no way to tell the CLI SAPI to return them */
  39          $this->body = ob_get_contents();
  40          ob_end_clean();
  41      }
  42  
  43      function assertRedirect() {
  44          $this->assertTrue( gp_startswith( $this->body, 'Redirecting to: ') );
  45      }
  46  
  47      function assertResponseContains( $needle ) {
  48          $this->assertTrue( gp_in( $needle, $this->body ) );
  49      }
  50  
  51      function assertResponseNotContains( $needle ) {
  52          $this->assertFalse( gp_in( $needle, $this->body ) );
  53      }
  54  }


Generated: Fri May 24 04:00:00 2013 Hosted by follow the white rabbit.