drupalextension icon indicating copy to clipboard operation
drupalextension copied to clipboard

Establish best practice for putting Context classes in modules

Open pfrenssen opened this issue 6 years ago • 2 comments

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:

  1. Where shall we place the Context classes? It would probably be best if they are located somewhere in the ./tests folder. 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/Context for the classes and ./tests/features for the scenarios?
  2. How shall we namespace them? Follow the core example and use Drupal\Tests\modulename\Behat\Context or follow the Behat example and use Drupal\modulename\Context?
  3. How do we publish the namespaces? Do we suggest that every module defines them manually in their composer.json file? Or do we automate it similar to how core defines its test namespaces in drupal_phpunit_populate_class_loader()?

pfrenssen avatar Jun 24 '19 07:06 pfrenssen

  1. Where shall we place the Context classes? It would probably be best if they are located somewhere in the ./tests folder. 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/Context for the classes and ./tests/features for the scenarios?
  • I'd go for ./tests/src/Behat/Context for the contexts, Behat seems like the semantically more appropriate prefix (Context is too broad)
  1. How shall we namespace them? Follow the core example and use Drupal\Tests\modulename\Behat\Context or follow the Behat example and use Drupal\modulename\Context?
  • Either is fine, everything but global namespace is better;
  • I'm inclined to go for the first;
  1. How do we publish the namespaces? Do we suggest that every module defines them manually in their composer.json file? Or do we automate it similar to how core defines its test namespaces in drupal_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.

MPParsley avatar Feb 23 '20 08:02 MPParsley

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.

pfrenssen avatar Feb 24 '20 16:02 pfrenssen