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 b3051d3
Showing 1 changed file with 165 additions and 81 deletions.
246 changes: 165 additions & 81 deletions user_guide/context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,32 @@ suite.
In order to customise the list of contexts your test suite requires, you need
to fine-tune the suite configuration inside ``behat.yml``:

.. 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 Down Expand Up @@ -180,15 +195,28 @@ 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 b3051d3

Please sign in to comment.