BDD with PHP livecoding

Behavior-driven development livecoding session in PHP

with Behat, PHPSpec and PHPUnit.

Jonathan VUILLEMIN

October 2019

First, let's remind a bit of theory ...

(quickly, as that's not the goal today)

BDD is a process designed to aid the management and the delivery of software development projects by improving communication between engineers and business professionals.

 

In so doing, BDD ensures all development projects remain focused on delivering what the business actually needs while meeting all requirements of the user.

What is BDD ?

Behaviour Driven Development

How does it work ?

Quick notes:

  • Business and developers speak the same ubiquitous language to illustrate behaviors (features)
  • Developers use illustrated behaviors (features) as the basis of automated tests (TDD)

Simplified BDD workflow

Want more theory ?

Check these guys !

... ok, and what about the tools ?

Some BDD cool PHP tools quick tour.

Gherkin

The ubiquitous language

Feature: Article publishing while authenticated

  Background:
    Given I have a user account with username "ekkinox"
    And I have the "author" role
    And I have the "publisher" role

  Scenario: I want to publish an article
    Given I am logged as "ekkinox"
    When I publish an article named "BDD is magic"
    Then I should see an article named "BDD is magic"
    And ...
    But ...
  • Both business and developers readable / writable
  • Domain specification
  • Basis for living documentation
  • Grammar supported in 60+ spoken languages

Behat

BDD framework for PHP to help you test business expectations


Scenario: I want to publish an article
  Given I am logged as "ekkinox"
  • Domain definition / specification focused tool
  • Complete, useful CLI & elegant gherkin support
  • Easy to use in frameworks & cool extensions
/**
 * @Given I am logged as :arg1
 */
public function iAmLoggedAs($arg1)
{
    $this->loginService->login($arg1);
    
    Assert::assertTrue($this->loginService->isLogged());
    Assert::assertEquals($arg1, $this->loginService->getLoggedUserName());
}

PHPSpec

Toolset to drive emergent design by specification

class LoginServiceSpec extends ObjectBehavior
{
    function it_has_by_default_no_logged_user()
    {
        $this->getLoggedUserName()->shouldReturn(null);
    }
}
  • Implementation focused tool
  • Complete, useful CLI
  • Extensible with plugins (example)
Do you want me to create `MyProject\Service\LoginService::getLoggedUserName()`for you?

[Y/n]

PHPUnit

Programmer-oriented testing framework for PHP

$stub = $this->createMock(SomeClass::class);

$stub->method('doSomething')->willReturn('foo');

$this->assertEquals('foo', $stub->doSomething());
  • Ok ...
  •         ... you know that one !

... so, let's use them !

PHP live coding example:

Create a (very simple) bank account management.

Sources can be found here:

https://github.com/ekkinox/bdd-livecoding

Questions ?

Thank you !

BDD with PHP

By Jonathan VUILLEMIN

BDD with PHP

PHP BDD livecoding session slides

  • 1,440