Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behat context in a trait

The basic Behat use case recommends using FeatureContext class. Also, you can specify any other PHP classes in the features/bootstrap directory and they are loaded, but in the alphabetical order, no matter what the dependencies are.

Given there is a trait and a FeatureContext class:

features/bootstrap/FeatureContext.php
features/bootstrap/MyLovelyTrait.php

What is the best way to load it properly? Obviously, MyLovelyTrait is used within the FeatureContext:

class FeatureContext extends BehatContext {
    use MyLovelyTrait;
}

And that fails because M > F, in the alphabet.

I will be happy to use composer autoloading, but I don't want to require_once the autoload.php file in the top of BehatContext.php file. Is there a way to specify this in behat.yml configuration? Also, any other best practice answer regarding class-loading of Behat context files will be appreciated.

like image 438
Pavel S. Avatar asked Mar 05 '14 13:03

Pavel S.


2 Answers

I'm not 100% sure this is answering your question but I am under the impression that you are trying to use multiple context files? If so you don't need the use statement instead within the FeatureContext.php construct method we use the line:

$this -> useContext('Subcontext', new Subcontext($parameters));

In this case the other context you want to use is called "Subcontext".

like image 154
MikeCon94 Avatar answered Oct 28 '22 15:10

MikeCon94


A good reason not to useContext('Subcontext') can be found in the Changelog of the upcoming version 3 of Behat:

3.0.0beta1 / 2013-08-13
...
  * Subcontexts removed in favor of context pools
like image 42
jugglefish Avatar answered Oct 28 '22 16:10

jugglefish