Skip to content
Merged
Show file tree
Hide file tree
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
51 changes: 24 additions & 27 deletions quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,49 +455,46 @@ code we could come up with to fulfil our scenario. Something like this:
#[Then('I should have :arg1 product(s) in the basket')]
public function iShouldHaveProductInTheBasket($count)
{
// Normally you would import this class - we are using the fully qualified name
// to highlight that Behat does not come with an assertion tool (see note below).
\PHPUnit\Framework\Assert::assertCount(
intval($count),
$this->basket
);
if (count($this->basket) !== intval($count)) {
throw new \Exception(
sprintf(
'The basket should have %d item(s), but it has %d.',
intval($count),
count($this->basket)
)
);
}
}

#[Then('the overall basket price should be £:arg1')]
public function theOverallBasketPriceShouldBePs($price)
{
\PHPUnit\Framework\Assert::assertSame(
floatval($price),
$this->basket->getTotalPrice()
);
$expectedPrice = floatval($price);
$actualPrice = $this->basket->getTotalPrice();

if ($expectedPrice !== $actualPrice) {
throw new \Exception(
sprintf(
'Expected basket total price to be %s, but got %s.',
$expectedPrice,
$actualPrice
)
);
}
}
}

As you can see, in order to test and implement our application, we introduced 2 objects -
``Shelf`` and ``Basket``. The first is responsible for storing products and their prices,
the second is responsible for the representation of our customer basket. Through appropriate step
definitions we declare products' prices and add products to the basket. We then compare the
state of our ``Basket`` object with our expectations using PHPUnit assertions.
state of our ``Basket`` object with our expectations and throw exception if the expectations aren't met.

.. note::

Behat doesn't come with its own assertion tool, but you can use any proper assertion
tool out there. A proper assertion tool is a library whose assertions throw
exceptions on failure. For example, if you're familiar with PHPUnit you can use
its assertions in Behat by installing it via composer:

.. code-block:: bash

$ php composer.phar require --dev phpunit/phpunit

and then by simply using assertions in your steps:

.. code-block:: php

\PHPUnit\Framework\Assert::assertCount(
intval($count),
$this->basket
);
tool out there.
Learn more about :ref:`assertion-tools`.

Now try to execute your feature tests:

Expand Down
51 changes: 51 additions & 0 deletions useful_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,57 @@ Integrating Behat with PHPStorm
More information on integrating Behat with PHPStorm can be found in this
`blog post`_.

.. _assertion-tools:

Assertion tools
---------------

A proper assertion tool is a library whose assertions throw exceptions on failure.

For example a list of the most known:

- https://github.com/webmozarts/assert
- https://github.com/beberlei/assert
- https://github.com/zenstruck/assert

.. caution::
If you are familiar with PHPUnit, you can use its assertion library
.. code-block:: bash

$ php composer.phar require --dev phpunit/phpunit

and then by simply using assertions in your steps:

.. code-block:: php

\PHPUnit\Framework\Assert::assertCount(
intval($count),
$this->basket
);

**WARNING: using PHPUnit for assertions no longer works with PHP 11.3.0 and later out-of-the-box**.

This is due to a change in how PHPUnit's internal components are initialized. The recommended workaround
to use the PHPUnit assertions is to bootstrap PHPUnit during Behat execution from a ``BeforeSuite`` hook:

.. code-block:: php

use Behat\Hook\BeforeSuite;

class FeatureContext {

#[BeforeSuite]
public static function initPhpunit() {
(new \PHPUnit\TextUI\Configuration\Builder())->build([]);
}
}

If you have multiple suites, you may want to use a static variable in the hook to ensure the initialization only
runs once.

Learn more at https://github.com/Behat/Behat/issues/1618.


Behat cheat sheet
-----------------

Expand Down