Why doesn't the code work if the business said "everything is correct"?
\n\nHave you ever encountered a situation: the manager claims the task is done, tests pass green, but the user is still unhappy? A classic scenario. The problem isn't the code — the problem is communication. We speak different languages: business operates with scenarios, while developers use methods and classes. This is where BDD (Behaviour-Driven Development) comes to the rescue — development driven by behavior. And yes, it works brilliantly in PHP.
\n\nIn this article, we'll explore how BDD in PHP helps turn vague "wants" into clear tests that both managers and programmers understand. You'll learn how BDD differs from regular TDD, how to write scenarios in Gherkin, and which tools (Behat, PHPSpec) actually simplify your life.
\n\nWhat is BDD in simple terms?
\n\nBDD (Behaviour-Driven Development) is an evolution of TDD. While TDD focuses on "how to test code," BDD answers the question "what should the code do to make the business happy."
\n\nImagine you're building an online store. Instead of an abstract test like "the applyDiscount() method should reduce the price," you write a scenario:
\n\n- \n
- Given: there is a product worth 1000 rubles in the cart \n
- When: the user enters the promo code "SALE10" \n
- Then: the cart total becomes 900 rubles \n
This scenario can be read by anyone — from a marketer to a team lead. No magic, just system behavior.
\n\nBDD vs TDD: What's the difference?
\n\nMany people confuse these concepts. Let's set the record straight:
\n\n- \n
- TDD — write a test, then code, then refactor. Tests are written by developers for developers. \n
- BDD — first write a behavior scenario in natural language, then automate it. Scenarios are written in dialogue with the business. \n
BDD doesn't replace TDD; it complements it. You still write unit tests, but now you have "living documentation" in the form of scenarios.
\n\nBDD Tools for PHP: Choosing Your Weapon
\n\nThe PHP ecosystem offers two main tools for behaviour-driven development: Behat and PHPSpec. They solve different tasks.
\n\nBehat — The Universal Soldier
\n\nBehat is a framework for acceptance testing. It works with the Gherkin language (that same "Given-When-Then"). Behat is ideal for integration tests, API checks, web interfaces, and high-level business logic.
\n\nPHPSpec — A Microscope for Developers
\n\nPHPSpec is a tool for class specification. It's closer to TDD, but with the BDD philosophy: you describe the behavior of an object, not its internals. PHPSpec is great for unit testing domain logic.
\n\nIn this article, we'll focus on Behat, as it best illustrates behaviour-driven development in PHP.
\n\nWriting Your First BDD Scenario in PHP with Behat
\n\nLet's create a real example. Suppose we are developing a loyalty system. The customer gets a 10% discount if the order total exceeds 5000 rubles.
\n\nStep 1: Installing Behat
\n\ncomposer require --dev behat/behat\n\nThen initialize the project:
\n\nvendor/bin/behat --init\n\nThe structure will be:
\n\nfeatures/\n bootstrap/\n FeatureContext.php\n discount.feature\n\nStep 2: Writing a Feature File in Gherkin
\n\nCreate the file features/discount.feature:
Feature: Discount calculation for loyal customers\n As a sales manager\n I want to provide a 10% discount on orders over 5000 rubles\n So that I can encourage large purchases\n\n Scenario: Order exceeds the discount threshold\n Given there are products worth 6000 rubles in the cart\n When I request the total cost\n Then the total cost should be