Skip to content
This repository has been archived by the owner on Nov 30, 2017. It is now read-only.

Commit

Permalink
Merge pull request #414 from pamil/behat-guide
Browse files Browse the repository at this point in the history
Initial documentation for Behat with service support
  • Loading branch information
Paweł Jędrzejewski committed Feb 14, 2016
2 parents 21c71e1 + 914e2af commit f8f7758
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
27 changes: 27 additions & 0 deletions behat/how-to-add-new-context.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
How to add a new context?
=========================

To add a new context it is needed to add an service in Behat container in ``etc/behat/services/contexts.xml`` file:

.. code-block:: xml
<service id="sylius.behat.context.CONTEXT_CATEGORY.CONTEXT_NAME" class="%sylius.behat.context.CONTEXT_CATEGORY.CONTEXT_NAME.class%" scope="scenario">
<tag name="sylius.behat.context" />
</service>
Then you can use it in your suite configuration:

.. code-block:: yaml
default:
suites:
SUITE_NAME:
contexts_as_services:
- "sylius.behat.context.CONTEXT_CATEGORY.CONTEXT_NAME"
filters:
tags: "@SUITE_TAG"
.. note::

The context categories are usually one of ``hook``, ``setup``, ``ui`` and ``domain``.
43 changes: 43 additions & 0 deletions behat/how-to-add-new-page.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
How to add a new page object?
=============================

Sylius uses a solution inspired by ``SensioLabs/PageObjectExtension``, which provides an infrastructure to create
pages that encapsulates all the user interface manipulation in page objects.

To create a new page object it is needed to add an service in Behat container in ``etc/behat/services/pages.xml`` file:

.. code-block:: xml
<service id="sylius.behat.page.PAGE_NAME" class="%sylius.behat.page.PAGE_NAME.class%" parent="sylius.behat.symfony_page" scope="scenario" public="false" />
.. note::

There are some boilerplates for common pages, which you may use. The available parents are ``sylius.behat.page`` (``Sylius\Behat\Page\Page``)
and ``sylius.behat.symfony_page`` (``Sylius\Behat\Page\SymfonyPage``). It is not required for a page to extend any class as
pages are POPOs (Plain Old PHP Objects).

Then you will need to add that service as a regular argument in context service.

The simplest Symfony-based page looks like:

.. code-block:: php
use Sylius\Behat\Page\SymfonyPage;
class LoginPage extends SymfonyPage
{
public function logIn($email, $password)
{
$document = $this->getDocument();
$document->fillField('Email', $email);
$document->fillField('Password', $password);
$document->pressButton('Login');
}
protected function getRouteName()
{
return 'sylius_user_security_login';
}
}
5 changes: 5 additions & 0 deletions behat/how-to-define-new-suite.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
How to define a new suite?
==========================

To define a new suite it is needed to create a suite configuration file in ``etc/behat/suites``
directory based on existing files in there and then register that file in ``etc/behat/suites.yml``.
12 changes: 12 additions & 0 deletions behat/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Behat guide
===========

.. toctree::
:hidden:

how-to-add-new-context
how-to-add-new-page
how-to-define-new-suite
introduction-to-containers-and-scopes

.. include:: /behat/map.rst.inc
34 changes: 34 additions & 0 deletions behat/introduction-to-containers-and-scopes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Introduction to containers and scopes
=====================================

In order to provide support for defining contexts and pages in Behat container with dependencies from Symfony application
container, our service definitions may contain some extra features.

There are 3 available containers:

- ``behat`` (the default one) - the container which holds all Behat services, its extensions services, our contexts,
pages and helper services used by them

- ``symfony`` - the container which holds all the services defined in the application, the services retrieved from this
container are isolated between scenarios, belongs to ``scenario`` scope

- ``symfony_shared`` - the container which holds all the services defined in the application, created only once,
the services retrieved from this container are not isolated between scenarios

There is one additional scope, ``scenario``, which ensures, that no service shared through entire Behat execution uses
the one that is only available in specific scenario. Every service retrieved from ``symfony`` container has this scope.

Usually, most of contexts and pages are defined in ``scenario`` scope as it's the most safe decision. You would need a
really got reason not to follow this convention.

Right now, you can only inject services from foreign containers into the default containers. To do so, use ``container``
option within ``argument`` element:

.. code-block:: xml
<service id="service.id" class="Class">
<argument type="service" id="behat.service.id" />
<argument type="service" id="another.behat.service.id" container="behat" />
<argument type="service" id="symfony.service.id" container="symfony" /> <!-- to use this container, your service must be in scenario scope too -->
<argument type="service" id="shared.symfony.service.id" container="symfony_shared" />
</service>
4 changes: 4 additions & 0 deletions behat/map.rst.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* :doc:`/behat/how-to-add-new-context`
* :doc:`/behat/how-to-add-new-page`
* :doc:`/behat/how-to-define-new-suite`
* :doc:`/behat/introduction-to-containers-and-scopes`
1 change: 1 addition & 0 deletions contributing/code/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ Contributing Code
bdd
standards
conventions
behat_guide
git
license
12 changes: 12 additions & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ Learn about how Sylius integrates with third-party applications.

.. include:: /integrations/map.rst.inc

The Behat Guide
---------------

Learn how to write clean and reusable features, contexts and pages.

.. toctree::
:hidden:

behat/index

.. include:: /behat/map.rst.inc

Migration Guide
---------------

Expand Down

0 comments on commit f8f7758

Please sign in to comment.