Giter Site home page Giter Site logo

phpspec-laravel's Introduction

#PhpSpec Laravel Extension

Work-in-progress PhpSpec Extension for testing Laravel applications.

##Why this extension?

This extension allows you to test your objects and classes as you would normally with PhpSpec, but gives you a Laravel application context to test within, so that you can continue to make use of Laravel's nice features.

In detail,

it:

  • Bootstraps the Laravel environment, so that you can use class aliases across your application without running into to testing trouble, and so that you can make use of Laravel's environment configuration for your testing environment. You will also continue to be able to use Laravel's helper functions across your codebase
  • Allows you to test your Eloquent models, which I ran into difficulty with before writing this extension
  • Provides a few extra Laravel-specific PhpSpec matchers to make testing your application code more straightforward

and

it is not:

  • A swap-in replacement for Laravel's built in PHPUnit setup. If you'd like functional tests, please use that, Behat, or Codeception

##Installation

Add this to your composer.json:

{
    "require": {
        "benconstable/phpspec-laravel": "*"
    }
}

then add this to your phpspec.yml:

extensions:
    - PhpSpec\Laravel\Extension\LaravelExtension

##Configuration

###Testing environment

By default, the extension bootstraps Laravel in the testing environment. You can change this to production (or whatever you like) by setting:

laravel_extension:
    testing_environment: 'production'

in your phpspec.yml.

###Database migration

If you'd like your database migrations to be run before each spec, you can specify:

laravel_extension:
    migrate_db: true

This is useful if you'd like to make use of a SQLite in-memory database for any Eloquent model tests (see here for how you'd set this up).

##Usage

###General testing

You should test your regular classes by extending the PhpSpec\Laravel\LaravelObjectBehavior class:

<?php namespace spec;

use PhpSpec\Laravel\LaravelObjectBehavior;

class MyClassSpec extends LaravelObjectBehavior {

    // Test code here...
}

###Testing Eloquent models

You should test your Eloquent models by extending the PhpSpec\Laravel\EloquentModelBehavior class:

<?php namespace spec;

use PhpSpec\Laravel\EloquentModelBehavior;

class MyPostModelSpec extends EloquentModelBehavior {

    public function it_should_have_comments()
    {
        $this->comments()->shouldDefineRelationship('hasMany', 'Comment');
    }
}

###Accessing the IoC container

You shouldn't need to, but just in case, the booted Laravel IoC container can be accessed like:

<?php

$this->laravel->app['variable'];

in your specs.

##Custom Matchers

Some custom matchers are provided for convenience, feel free to ignore them completely!

###DefineRelationshipMatcher

This matcher lets you check for the existence of a valid Eloquent relationship.

####Usage

should[Not]DefineRelationship('relationshipType', 'Related\Class')

Example

<?php namespace spec;

use PhpSpec\Laravel\EloquentModelBehavior;

class MyPostModelSpec extends EloquentModelBehavior {

    public function it_should_have_comments()
    {
        $this->comments()->shouldDefineRelationship('hasMany', 'Comment');
    }
}

##Roadmap

  • Improved code generation for Laravel
  • More useful Eloquent matchers
  • Tests

##Further reading

The following articles/websites have been useful to me when developing this extension:

  • This open issue on the PHPSpec repo was a good help and is an interesting read
  • Taylor Otwell's video on DI and unit testing in Laravel
  • Laracasts has a few posts and guides on PHPSpec and Laravel
  • This tutorial has some useful information on setting up your database for testing

phpspec-laravel's People

Contributors

benconstable avatar

Watchers

Mariusz Szot avatar James Cloos 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.