docs(cookbooks): adding creating a behat extension#146
Conversation
acoulton
left a comment
There was a problem hiding this comment.
This is great, thanks @monitaurus - I have a couple of suggestions that I think might help, let me know what you think.
I'd have found this guide extremely useful when I started trying to figure out extensions!
| To ensure Behat can find and load the ``LogsExtension.php`` file, it is important to place it within the `ServiceContainer` folder. | ||
| While there might be alternatives, we will stick to the straightforward method. |
There was a problem hiding this comment.
I'm not certain this is correct - I think it can be called & located anywhere that your composer autoloader can find it?
There was a problem hiding this comment.
This is the part where I was stuck, not understanding why my extension wouldn't load (kudos @TimoBakx for helping me on Symfony's Slack 🙏).
From my research, extensions seems to be loaded via the ExtensionManager. $locator is the key value used to define the extension configuration in the behat.yml file.
There four ways for the manager to load the extension:
if (class_exists($class = $locator)) {
return new $class;
}
if (class_exists($class = $this->getFullExtensionClass($locator))) {
return new $class;
}
if (file_exists($locator)) {
return require($locator);
}
if (file_exists($path = $this->extensionsPath . DIRECTORY_SEPARATOR . $locator)) {
return require($path);
}But I didn't know why, it was not working on putting the full namespace of the class wasn't enough in my tests, I had to put it in the ServiceContainer folder.
So within the post I choose to go the easy way, but we can change it.
I can try to understand why it wasn't working at the first place.
There was a problem hiding this comment.
@monitaurus using the fully-qualified class for the extension does work and is applied before any of the subsequent magic in that method. If that wasn't working for you my best guess would be your class was in a file that composer couldn't autoload (so the class_exists($locator) at the top of that quoted code block would have returned false).
There was a problem hiding this comment.
I tried again, with the new HelloWorld extension, but it didn't work.
I'll try some step debug to find out, but I just let the full class namespace in the cookbook for simplicity.
… creation Co-authored-by: Andrew Coulton <andrew@ingenerator.com>
Co-authored-by: Andrew Coulton <andrew@ingenerator.com>
Co-authored-by: Andrew Coulton <andrew@ingenerator.com>
452ccbc to
f91f7c9
Compare
acoulton
left a comment
There was a problem hiding this comment.
@monitaurus thanks so much for taking the time to rewrite this with a different example. I just have a few small comments on naming / wording but otherwise it looks great and I think people will find it very useful.
950af30 to
2386979
Compare
… extension * apply suggestions * remove some last code block Co-authored-by: Andrew Coulton <andrew@ingenerator.com>
2386979 to
08da28e
Compare
acoulton
left a comment
There was a problem hiding this comment.
@monitaurus thanks for the updates, LGTM 👍
carlos-granados
left a comment
There was a problem hiding this comment.
Looks really good and helpful
I recently wrote a blog post explaining how to create a Behat extension: How to Create a Behat Extension.
As there's not so much information about extensions in the official Behat's documentation, I submit this PR to add a cookbook, which is a rewrite of the post, in the Behat docs tone and removing extra stuff.
It's a cookbook and not a documentation, as it's doesn't explain the whole extension system, but give information on creating a simple extension.