/**
  * Return request index which is defined in config with updated query data, leaving global values untouched.
  *
  * @param $getrequests
  * @param $postrequests
  * @param $postdata
  * @param $requestsfromconfig
  * @return array requestindex, method and path.
  */
 private function is_request_in_config($capturelabel, $requests)
 {
     $requestsfromconfig = $this->get_capture_request_from_config($capturelabel, \behat_hooks::$featurefile);
     $requestfromhar = null;
     // Check if request is already set by user in config.
     if (!empty($requestsfromconfig)) {
         // Loop though each request from har and find the appropriate one.
         // We need this to replace query values except the global set by user.
         foreach ($requests as $index => $request) {
             if ($requestsfromconfig['method'] == $request['method'] && $requestsfromconfig['path'] == $request['path']) {
                 break;
             }
         }
         if (empty($request)) {
             util::performance_exception("Request from har is not found in config. Check: " . \behat_hooks::$featurefile . " : " . $capturelabel);
         }
         // Replace global values in query array with config values.
         foreach ($requestsfromconfig['query'] as $key => $value) {
             if (preg_match('/^\\$\\{.*\\}/', $value)) {
                 $request['query'][$key] = $value;
             }
         }
         return $request;
     }
     return array();
 }
 /**
  * Method for creating a new HAR file
  *
  * @param string $label optional label
  *
  * @return string
  */
 public static function new_har($label = '')
 {
     $proxyurl = util::get_option('proxyurl');
     $proxyport = util::get_option('proxyport');
     $data = array("captureContent" => 'true', "initialPageRef" => $label, "captureHeaders" => 'true', "captureBinaryContent" => 'true');
     $url = $proxyurl . "/proxy/" . $proxyport . "/har";
     $response = self::curl_put($url, $data);
     return $response;
 }
 /**
  * After suite event.
  *
  * @param SuiteEvent $event
  * @AfterSuite
  */
 public static function after_suite(SuiteEvent $event)
 {
     $browsermobproxy = new browsermobproxyclient(util::get_option('proxyurl'));
     $browsermobproxy->close_connection();
     echo PHP_EOL . "Test plan has been generated under:" . PHP_EOL;
     echo " - " . util::get_final_testplan_path() . PHP_EOL;
 }
 /**
  * Save final har data approved by user.
  *
  * @param string $capturelabel capture label
  * @param array $request request to be saved.
  * @return bool true
  */
 public static function save_final_har_data($capturelabel, $request)
 {
     $updatedtestplanconfigfile = self::get_final_testplan_path() . DIRECTORY_SEPARATOR . 'testplan.json-dist';
     // If we have already written something then update the new file.
     if (file_exists($updatedtestplanconfigfile)) {
         $config = file_get_contents($updatedtestplanconfigfile);
         $config = json_decode($config, true);
     } else {
         $config = util::get_config();
     }
     $config['scenarios'][\behat_hooks::$featurefile]['requests'][$capturelabel] = $request;
     // Update config so it can be used in final release.
     file_put_contents($updatedtestplanconfigfile, json_encode($config));
     // Remove har file as it's work is done.
     unlink(self::get_har_file_path($capturelabel));
     return true;
 }
 /**
  * Execute behat command for featurename and return exit status.
  *
  * @return int status code.
  */
 protected function execute_behat_generator()
 {
     $cmd = "vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml ';
     $process = new Process($cmd);
     $process->setWorkingDirectory(__DIR__ . "/../../../../../");
     $process->setTimeout(null);
     $process->start();
     if ($process->getStatus() !== 'started') {
         echo "Error starting process";
         $process->signal(SIGKILL);
         exit(1);
     }
     while ($process->isRunning()) {
         $output = $process->getIncrementalOutput();
         $op = trim($output);
         if (!empty($op)) {
             echo $output;
         }
     }
     if ($process->getExitCode() !== 0) {
         echo $process->getErrorOutput();
     }
     return $process->getExitCode();
 }
 /**
  * Return xml for the given tag.
  *
  * @param string $tag name of the tag
  * @param array $replacements search=> replace in xml. with #!search!# => replace
  * @return string xml for the testplan tag.
  * @throws \moodle_exception
  */
 public static function get_testplan_tag_xml($tag, $replacements = array())
 {
     $xmlfilepath = __DIR__ . "/../fixtures/" . $tag . ".xml";
     if (!file_exists($xmlfilepath)) {
         util::performance_exception("Xml for tag " . $tag . " not found");
     }
     $tagxml = file_get_contents($xmlfilepath);
     // Replace all required values.
     foreach ($replacements as $search => $replace) {
         $tagxml = str_replace('#!' . $search . '!#', $replace, $tagxml);
     }
     return $tagxml;
 }