I am writing this in order to document the many hours spend trying to find a solution for a very simple problem. The Php version used is 5.3 or 5.4 (depends on the project to be used on):
“How to have multiple contexts in Behat and maintain the OOP structure, parameters, support an expanding number of context files for a scalable application and most of all, have code hinting available in all the contexts.”
The client I am working with at the moment has multiple applications all extending one internal framework. The problem is that everyone wants automation for the main framework but nobody has time for it because the clients do not pay for that. A typical not out of the ordinary situation. A solution was to get what we have on the projects and create a repository with tests for the main framework based on an estimation of effort, complexity and deviation from the standard implementation. In theory it will worked brilliantly.
As in every good story we have set up a Git repo, we did establish the flow, added in the code reviews, coding standards and we are ready to go. After a week features, tests and helpers are flowing from all the directions. The joy!
The structure of our project would be a helperContext that together with MinkContext and any other context must be accessible into any other class which will be added later on. We must also be able to pass variables from one context to another at will.
Let’s set a baseline. Php can extend 1 class and only 1 and inherit any number of interfaces.
How Behat works:
There is a file called Application.php, inside it we get to ClassLoader.php which gets from composer a list of all the classes of the modules that have been installed in the project. From here on, each file is included (it is include not include_once).
1) Let’s to pull this out! In almost every Behat presentation there is this phrase stating that there is a function called setContext() which will be the holly grail. Youp, there is, here’s the documentation http://docs.behat.org/guides/4.context.html . Works as intended BUT the MinkContext can’t be extended anymore since BehatContext has to be in order to accommodate subcontexts. The solution is to include MinkContext using the useContext() functionality. This will let us use it’s classes with the syntax
. BUT we will have no code hinting anywhere.
2) How about creating a class that extends Mink and make FeatureContext extend that class. It should come packed with Mink and all our added functionalities. Youp it does. It even has auto-complete, self high-five BUT if we want to create another context called LoginContext, where we want to store Login related only functionality, we can’t extend FeatureContext. Because FeatureContext is the framework’s entry point and already exists at that point. We will get “Multiple definitions for FeatureContext exist” and it will crash after 100 self inclusions. Damn, so close! We can taste the victory!
3) Let’s have FeatureContext extend MinkContext and implement ContextInterface. This will make FeatureContext awesoommee, it will contain everything and also after we copy paste the implementation of ContextInterface from BehatContext we maintain the inheritance from the documentation. When we utilize useContext() on LoginContext we get all it’s insides at runtime. Here we have scalability. BUT no code-hinting anywhere else except in FeatureContext, also we have to manually add every new context file added to the project into the constructor. Oh god damn so close yet so far.
4) Maybe dropping the OOP and having the helperContext a simple file with methods. This should work BUT we are loosing everything and it must be included by hand all time if every other context. Will not go down this path on how many problems there are.
For now I am out of ideas. Probably I need something in the constructor of FeatureContext that enables the usage of useContext() but removes some code that allows the helperContext to have code hinting.