Coder Social home page Coder Social logo

laravel-bdd-helpers's Introduction

Laravel BDD Helpers

Stuff that helps you do BDD in a Laravel context.

This is stil WIP, but feel free to start using it in your projects!

Content

  1. Installation
  2. Laravel Behat Context
  3. Environment Behat Context
  4. HTTP client Behat context
  5. Real life examples

Installation

Install it trough Composer:

"suhm/laravel-bdd-helpers": "dev-master"

Laravel Behat context

The Laravel Behat context trait makes a Laravel Application instance available to you in your Behat features.

Use it like this:

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Behat context class.
 */
class FeatureContext implements SnippetAcceptingContext
{
    use LaravelBdd\Behat\Laravel;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context object.
     * You can also pass arbitrary arguments to the context constructor through behat.yml.
     */
    public function __construct()
    {
        $this->prepareApplication(__DIR__.'/../../bootstrap/start.php');
    }
}

Now you can interact with the Application instance like you can with Laravel's default TestCase.

Environment Behat Context

By default, the Laravel context will set the environment to testing. If you need to set the environment to something else, or maybe you aren't using the Laravel context trait but still need to load your test configuration files.

The Environment trait includes a method called setEnvironment(). The first parameter is the name of the environment, and defaults to testing. The second parameter is the name of the environment var to export, and defaults to APP_ENV.

You can set the environment name of your application and optionally export it:

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Behat context class.
 */
class FeatureContext implements SnippetAcceptingContext
{
    use LaravelBdd\Behat\Environment;
    use LaravelBdd\Behat\Laravel;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context object.
     * You can also pass arbitrary arguments to the context constructor through behat.yml.
     */
    public function __construct()
    {
        $this->setEnvironment(); // Default env is 'testing', export 'APP_ENV=testing'
        $this->setEnvironment('acceptance'); // Set env to 'acceptance' and export 'APP_ENV=acceptance'
        $this->setEnvironment('testing', 'SOMETING'); // Set env to testing and export 'SOMETHING=testing'
        $this->prepareApplication(__DIR__.'/../../bootstrap/start.php');
    }
}

HTTP client Behat context

If you need to hit routes and controller actions, you can use this trait to get an instance of the Laravel HTTP client.

You can then do stuff like this:

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Behat context class.
 */
class FeatureContext implements SnippetAcceptingContext
{
    use LaravelBdd\Behat\Laravel;
    use LaravelBdd\Behat\HttpClient;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context object.
     * You can also pass arbitrary arguments to the context constructor through behat.yml.
     */
    public function __construct()
    {
        $this->prepareApplication(__DIR__.'/../../bootstrap/start.php');
        $this->createClient();
    }

    /**
     * @When I visit :uri
     */
    public function iVisit($uri)
    {
        $this->client->request('GET', $uri);
    }

Real life examples

For real life examples of doing BDD in a Laravel context, please see this repository: https://github.com/petersuhm/laravel-bdd

laravel-bdd-helpers's People

Contributors

petersuhm avatar

Stargazers

Will Murray avatar Jeff Madsen avatar mukaken avatar Philip Brown avatar quickgreen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.