19. In BDD, you write feature files in the form of user stories
that you test against.
20. BDD uses a ubiquitous language - basically, a language
that business stakeholders, project managers, developers
and our automated tools can all understand.
21. Sample Behat Feature File
Feature: Laravel Test
In order to demonstrate Laravel and Behat
As a user
I need to be able to visit the homepage of a new Laravel app
Scenario: Homepage
Given I am on the homepage
Then I should see "Laravel 5"
45. features/fitbit.feature
Feature: Fitbit Integration
In order to obtain Fitbit data
As a user
I need to be able to authenticate with Fitbit
Scenario: Not yet authenticated
Given I am not logged in as “josh@creatuity.com”
When I go to "/fitbit/"
Then I should see "Please authenticate"
46. vendor/bin/behat —append-snippets
Scenario: Not yet authenticated:6
Given I am not logged in as “josh@creatuity.com
When I go to "/fitbit/"
Then I should see "Please authenticate"
1 scenario (1 undefined)
3 steps (1 undefined, 2 skipped)
0m0.48s (11.00Mb)
u features/bootstrap/FeatureContext.php - `I am not logged in as`
definition added
50. Behat expects us to add logic to this function to
detect the user is not logged in.
51. Before we do that, let’s finish out our feature file.
52. features/fitbit.feature continued
Scenario: I have authenticated
Given I am logged in as “josh@creatuity.com”
When I go to "/fitbit/"
Then I should see "Welcome back"
Scenario: I have sleep data
Given I am logged in as “josh@creatuity.com”
When I go to "/fitbit/sleep/"
Then I should see "Sleep Report"
55. /features/bootstrap/FeatureContext.php
/**
* @Given I am not logged in as :email
*/
public function iAmNotLoggedInAs($email)
{
// We completely log out
// Destroy the previous session
if (Session::isStarted()) {
Session::regenerate(true);
} else {
Session::start();
}
}
56. /features/bootstrap/FeatureContext.php
public function iAmLoggedInAs($email)
{
// Destroy the previous session
if (Session::isStarted()) {
Session::regenerate(true);
} else {
Session::start();
}
// Login the user and since the driver and this code now
// share a session this will also login the driver session
$user = User::where('email', $email)->firstOrFail();
Auth::login($user);
// Save the session data to disk or to memcache
Session::save();
// Hack for Selenium
// Before setting a cookie the browser needs to be launched
if ($this->getSession()->getDriver() instanceof BehatMinkDriverSelenium2Driver) {
$this->visit('login');
}
// Get the session identifier for the cookie
$encryptedSessionId = Crypt::encrypt(Session::getId());
$cookieName = Session::getName();
// Set the cookie
$minkSession = $this->getSession();
$minkSession->setCookie($cookieName, $encryptedSessionId);
}
58. vendor/bin/behat
…
Scenario: I have sleep data
Given I am logged in as "josh@creatuity.com"
When I go to "/fitbit/sleep/"
Then I should see "Sleep Report"
The text "Sleep Report" was not found anywhere in the text of
the current page. (BehatMinkExceptionResponseTextException)
--- Failed scenarios:
features/fitbit.feature:6
features/fitbit.feature:11
features/fitbit.feature:16
71. vendor/bin/phpspec run
10 ! is initializable (142ms)
class Fitbit does not exist.
15 ! connects to fitbit (100ms)
class Fitbit does not exist.
20 ! returns sleep data
class Fitbit does not exist.
---- broken examples
Fitbit
10 ! is initializable (142ms)
class Fitbit does not exist.
Fitbit
15 ! connects to fitbit (100ms)
class Fitbit does not exist.
Fitbit
20 ! returns sleep data
class Fitbit does not exist.
1 specs
3 examples (3 broken)
76. This is very powerful with frameworks like Laravel
and Magento, which have PHPSpec plugins that
help PHPSpec know where class files should be
located.
77. Fitbit.php - class Fitbit {
function connect($email)
{
// TODO: write logic here
}
function sleepData($email)
{
// TODO: write logic here
}