Hello world :)
I am struggling since a few days to migrate my application to Behat 3.
I would like to use the new "several contexts by suite" feature to have a more flexible and reusable code. I would also like to organize my context classes into several folders for more readability as I will have many of them.
Below is an example of behat.yml and folders structures I would like to do :
# behat.yml
default:
autoload:
- %paths.base%/bootstrap
suites:
aWebSuite-dev:
paths:
- %paths.base%/features/someWebFeatures
contexts:
- aWebFeatureContext
- EnvironmentContext:
- Dev
- WebContext
- UserContext
aMobileSuite-dev:
paths:
- %paths.base%/features/someMobileFeatures
contexts:
- aMobileFeatureContext
- EnvironmentContext:
- Dev
- MobileContext
- UserContext
And the folders structure:
behat/
|- bin/
|- bootstrap/
|- features/
|- aMobileFeatureContext.php
|- anotherMobileFeatureContext.php
|- aWebFeatureContext.php
|- interfaces/
|- WebContext.php
|- MobileContext.php
|- environments/
|- EnvironmentContext.php
|- Modules/
|- UserContext.php
|- anotherModuleContext.php
|- features/
|- someWebFeatures/
|- feature1.feature
|- feature2.feature
|- someMobileFeatures/
|- feature1.feature
|- feature2.feature
Such folders structure in bootstrap/ does not work because Behat is not able to find and load the context classes. I think I understood that it was because Behat 3 use PSR-0 autoloader to load the context classes from bootstrap/ and the structure above does not comply to PSR-0.
So for now, I am using the following structure :
behat/
|- bin/
|- bootstrap/
|- aMobileFeatureContext.php
|- anotherMobileFeatureContext.php
|- aWebFeatureContext.php
|- WebContext.php
|- MobileContext.php
|- EnvironmentContext.php
|- UserContext.php
|- anotherModuleContext.php
But every context files are mixing in the same folder. Is there still a way to achieve my first idea ?
Thanks a lot !
If your classes are autoloadable by your composer autoloader, they can be sued by Behat too, even when they don't follow PSR-0
Hi stof !
Thanks for your help ! I think I am missing something with PSR and autoloader...
I tried with the following configuration :
default:
autoload:
- %paths.base%/bootstrap/
suites:
test:
paths:
- %paths.base%/features/web
contexts:
- WebContext
- FeatureContext
behat/
|- bin/
|- bootstrap/
|- Features/
|- FeatureContext.php
|- Interfaces/
|- WebContext.php
|- features/
|- web/
|- feature1.feature
|- feature2.feature
[...]
"autoload": {
"psr-4": {
"myCompany\\": "bootstrap/"
}
},
My class WebContext is in namespace myCompany\Interfaces and implements Behat\Behat\Context\Context. Same for FeatureContext (but it is in namespace myCompany\Features ).
I do run composer.phar update but I still get the following error :
[Behat\Behat\Context\Exception\ContextNotFoundException]
`WebContext` context class not found and can not be used.
What I am doing wrong ? :(
this is because the name of your class is not WebContext but myCompany\Interfaces\WebContext as you put it in a namespace. So you need to fix the config referencing it
Oh, right. It works now !
Sorry for the newbie questions.
Below is the behat.yml working with the autoload and structure in my previous post, for people who may have the same problem :
default:
autoload:
- %paths.base%/bootstrap/
suites:
test:
paths:
- %paths.base%/features/web
contexts:
- myCompany\Interfaces\WebContext
- myCompany\Features\FeatureContext
I still have other questions about Behat 3 but I think I should create other topics instead.
I close this issue, thanks a lot for helping stof !
Remember that you need to run php composer.phar dump-autoload if you modify your composer.json.
Most helpful comment
Oh, right. It works now !
Sorry for the newbie questions.
Below is the
behat.ymlworking with the autoload and structure in my previous post, for people who may have the same problem :I still have other questions about Behat 3 but I think I should create other topics instead.
I close this issue, thanks a lot for helping stof !