4 min. read
Categories: Business Technical
BDD in Sylius: Part One
BDD in Sylius: Part One

Hello folks! With this blog post I would like to start the series of articles about Behat usage in our beloved project.

Introduction

As you all know, Sylius is built on strong BDD fundamentals, which is one of the most important things that makes Sylius what it is. Moreover, after years of using this approach in open-source as well as in commercial projects, we not only believe in Behavior-Driven Development – we actually know it works. Therefore, we’re eager to share our knowledge and experience. This series will be our ode to BDD methodology and one of the greatest tools you can use to implement it in your projects – Behat.

Behavior-Driven… what?

If you’re at the beginning of your adventure with Sylius (or even programming in PHP at all), you may have not heard about BDD methodology. Making long story short, Behavior-Driven Development is derived from Test-Driven Development, and based on the same test-focused approach.


BDD Cycle
The difference is, that BDD focuses a lot on business value provided by the software, not only on quality of the software itself. Moreover, Behavior-Driven Development improves application development process by decreasing number of translations between business and technical people. It’s achieved by developing common language platform called “ubiquitous language”, which has one goal – ensure that all people involved in a project use the same words to describe the same concepts.

One of the most important (and most difficult) thing during developing BDD-based project, is describing business expectations in detail. BDD methodology focuses on examples as the most precise way to explain purpose of the specific feature. This is the so-called Story BDD, which with the power of user stories, helps developers to build more mature and accurate systems. As PHP developers, we are lucky to have one of the best tools ever made for Story BDD.

What is (love) Behat?

Behat is an open-source Story BDD tool created for PHP applications. Even though it’s heavily inspired by Ruby’s tool Cucumber, now it’s considered as it’s official implementation for PHP language. In the end, it’s also easy to customize and extend. At Sylius, we had a great pleasure to develop our own extensions improving this already great tool. In fact, we have enjoyed it so much that together with my teammates, Łukasz & Kamil, we decided to create Friends of Behat organization. Make sure to check it out!

As BDD methodology says, example is the best way to explain the goal, and so Behat allows to create example-focused user stories. Not only they can be used for better understanding your application’s purpose (which is really important – every feature should have a reason to live), but also can be used to test these features after they’re implemented. As a result, we have the tool that takes care for both, outer and inner quality of the application.

Sylius has been using Behat for the last few years. We strongly believe it’s one of these tools that helps you sleep calmly. Basing on our experience we can say for sure, that using Behat makes the project stronger and better tested – and therefore helps providing new features more securely and smoothly.

How to use it?

Following BDD rules, the best way to show you the power of Behat is showing you the example of its usage. The first thing that needs to be done, is .feature file written with Gherkin language. This is how it can look like:

1  Feature: Receiving fixed discount on cart
2     In order to pay proper amount while buying promoted goods
3     As a Visitor
4     I want to have promotions applied to my cart
5
6     Background:
7         Given the store has a product "PHP T-Shirt" priced at $100.00
8         And there is a promotion "Holiday promotion"
9         And it gives $10.00 discount to every order
10
11    Scenario: Receiving fixed discount for my cart
12        When I add product "PHP T-Shirt" to the cart
13        Then my cart total should be $90.00
14        And my discount should be -$10.00

As you can see, scenario describes one specific example of the feature – and therefore shows exactly what we want to achieve after implementing it. Whole feature file consists of a few conventional parts:

  1. Feature title (line 1)
  2. Feature benefit (line 2)
  3. Feature actor (line 3) – the beneficiary of the feature
  4. Feature way to reach the benefit (line 4)
  5. Background (line 6) – application setup, usually containing some data that should be placed in database before any action can be taken
  6. One or more scenarios (line 11) – list of specific steps which are taken to fulfill the feature benefit

What is more, features are not written in some programming language, they are normal sentences, that can be easily understood by any non-technical person – and this is exactly how it should be!

Next, each of these steps is mapped to PHP code, in a class called Context, where the required setup and assertions are made. Below you can see, how some of such implementations can look like:

/**
 * @Given the store has a product :name priced at :price
 */
public function theStoreHasProduct(string $name, string $price): void
{
    $product = new Product();
    $product->setName($name);
    $product->setPrice(
        new Money(str_replace(['$', '.'], '', '$100.00'), new Currency('USD'))
    );

    $this->productRepository->save($product);
}

/**
 * @Then my cart total should be :total
 */
public function myCartTotalShouldBe(string $total): void
{
    $cartTotal = $this->getDocument()->find('css', '#cart-total')->getText();

    assert($cartTotal === $total);
}

Of course at the beginning your test will fail – perhaps you don’t have the cart page or even a product entity yet. Nevertheless the goal is explained, you can start coding and be sure, that you’ve implemented exactly what you wanted to achieve!

One more thing – provided steps implementations are naturally only examples, they can differ depending on your application structure and adopted solutions. At the end the only thing that matters is having well designed and fully tested application, which fulfills the business expectations – and this is one of the most reliable ways to achieve it.

What’s next?

Obviously those are not the only features of Behat, which is quite a complex tool. In the next blog posts from the BDD series we will provide more awesome and advanced examples of Behat usage in Sylius (and possibly in your applications as well). I hope this article would be a nice first step in your adventure with BDD – or would ensure you that this path is the proper one. Stay tuned and subscribe to our newsletter to get the next text straight into your inbox!

Tags: BDD Testing
Share:
Mateusz Zalewski
Long-time Core Team member and the main Trainer in the Sylius company with the current focus on improving training programs to make them as fun and developing for the Sylius newcomers as possible. He's also hung up on enhancing Sylius' architecture and its plugins' system. Privately, a huge enthusiast of history and astronomy.
More from our blog
Cloud 4 min read 17.06.2024
We are thrilled to announce that we just signed a strategic partnership with Platform.sh, and as a result, we are extending our offer with Sylius Cloud powered by Platform.sh. Platform.sh is a modern Platform-as-a-Service (PaaS) solution that allows businesses to leverage the cloud environment without losing access to the code… Read More
Technical 4 min read 11.06.2024
Abstract 1.12 released in Q4 2022 1.13 on Apr 23rd, 2024 (a year later than we anticipated while releasing 1.12) 3859 commits 23 contributors A stabilized Sylius API powered by API Platform It’s been a long and bumpy road. Having it behind our backs was a highway that led Sylius… Read More
Business Ecosystem News 4 min read 06.06.2024
Welcome to the May summary! As an open-source eCommerce framework, Sylius continues to evolve with significant contributions from our vibrant community and valuable product updates. Apart from describing the technical changes, we will also quickly summarize the Sylius Technical Fundamentals & Sylius Polish Community Meetup and eCommerce Day Kaunas, as… Read More
Comments