Skip to content

Commit

Permalink
PHP Config - Context
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Dec 9, 2024
1 parent 6d8c670 commit 7224c2b
Showing 1 changed file with 169 additions and 85 deletions.
254 changes: 169 additions & 85 deletions user_guide/context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ In order to be used by Behat, your context class should follow these rules:
#. The context class should be called ``FeatureContext``. It's a simple convention
inside the Behat infrastructure. ``FeatureContext`` is the name of the
context class for the default suite. This can easily be changed through
suite configuration inside ``behat.yml``.
suite configuration inside ``behat.php``.

#. The context class should be discoverable and loadable by Behat. That means you
should somehow tell Behat about your class file. Behat comes with a PSR-0
autoloader out of the box and the default autoloading directory is
``features/bootstrap``. That's why the default ``FeatureContext`` is loaded so
easily by Behat. You can place your own classes under ``features/bootstrap``
by following the PSR-0 convention or you can even define your own custom
autoloading folder via ``behat.yml``.
autoloading folder via ``behat.php``.

.. note::

Expand Down Expand Up @@ -135,19 +135,34 @@ your code more structured by allowing you to use multiple contexts in a single t
suite.

In order to customise the list of contexts your test suite requires, you need
to fine-tune the suite configuration inside ``behat.yml``:
to fine-tune the suite configuration inside ``behat.php``:

.. code-block:: yaml
# behat.yml
.. code-block:: php
default:
suites:
default:
contexts:
- FeatureContext
- SecondContext
- ThirdContext
<?php
// behat.php
use App\Tests\Behat\Context\FeatureContext;
use App\Tests\Behat\Context\SecondContext;
use App\Tests\Behat\Context\ThirdContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
$profile = new Profile('default')
->withSuite(
new Suite('default')
->withContexts(
FeatureContext::class,
SecondContext::class,
ThirdContext::class,
)
)
;
return new Config()
->withProfile($profile)
;
The first ``default`` in this configuration is a name of the profile. We
will discuss profiles a little bit later. Under
Expand All @@ -174,21 +189,34 @@ Behat sees a scenario in your test suite, it will:
``Behat\Behat\Context\Context`` interface and be autoloadable by
Behat.

Basically, all contexts under the ``contexts`` section of your ``behat.yml``
Basically, all contexts under the ``contexts`` section of your ``behat.php``
are the same for Behat. It will find and use the methods in them the same way
it does in the default ``FeatureContext``. And if you're happy with a single
context class, but you don't like the name ``FeatureContext``, here's
how you change it:

.. code-block:: yaml
.. code-block:: php
<?php
// behat.php
use App\Tests\Behat\Context\MyAwesomeContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
# behat.yml
$profile = new Profile('default')
->withSuite(
new Suite('default')
->withContexts(
MyAwesomeContext::class,
)
)
;
default:
suites:
default:
contexts:
- MyAwesomeContext
return new Config()
->withProfile($profile)
;
This configuration will tell Behat to look for ``MyAwesomeContext``
instead of the default ``FeatureContext``.
Expand All @@ -201,23 +229,44 @@ instead of the default ``FeatureContext``.
same context, you will have to define that specific context for every
specific suite:

.. code-block:: yaml
# behat.yml
default:
suites:
default:
contexts:
- MyAwesomeContext
- MyWickedContext
suite_a:
contexts:
- MyAwesomeContext
- MyWickedContext
suite_b:
contexts:
- MyAwesomeContext
.. code-block:: php
<?php
// behat.php
use App\Tests\Behat\Context\MyAwesomeContext;
use App\Tests\Behat\Context\MyWickedContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
$profile = new Profile('default')
->withSuite(
new Suite('default')
->withContexts(
MyAwesomeContext::class,
MyWickedContext::class,
)
)
->withSuite(
new Suite('suite_a')
->withContexts(
MyAwesomeContext::class,
MyWickedContext::class,
)
)
->withSuite(
new Suite('suite_b')
->withContexts(
MyAwesomeContext::class,
MyWickedContext::class,
)
)
;
return new Config()
->withProfile($profile)
;
This configuration will tell Behat to look for ``MyAwesomeContext`` and
``MyWickedContext`` when testing ``suite_a`` and ``MyAwesomeContext`` when
Expand Down Expand Up @@ -259,62 +308,85 @@ As a matter of fact, Behat gives you ability to do just that. You can
specify arguments required to instantiate your context classes through
same ``contexts`` setting inside your ``behat.yml``:

.. code-block:: yaml
# behat.yml
default:
suites:
default:
contexts:
- MyAwesomeContext:
- http://localhost:8080
- /var/tmp
.. note::
.. code-block:: php
Note an indentation for parameters. It is significant:
<?php
// behat.php
.. code-block:: yaml
use App\Tests\Behat\Context\MyAwesomeContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
contexts:
- MyAwesomeContext:
- http://localhost:8080
- /var/tmp
$profile = new Profile('default')
->withSuite(
new Suite('default')
->addContext(MyAwesomeContext::class, [
'http://localhost:8080',
'/var/tmp',
])
)
;
Aligned four spaces from the context class itself.
return new Config()
->withProfile($profile)
;
Arguments would be passed to the ``MyAwesomeContext`` constructor in
the order they were specified here. If you are not happy with the idea
of keeping an order of arguments in your head, you can use argument
names instead:

.. code-block:: yaml
.. code-block:: php
<?php
// behat.php
use App\Tests\Behat\Context\MyAwesomeContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
# behat.yml
$profile = new Profile('default')
->withSuite(
new Suite('default')
->addContext(MyAwesomeContext::class, [
'baseUrl' => 'http://localhost:8080',
'tempPath' => '/var/tmp',
])
)
;
default:
suites:
default:
contexts:
- MyAwesomeContext:
baseUrl: http://localhost:8080
tempPath: /var/tmp
return new Config()
->withProfile($profile)
;
As a matter of fact, if you do, the order in which you specify these
arguments becomes irrelevant:

.. code-block:: yaml
.. code-block:: php
<?php
// behat.php
use App\Tests\Behat\Context\MyAwesomeContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
# behat.yml
$profile = new Profile('default')
->withSuite(
new Suite('default')
->addContext(MyAwesomeContext::class, [
'tempPath' => '/var/tmp',
'baseUrl' => 'http://localhost:8080',
])
)
;
default:
suites:
default:
contexts:
- MyAwesomeContext:
tempPath: /var/tmp
baseUrl: http://localhost:8080
return new Config()
->withProfile($profile)
;
Taking this a step further, if your context constructor arguments are
optional:
Expand All @@ -329,16 +401,28 @@ optional:
You then can specify only the parameter that you actually need to change:

.. code-block:: yaml
# behat.yml
.. code-block:: php
default:
suites:
default:
contexts:
- MyAwesomeContext:
tempPath: /var/tmp
<?php
// behat.php
use App\Tests\Behat\Context\MyAwesomeContext;
use Behat\Config\Config;
use Behat\Config\Profile;
use Behat\Config\Suite;
$profile = new Profile('default')
->withSuite(
new Suite('default')
->addContext(MyAwesomeContext::class, [
'tempPath' => '/var/tmp',
])
)
;
return new Config()
->withProfile($profile)
;
In this case, the default values would be used for other parameters.

Expand Down

0 comments on commit 7224c2b

Please sign in to comment.