drupalextension
drupalextension copied to clipboard
Establish best practice for putting Context classes in modules
This is related to #518.
It would be good to establish some best practices for shipping Context classes with modules. Currently most modules use SubContexts but these come with some problems: they are placed in the root of the module folder, use a specific naming scheme, and use the global namespace (ref. #518).
In case a module wants to ship one or more Context classes:
- Where shall we place the Context classes? It would probably be best if they are located somewhere in the
./testsfolder. Perhaps in./tests/src/Behat/Context? We could then also suggest to put the scenarios that ship with the module in./tests/src/Behat/features. Or would it be better to use./tests/src/Contextfor the classes and./tests/featuresfor the scenarios? - How shall we namespace them? Follow the core example and use
Drupal\Tests\modulename\Behat\Contextor follow the Behat example and useDrupal\modulename\Context? - How do we publish the namespaces? Do we suggest that every module defines them manually in their
composer.jsonfile? Or do we automate it similar to how core defines its test namespaces indrupal_phpunit_populate_class_loader()?
- Where shall we place the Context classes? It would probably be best if they are located somewhere in the
./testsfolder. Perhaps in./tests/src/Behat/Context? We could then also suggest to put the scenarios that ship with the module in./tests/src/Behat/features. Or would it be better to use./tests/src/Contextfor the classes and./tests/featuresfor the scenarios?
- I'd go for
./tests/src/Behat/Contextfor the contexts, Behat seems like the semantically more appropriate prefix (Context is too broad)
- How shall we namespace them? Follow the core example and use
Drupal\Tests\modulename\Behat\Contextor follow the Behat example and useDrupal\modulename\Context?
- Either is fine, everything but global namespace is better;
- I'm inclined to go for the first;
- How do we publish the namespaces? Do we suggest that every module defines them manually in their
composer.jsonfile? Or do we automate it similar to how core defines its test namespaces indrupal_phpunit_populate_class_loader()?
- at first sight the composer way seems like the best BUT
- automating it seems like the easiest way (less effort)
- less configuration (in composer.json) seems like less legacy in time?
- Would there be a difference in performance? If not perhaps we should do with the class loader, if it does I'd go for the most performant way.
I wouldn't expect there to be much or any performance impact in automating the population of the class namespaces. This happens at the end of a composer install or composer update, and then the classmaps are written to disk, so they are virtually zero cost at access time.