@@ -16,6 +16,9 @@ Setting Up the Context
1616First, we create a ``Context `` class that will throw a ``PendingException `` with a configurable text.
1717The configuration will also control whether the behaviour is enabled or not.
1818
19+ The example directory structure and file locations in this cookbook assume you have configured
20+ composer to autoload the ``HelloWorld `` namespace from the ``src `` directory.
21+
1922.. code-block ::
2023
2124 src/
@@ -212,14 +215,78 @@ through the ``HelloWorldExtension``, ensuring it gets loaded:
212215 // ...
213216 }
214217
218+ Finally, we can define a custom configuration object for use in a project's ``behat.php ``.
219+ This step is optional - users can also register your extension with the generic
220+ ``Behat\Config\Extension `` class. However, providing a custom class will make it easier
221+ for users to discover and configure the correct settings for your extension.
222+
223+ .. code-block :: php
224+
225+ <?php
226+
227+ namespace HelloWorld\Config;
228+
229+ use Behat\Config\ExtensionConfigInterface;
230+ use HelloWorld\ServiceContainer\HelloWorldExtension;
231+
232+ final class HelloWorldExtensionConfig implements ExtensionConfigInterface
233+ {
234+
235+ // You don't need to set defaults here, they are defined along with the config structure in
236+ // HelloWorld\ServiceContainer\HelloWorldExtension::configure(). It is important that this
237+ // object only returns settings that the user has explicitly configured.
238+ private array $settings = [];
239+
240+ // The following methods must be provided for Behat to read the configuration object
241+
242+ public function name(): string
243+ {
244+ // This should return the fully qualified name of your entrypoint class
245+ return HelloWorldExtension::class;
246+ }
247+
248+ public function toArray(): array
249+ {
250+ return $this->settings;
251+ }
252+
253+ // The following methods provide the mechanism for users to customise your
254+ // extension's configuration.
255+ // We recommend using setters rather than constructor properties, for
256+ // consistency with Behat's own config objects.
257+
258+ public function enable(): self
259+ {
260+ $this->settings['enable'] = true;
261+
262+ return $this;
263+ }
264+
265+ public function disable(): self
266+ {
267+ $this->settings['enable'] = false;
268+
269+ return $this;
270+ }
271+
272+ public function withText(string $text): self
273+ {
274+ $this->settings['text'] = $text;
275+
276+ return $this;
277+ }
278+ }
279+
280+
215281 Using the extension
216282-------------------
217283
218284Now that the extension is ready and will inject values into context, we just
219285need to configure it into a project.
220286
221- In the ``extensions `` key of a profile (``default `` in our case), we'll add
222- the ``HelloWorldExtension `` key and configure our ``text `` and ``enable `` value.
287+ We'll set up an instance of our custom ``HelloWorldExtensionConfig `` configuration
288+ object, and use the ``withExtension() `` method of a profile (``default `` in our
289+ case) to register it.
223290
224291Finally, we need to load the ``HelloWorld\Context\HelloWorldContext `` into our suite.
225292
@@ -235,14 +302,14 @@ Here's the ``behat.php``:
235302 use Behat\Config\Suite;
236303 use FeatureContext;
237304 use HelloWorld\Context\HelloWorldContext;
238- use HelloWorld\ServiceContainer\HelloWorldExtension ;
305+ use HelloWorld\Config\HelloWorldExtensionConfig ;
239306
240307 return new Config()
241308 ->withProfile(new Profile('default')
242- ->withExtension(new Extension(HelloWorldExtension::class, [
243- 'text' => 'Hi there!',
244- 'enable' => true,
245- ]) )
309+ ->withExtension(new HelloWorldExtensionConfig()
310+ ->withText( 'Hi there!')
311+ ->enable()
312+ )
246313 ->withSuite(new Suite('default')
247314 ->withContexts(
248315 FeatureContext::class,
0 commit comments