/**
  * Retrieve the parameters from the context according to their name in the callback function
  *
  * @param TestSuite $context
  * @param bool      $with_names
  *
  * @return array
  */
 public function parameters($context, $with_names = true)
 {
     $parameters = $this->reflection->getParameters();
     $call_parameters = [];
     foreach ($parameters as $i => $param) {
         $call_parameters[$i] =& $context->get_value($param->getName());
         if ($with_names) {
             $call_parameters[$param->getName()] =& $call_parameters[$i];
         }
     }
     return $call_parameters;
 }
Exemple #2
0
 /**
  * The then keyword
  * Run the actual test
  * All given values needed will be parsed, and will execute every actions given by when
  * Will store the result of the test for further use
  *
  * @param callback $callback
  * @param string   $label
  *
  * @return void
  * @throws Exception
  */
 public function then($callback, $label)
 {
     $saved = clone $this->current_suite;
     $result = $this->current_suite->run($callback);
     $this->current_suite = $saved;
     $testNumber = count($this->results);
     $testDescription = $this->current_suite->description();
     $this->results[] = $result;
     if ($result->is_error()) {
         $this->errors[] = $result;
         $this->labels[] = $label;
         $this->reporter->reportFailure($testNumber, $testDescription);
     } else {
         $this->reporter->reportSuccess($testNumber, $testDescription);
     }
 }
    /**
     * Render a failing test
     *
     * @param int    $n
     * @param string $label
     *
     * @return void
     */
    public function render($n, $label)
    {
        echo PHP_EOL . PHP_EOL;
        $red = chr(27) . '[31m';
        $blue = chr(27) . '[34m';
        $white = chr(27) . '[0m';
        $completeLabel = '';
        if ($label !== null) {
            $labels = $this->context->labels();
            foreach ($labels as $i => $l) {
                if (!$l->isEmpty()) {
                    $completeLabel .= '       ' . $l . ' -> ' . $this->context->get_value($i) . PHP_EOL;
                }
            }
            $completeLabel .= '       Then ' . $label . PHP_EOL;
        }
        echo <<<FAILURE
  {$n}) {$this->context->description()}   Then { {$this->callback->code()} }

     {$red}Failure/Error: Then { {$this->callback->code()} }
{$completeLabel}
       Then expression failed at {$this->callback->file()}:{$this->callback->line()}
FAILURE;
        $parameters = $this->callback->parameters($this->context);
        foreach ($parameters as $i => $parameter) {
            if (is_int($i)) {
                continue;
            }
            echo PHP_EOL . "       {$this->format_value($parameter)} <- {$i}";
        }
        echo $blue;
        foreach ($this->stack as $error) {
            echo PHP_EOL . "     # {$error['file']}:{$error['line']}:in `{$error['function']}'";
        }
        echo $white;
    }