Author: Fabien Potencier (fabien@blackfire.io)
Example #1
0
 public function __construct(ValueBag $values, ValueBag $extra, \Exception $error = null)
 {
     $this->values = $values->all();
     $this->valueBag = $values;
     $this->extra = $extra;
     $this->error = $error;
 }
Example #2
0
 public function postRun(Scenario $scenario, ValueBag $values, ValueBag $extra)
 {
     if ($extra->has('blackfire_build')) {
         $build = $extra->get('blackfire_build');
         // did we profiled anything?
         if (!$build->getJobCount()) {
             // don't finish the build as it won't work with 0 profiles
             $this->logger->error(sprintf('Report "%s" aborted as it has no profiles', $scenario->getTitle()));
             $extra->remove('blackfire_build');
             return;
         }
         $extra->set('blackfire_report', $report = $this->blackfire->endBuild($build));
         $extra->remove('blackfire_build');
     }
     // avoid getting the report if not needed
     if (!$this->logger) {
         return;
     }
     try {
         if ($report->isErrored()) {
             $this->logger->critical(sprintf('Report "%s" errored', $scenario->getTitle()));
         } else {
             if ($report->isSuccessful()) {
                 $this->logger->debug(sprintf('Report "%s" pass', $scenario->getTitle()));
             } else {
                 $this->logger->error(sprintf('Report "%s" failed', $scenario->getTitle()));
             }
         }
     } catch (BlackfireException $e) {
         $this->logger->critical(sprintf('Report "%s" is not available (%s)', $scenario->getTitle(), $e->getMessage()));
     }
     $this->logger->info(sprintf('Report "%s" URL: %s', $scenario->getTitle(), $report->getUrl()));
 }
Example #3
0
 private function extractVariables($extractions, ValueBag $values = null, Crawler $crawler = null, RequestInterface $request, ResponseInterface $response)
 {
     if (null === $values) {
         throw new LogicException('Unable to extract variables if no ValueBag is registered.');
     }
     $variables = $this->createVariables($response, $crawler);
     foreach ($extractions as $name => $extract) {
         list($expression, $attributes) = $extract;
         if (!is_array($attributes)) {
             $attributes = [$attributes];
         }
         try {
             $data = $this->language->evaluate($expression, $variables + $values->all(true));
             if ($data instanceof Crawler) {
                 $value = $data->extract($attributes);
                 if (count($attributes) == 1) {
                     $data = count($data) > 1 ? $value : $value[0];
                 } else {
                     $data = $value;
                 }
             }
             $values->set($name, $data);
         } catch (ExpressionSyntaxError $e) {
             $msg = sprintf('Syntax Error in "%s": %s', $expression, $e->getMessage());
             $this->logger and $this->logger->critical($msg, ['request' => $request->getHeaderLine('X-Request-Id')]);
             throw new ExpectationErrorException($msg);
         }
     }
 }
Example #4
0
 private function prepareRequest(Step $step, ValueBag $values, RequestInterface $request, $options)
 {
     $options['allow_redirects'] = false;
     if (!$step->getDelay()) {
         $options['delay'] = 0;
     } else {
         try {
             $options['delay'] = $this->language->evaluate($step->getDelay(), $values->all(true));
         } catch (ExpressionSyntaxError $e) {
             $msg = sprintf('Delay syntax error in "%s": %s', $step->getDelay(), $e->getMessage());
             $this->logger and $this->logger->critical($msg, ['request' => $request->getHeaderLine('X-Request-Id')]);
             throw new InvalidArgumentException($msg);
         }
     }
     unset($options['expectations']);
     if ($step->getExpectations()) {
         $options['expectations'] = $step->getExpectations();
     }
     unset($options['extractions']);
     if ($step->getExtractions()) {
         $options['extractions'] = $step->getExtractions();
     }
     foreach ($this->extensions as $extension) {
         $options = $extension->prepareRequest($step, $values, $request, $options);
     }
     return $options;
 }
Example #5
0
 private function evaluateValues(ValueBag $values, $data)
 {
     if (is_string($data)) {
         return $this->language->evaluate($data, $values->all(false));
     }
     foreach ($data as $key => $value) {
         $data[$key] = $this->language->evaluate($value, $values->all(false));
     }
     return $data;
 }
 private function extractVariables($extractions, ValueBag $values = null, Crawler $crawler = null, RequestInterface $request, ResponseInterface $response)
 {
     if (null === $values) {
         throw new LogicException('Unable to extract variables if no ValueBag is registered.');
     }
     $variables = $this->createVariables($response, $crawler);
     foreach ($extractions as $name => $expression) {
         try {
             $data = $this->language->evaluate($expression, $variables + $values->all(true));
             if ($data instanceof Crawler) {
                 $data = $data->extract('_text');
                 $data = count($data) > 1 ? $data : $data[0];
             }
             $msg = sprintf('Extracting value form "%s": %s', $name, is_scalar($data) ? $data : '[Non-scalar-data]');
             $this->logger and $this->logger->debug($msg, ['request' => $request->getHeaderLine('X-Request-Id')]);
             $values->set($name, $data);
         } catch (ExpressionSyntaxError $e) {
             $msg = sprintf('Syntax Error in "%s": %s', $expression, $e->getMessage());
             $this->logger and $this->logger->critical($msg, ['request' => $request->getHeaderLine('X-Request-Id')]);
             throw new ExpectationErrorException($msg);
         }
     }
 }