Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Config - Context #182

Open
wants to merge 1 commit into
base: v3.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 169 additions & 85 deletions user_guide/context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,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 @@ -139,19 +139,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;
Comment on lines +149 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the contexts in all these examples in the root namespace, because "out of the box" just following the instructions on this page, these won't be autoloadable by behat (unless you put them in features/bootstrap/App/Tests/Behat/Context/FeatureContext).

Of course I realise that the vast majority of people these days are familiar with how to configure autoloading themselves e.g. through composer, but while we're providing / documenting Behat's custom autoloader I think the docs examples should be consistent with how that behaves.

Suggested change
use App\Tests\Behat\Context\FeatureContext;
use App\Tests\Behat\Context\SecondContext;
use App\Tests\Behat\Context\ThirdContext;
use FeatureContext;
use SecondContext;
use 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 @@ -178,21 +193,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use 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 @@ -205,23 +233,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;
Comment on lines +241 to +242
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use App\Tests\Behat\Context\MyWickedContext;
use MyAwesomeContext;
use 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 @@ -263,62 +312,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``:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
same ``contexts`` setting inside your ``behat.yml``:
same ``contexts`` setting inside your ``behat.php``:


.. 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially in this example, as the example context class itself is not namespaced and says it is in features/bootstrap/MyAwesomeContext.php:

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use 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 @@ -333,16 +405,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use App\Tests\Behat\Context\MyAwesomeContext;
use 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
Loading