From 91671ac792023339bdeb9a31c942eb64a3b240db Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Tue, 30 Sep 2025 18:06:49 +0000 Subject: [PATCH 1/3] update to tell PHPUnit assertions stopped working resolve Behat/Behat#1618 --- quick_start.rst | 51 +++++++++++++++++++++----------------------- useful_resources.rst | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/quick_start.rst b/quick_start.rst index 2cb8090..aad9bb7 100644 --- a/quick_start.rst +++ b/quick_start.rst @@ -455,21 +455,32 @@ 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 + ) + ); + } } } @@ -477,27 +488,13 @@ As you can see, in order to test and implement our application, we introduced 2 ``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: diff --git a/useful_resources.rst b/useful_resources.rst index 4fe2abd..c67ad4b 100644 --- a/useful_resources.rst +++ b/useful_resources.rst @@ -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 ----------------- From 66a38a45651ce031e7f6613923fc9f6c662ffa1d Mon Sep 17 00:00:00 2001 From: Andrew Coulton Date: Thu, 8 Jan 2026 21:38:01 +0000 Subject: [PATCH 2/3] cr fixes --- useful_resources.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/useful_resources.rst b/useful_resources.rst index c67ad4b..ec02afb 100644 --- a/useful_resources.rst +++ b/useful_resources.rst @@ -49,7 +49,7 @@ For example a list of the most known: **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: + to use the PHPUnit assertions is to bootstrap PHPUnit during Behat execution from a ``BeforeSuite`` hook: .. code-block:: php From 6054f55a308c2ce8a2141957050e06d9c7b0ba92 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Fri, 16 Jan 2026 14:55:58 +0000 Subject: [PATCH 3/3] fix the PHPUnit caution block --- useful_resources.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/useful_resources.rst b/useful_resources.rst index ec02afb..8f9cbc2 100644 --- a/useful_resources.rst +++ b/useful_resources.rst @@ -31,8 +31,11 @@ For example a list of the most known: - https://github.com/beberlei/assert - https://github.com/zenstruck/assert -.. caution:: +.. admonition:: Caution with PHPUnit + :class: caution + If you are familiar with PHPUnit, you can use its assertion library + .. code-block:: bash $ php composer.phar require --dev phpunit/phpunit