/** * Attempts to translate definition using translator and produce translated one on success. * * @param Suite $suite * @param Definition $definition * @param null|string $language * * @return Definition|TranslatedDefinition */ public function translateDefinition(Suite $suite, Definition $definition, $language = null) { $assetsId = $suite->getName(); $pattern = $definition->getPattern(); $translatedPattern = $this->translator->trans($pattern, array(), $assetsId, $language); if ($pattern != $translatedPattern) { return new TranslatedDefinition($definition, $translatedPattern, $language); } return $definition; }
protected final function getDefinitionType(Definition $definition, $onlyOne = false) { $this->keywords->setLanguage($this->translator->getLocale()); $method = 'get' . ucfirst($definition->getType()) . 'Keywords'; $keywords = explode('|', $this->keywords->{$method}()); if ($onlyOne) { return current($keywords); } return 1 < count($keywords) ? '[' . implode('|', $keywords) . ']' : implode('|', $keywords); }
/** * Colorizes step text arguments according to definition. * * @param string $text * @param Definition $definition * @param TestResult $result * * @return string */ public function paintText($text, Definition $definition, TestResult $result) { $regex = $this->patternTransformer->transformPatternToRegex($definition->getPattern()); $style = $this->resultConverter->convertResultToString($result); $paramStyle = $style . '_param'; // If it's just a string - skip if ('/' !== substr($regex, 0, 1)) { return $text; } // Find arguments with offsets $matches = array(); preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE); array_shift($matches); // Replace arguments with colorized ones $shift = 0; $lastReplacementPosition = 0; foreach ($matches as $key => $match) { if (!is_numeric($key) || -1 === $match[1] || false !== strpos($match[0], '<')) { continue; } $offset = $match[1] + $shift; $value = $match[0]; // Skip inner matches if ($lastReplacementPosition > $offset) { continue; } $lastReplacementPosition = $offset + strlen($value); $begin = substr($text, 0, $offset); $end = substr($text, $lastReplacementPosition); $format = "{-{$style}}{+{$paramStyle}}%s{-{$paramStyle}}{+{$style}}"; $text = sprintf("%s{$format}%s", $begin, $value, $end); // Keep track of how many extra characters are added $shift += strlen($format) - 2; $lastReplacementPosition += strlen($format) - 2; } // Replace "<", ">" with colorized ones $text = preg_replace('/(<[^>]+>)/', "{-{$style}}{+{$paramStyle}}\$1{-{$paramStyle}}{+{$style}}", $text); return $text; }
/** * Initializes redundant exception. * * @param Behat\Behat\Definition\Definition $step2 duplicate step definition * @param Behat\Behat\Definition\Definition $step1 firstly matched step definition */ public function __construct(Definition $step2, Definition $step1) { $message = sprintf("Step \"%s\" is already defined in %s:%d\n\n%s:%d\n%s:%d", $step2->getRegex(), $step1->getFile(), $step1->getLine(), $step1->getFile(), $step1->getLine(), $step2->getFile(), $step2->getLine()); parent::__construct($message); }
/** * Extracts the formatted footer from the definition. * * @param Suite $suite * @param Definition $definition * * @return string[] */ private function extractFooter(Suite $suite, Definition $definition) { $lines = array(); $lines[] = strtr('{space}<def_dimmed>|</def_dimmed> at `{path}`', array('{space}' => str_pad('', mb_strlen($suite->getName(), 'utf8') + 1), '{path}' => $definition->getPath())); return $lines; }
/** * Prints path to step. * * @param Behat\Gherkin\Node\StepNode $step step node * @param Behat\Behat\Definition\Definition $definition definition (if step defined) * @param Exception $exception exception (if step failed) */ protected function printStepPath(StepNode $step, Definition $definition = null, \Exception $exception = null) { $color = $exception instanceof Pending ? 'pending' : 'failed'; $type = $step->getType(); $text = $step->getText(); $stepPath = "In step `{$type} {$text}'."; $stepPathLn = mb_strlen($stepPath); $node = $step->getParent(); if ($node instanceof BackgroundNode) { $scenarioPath = "From scenario background."; } else { $title = $node->getTitle(); $title = $title ? "`{$title}'" : '***'; $scenarioPath = "From scenario {$title}."; } $scenarioPathLn = mb_strlen($scenarioPath); $this->maxLineLength = max($this->maxLineLength, $stepPathLn); $this->maxLineLength = max($this->maxLineLength, $scenarioPathLn); $this->write(" {+{$color}}{$stepPath}{-{$color}}"); if (null !== $definition) { $indentCount = $this->maxLineLength - $stepPathLn; $this->printPathComment($definition->getFile(), $definition->getLine(), $indentCount); } else { $this->writeln(); } $this->write(" {+{$color}}{$scenarioPath}{-{$color}}"); $indentCount = $this->maxLineLength - $scenarioPathLn; $this->printPathComment($node->getFile(), $node->getLine(), $indentCount); $this->writeln(); }
/** * Attempts to match provided definition against a step text. * * @param Definition $definition * @param string $stepText * @param ArgumentInterface[] $multiline * * @return null|SearchResult */ private function match(Definition $definition, $stepText, array $multiline) { $regex = $this->patternTransformer->transformPatternToRegex($definition->getPattern()); if (!preg_match($regex, $stepText, $match)) { return null; } $function = $definition->getReflection(); $match = array_merge($match, array_values($multiline)); $arguments = $this->argumentOrganiser->organiseArguments($function, $match); return new SearchResult($definition, $stepText, $arguments); }
/** * {@inheritdoc} */ public function __toString() { return $this->definition->__toString(); }
/** * Initializes redundant exception. * * @param Definition $step2 duplicate step definition * @param Definition $step1 firstly matched step definition */ public function __construct(Definition $step2, Definition $step1) { $message = sprintf("Step \"%s\" is already defined in %s\n\n%s\n%s", $step2->getPattern(), $step1->getPath(), $step1->getPath(), $step2->getPath()); parent::__construct($message); }
/** * {@inheritdoc} */ protected function colorizeDefinitionArguments($text, Definition $definition, $color) { $regex = $definition->getRegex(); $paramColor = $color . '_param'; // Find arguments with offsets $matches = array(); preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE); array_shift($matches); // Replace arguments with colorized ones $shift = 0; foreach ($matches as $key => $match) { if (!is_numeric($key) || -1 === $match[1]) { continue; } $offset = $match[1] + $shift; $value = $match[0]; $begin = substr($text, 0, $offset); $end = substr($text, $offset + strlen($value)); // Keep track of how many extra characters are added $shift += strlen($format = "{+strong class=\"{$paramColor}\"-}%s{+/strong-}") - 2; $text = sprintf('%s' . $format . '%s', $begin, $value, $end); } // Replace "<", ">" with colorized ones $text = preg_replace('/(<[^>]+>)/', "<strong class=\"{$paramColor}\">\$1</strong>", $text); $text = strtr($text, array('{+' => '<', '-}' => '>')); return $text; }