false,'cookies' => true]); // create client for admin requests self::$client_admin = new \GuzzleHttp\Client(['http_errors' => false,'cookies' => true]); self::UseAdmin(); self::Install(); \gp\tool::GetLangFile(); self::LogIn(); $url = '/phpinfo.php'; $response = self::GuzzleRequest('GET',$url); $body = $response->getBody(); self::$phpinfo = (string)$body; } public static function StartServer(){ global $dataDir; $proc = ['php','-S','localhost:8081']; // doc root $proc[] = '-t'; $proc[] = $dataDir; // '.'; // error log $proc[] = '-d'; $proc[] = 'error_log='.$dataDir . '/data/request-errors.log'; // xdebug configuration to collect code coverage //$proc[] = '-c'; //$proc[] = __DIR__ . '/phpconfig.ini'; $proc[] = '-d'; $proc[] = 'auto_prepend_file='.__DIR__ . '/ServerPrepend.php'; // xdebug profiling /* $profile_dir = sys_get_temp_dir().'/test-profiler'; if( file_exists($profile_dir) ){ \gp\tool\Files::RmAll($profile_dir); } mkdir($profile_dir); $proc[] = '-d'; $proc[] = 'xdebug.profiler_enable=1'; $proc[] = '-d'; $proc[] = 'xdebug.profiler_output_dir='.$profile_dir; */ self::$process = new \Symfony\Component\Process\Process($proc); self::$process->start(function($type,$buffer){ self::$proc_output[] = ['type'=>$type,'buffer'=>(string)$buffer]; }); usleep(100000); //wait for server to get going register_shutdown_function(function(){ self::Console('Stopping server process'); gptest_bootstrap::$process->stop(); usort(self::$tracking,function($a,$b){ if( $a['time'] == $b['time'] ){ return 0; } return ($a['time'] < $b['time']) ? 1 : -1; }); self::Console('Slowest requests (the slowest will usually be because of scss compilation'); self::$tracking = array_slice(self::$tracking,0,5); foreach(self::$tracking as $req){ self::Console(' - '.$req['url'].' in '.$req['class'].' took '.$req['time']); } }); } /** * Switch current guzzle client to $client_admin */ public static function UseAdmin(){ self::$client_current = self::$client_admin; self::$is_admin = true; } /** * Switch current guzzle client to $client_user */ public static function UseAnon(){ self::$client_current = self::$client_user; self::$is_admin = false; } /** * Print process output * */ public static function ProcessOutput($type,$url){ echo "\n\n----------------------------------------------------------------"; self::Console('Begin Process Output: '.$type.' '.$url); echo "\n"; if( !empty(self::$proc_output) ){ self::Console('Proc Output'); print_r(self::$proc_output); } echo "\nEnd Process Output\n----------------------------------------------------------------\n\n"; } /** * Send a GET request to the test server * */ public static function GetRequest( $slug, $query='', $nonce_action=false ){ $url = \gp\tool::GetUrl( $slug, $query, false, $nonce_action); return self::GuzzleRequest('GET',$url); } /** * Send a POST request to the test server * */ public static function PostRequest($slug, $params = []){ $url = \gp\tool::GetUrl($slug); $options = ['form_params' => $params]; return self::GuzzleRequest('POST',$url,200,$options); } /** * Send a request to the test server and check the response * */ public static function GuzzleRequest($type,$url,$expected_resonse = 200, $options = []){ global $dataDir; $start = microtime(true); $response = null; $debug_file = $dataDir . '/data/response-' . self::$requests . '-anon-' . $type . '-' . str_replace('/','_',$url); if( self::$is_admin ){ $debug_file = $dataDir . '/data/response-' . self::$requests . '-admin-' . $type . '-' . str_replace('/','_',$url); } $tracking = [ 'url' => $url, 'is_admin' => self::$is_admin, 'class' => get_called_class(), ]; try{ self::$proc_output = []; $options['headers'] = ['X-REQ-ID' => self::$requests,'Connection'=>'close']; $url = 'http://localhost:8081' . $url; $response = self::$client_current->request($type, $url, $options); $body = $response->getBody(); $status = $response->getStatusCode(); file_put_contents($debug_file, $body); self::$requests++; self::$process->getOutput(); # makes symfony/process populate our self::$proc_output if( $expected_resonse !== $status ){ self::ProcessOutput($type,$url); self::Console('PHPINFO()'); echo (string)self::$phpinfo; } self::assertEquals($expected_resonse, $status); // make sure the response doesn't have