Exemplo n.º 1
0
 /**
  * Defines a step with ->Given|When|Then|...('/regex/', callback) or
  * call a step with ->Given|When|Then|...('I enter "12" in the field', $world) or
  * even with arguments ->Given|When|Then|...('I fill up fields', $world, $table).
  *
  * @param   string  $type       step type (Given|When|Then|...)
  * @param   string  $arguments  step regex & callback
  *
  * @throws  Behat\Behat\Exception\RedundantException     if step definition is already exists
  */
 public function __call($type, $arguments)
 {
     if (2 == count($arguments) && is_callable($arguments[1])) {
         switch (strtolower($type)) {
             case 'when':
                 $definition = new When($arguments[1], $arguments[0]);
                 break;
             case 'then':
                 $definition = new Then($arguments[1], $arguments[0]);
                 break;
             case 'given':
             default:
                 $definition = new Given($arguments[1], $arguments[0]);
                 break;
         }
         $this->dispatcher->addDefinition($definition);
     } else {
         $text = array_shift($arguments);
         $world = array_shift($arguments);
         $step = new StepNode($type, $text);
         $step->setArguments($arguments);
         $definition = $this->dispatcher->findDefinition($world, $step);
         $definition->run($world);
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * @see     Behat\Behat\Context\Loader\ContextLoaderInterface::load()
  */
 public function load(ContextInterface $context)
 {
     $reflection = new \ReflectionObject($context);
     foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodRefl) {
         foreach ($this->readMethodAnnotations($reflection->getName(), $methodRefl) as $annotation) {
             if ($annotation instanceof DefinitionInterface) {
                 $this->definitionDispatcher->addDefinition($annotation);
             } elseif ($annotation instanceof TransformationInterface) {
                 $this->definitionDispatcher->addTransformation($annotation);
             } elseif ($annotation instanceof HookInterface) {
                 $this->hookDispatcher->addHook($annotation);
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Defines a step with ->Given|When|Then|...('/regex/', callback) or
  * call a step with ->Given|When|Then|...('I enter "12" in the field', $world) or
  * even with arguments ->Given|When|Then|...('I fill up fields', $world, $table).
  *
  * @param   string  $type       step type (Given|When|Then|...)
  * @param   string  $arguments  step regex & callback
  *
  * @throws  Behat\Behat\Exception\Redundant     if step definition is already exists
  */
 public function __call($type, $arguments)
 {
     if (2 == count($arguments) && is_callable($arguments[1])) {
         $debug = debug_backtrace();
         $debug = $debug[1];
         $this->objects[] = new Definition($type, $arguments[0], $arguments[1], $debug['file'], $debug['line']);
     } else {
         $text = array_shift($arguments);
         $world = array_shift($arguments);
         $step = new StepNode($type, $text);
         $step->setArguments($arguments);
         $definition = $this->dispatcher->findDefinition($step);
         $definition->run($world);
     }
     return $this;
 }
 public function initialize()
 {
     if ($this->initialized) {
         return true;
     }
     $keywords = new \Behat\Gherkin\Keywords\ArrayKeywords(array('en' => array('feature' => 'Feature', 'background' => 'Background', 'scenario' => 'Scenario', 'scenario_outline' => 'Scenario Outline|Scenario Template', 'examples' => 'Examples|Scenarios', 'given' => 'Given', 'when' => 'When', 'then' => 'Then', 'and' => 'And', 'but' => 'But')));
     $keywords->setLanguage('en');
     $lexer = new \Behat\Gherkin\Lexer($keywords);
     $parser = new Parser($lexer);
     foreach ($this->getParameter('templates') as $template) {
         foreach (glob($template) as $file) {
             /** @var $feature \Behat\Gherkin\Node\FeatureNode */
             $feature = $parser->parse(file_get_contents($file));
             /** @var $scenario \Behat\Gherkin\Node\ScenarioNode */
             foreach ($feature->getScenarios() as $scenario) {
                 $this->definitionDispatcher->addDefinition(new \Weavora\MinkExtra\Definition\LazyDefinition($scenario));
             }
         }
     }
     $this->initialized = true;
     return true;
 }
Exemplo n.º 5
0
    /**
     * Returns available definitions in string.
     *
     * @param   string  $search         search string
     * @param   string  $language       default definitions language
     * @param   Boolean $shortNotation  show short notation instead of full one
     *
     * @return  string
     */
    private function getDefinitionsForPrint($search = null, $language = 'en', $shortNotation = true)
    {
        if ($shortNotation) {
            $template = '<info>{type}</info> <comment>{regex}</comment>';
        } else {
            $template = <<<TPL
<info>{type}</info> <comment>{regex}</comment>
    {description}<path># {path}</path>

TPL;
        }
        $definitions = array();
        foreach ($this->dispatcher->getDefinitions() as $regex => $definition) {
            $regex = $this->dispatcher->translateDefinitionRegex($regex, $language);
            if ($search && !preg_match('/' . str_replace(' ', '.*', preg_quote($search, '/') . '/'), $regex)) {
                continue;
            }
            $regex = preg_replace_callback('/\\([^\\)]*\\)/', function ($capture) {
                return "</comment><capture>{$capture[0]}</capture><comment>";
            }, $regex);
            $definitions[] = strtr($template, array('{regex}' => $regex, '{type}' => str_pad($definition->getType(), 5, ' ', STR_PAD_LEFT), '{description}' => $definition->getDescription() ? '- ' . $definition->getDescription() . "\n    " : '', '{path}' => $definition->getPath()));
        }
        return implode("\n", $definitions);
    }
Exemplo n.º 6
0
 public function load(ContextInterface $context)
 {
     foreach ($this->sitemap->findClasses() as $className) {
         $reflection = new \ReflectionClass($className);
         if ($reflection->isSubclassOf('Behat\\Behat\\Context\\BehatContext')) {
             continue;
         }
         foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodRefl) {
             if ($methodRefl->getDeclaringClass() != $reflection) {
                 continue;
             }
             // only own methods; intentionally != because reflection is not identical
             foreach ($this->readMethodAnnotations($reflection->getName(), $methodRefl) as $annotation) {
                 if ($annotation instanceof \Behat\Behat\Definition\DefinitionInterface) {
                     $this->definitionDispatcher->addDefinition($annotation);
                 } elseif ($annotation instanceof \Behat\Behat\Definition\TransformationInterface) {
                     $this->definitionDispatcher->addTransformation($annotation);
                 } elseif ($annotation instanceof \Behat\Behat\Hook\HookInterface) {
                     $this->hookDispatcher->addHook($annotation);
                 }
             }
         }
     }
 }