2017 © Pedro Peláez
 

library phpspec-laravel

Test your Laravel applications with PhpSpec

image

benconstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  • Thursday, July 12, 2018
  • by BenConstable
  • Repository
  • 12 Watchers
  • 141 Stars
  • 150,864 Installations
  • PHP
  • 25 Dependents
  • 0 Suggesters
  • 44 Forks
  • 1 Open issues
  • 19 Versions
  • 6 % Grown

The README.md

phpspec Laravel Extension

phpspec extension for testing Laravel applications., (*1)

Build Status Latest Stable Version Total Downloads License, (*2)

Versions

Depending on the version of Laravel and/or Phpspec you're using, you'll want to make sure that you're using the version of this package that's right for you. Use the table below to pick the right one., (*3)

PHP Version Package Version Laravel Version Phpspec Version
>=5.3.0 ^v1.2 ^v4.1 ^v2.0
>=5.4.0 ^v2.0 v5.0-v5.3 ^v2.1
>=5.6.0 ^v3.0 ^v5.1-v5.4 ^v3.0
>=7.0.0 ^v4.0 ^v5.4 ^v4.0

Installation

Install the package with composer:, (*4)

composer require --dev "benconstable/phpspec-laravel:~4.0"

then add this to your phpspec.yml:, (*5)

extensions:
  PhpSpec\Laravel\Extension\LaravelExtension: ~

You can take a look at example.phpspec.yml for a good set of sensible phpspec defaults for a Laravel project., (*6)

Why this extension?

This extension provides you with a bootstrapped Laravel environment when writing your phpspec tests., (*7)

It allows you to make use of some of the nice features that Laravel provides, like class aliases and helper functions, without being hindered by your testing framework., (*8)

This extension is not a swap-in replacement for Laravel's built in PHPUnit setup. If you'd like integration and/or functional tests, please use that, Behat, or Codeception., (*9)

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:, (*10)

extensions:
  PhpSpec\Laravel\Extension\LaravelExtension:
    testing_environment: "production"

in your phpspec.yml., (*11)

App bootstrap path

By default, the extension will bootstrap your app by looking for bootstrap/app.php in the directory above vendor/. This is the default location that Laravel provides., (*12)

You can manually specify the path to the bootstrap file if you're using a non-standard installation, like so:, (*13)

extensions:
  PhpSpec\Laravel\Extension\LaravelExtension:
    framework_path: "/non/standard/laravel/setup/app.php"

You can specify either an absolute path (use leading slash), or a path relative to the vendor/ directory., (*14)

Usage

Testing without Laravel

If you're not using any code specific to the Laravel environment, then you don't need to do anything differently. Just write your phpspec tests as normal!, (*15)

Testing with Laravel

If you want to take advantage of Laravel's aliases, or use some of its helper functions, extend your specs from PhpSpec\Laravel\LaravelObjectBehavior. This will prevent errors when testing., (*16)

For example, this class uses an alias:, (*17)

<?php
namespace App;

use Inspiring;

class MyInspiring extends Inspiring
{
    public function quoteBackwards()
    {
        return strrev(parent::quote());
    }
}

and without extending from PhpSpec\Laravel\LaravelObjectBehavior:, (*18)

<?php
namespace spec\App;

use PhpSpec\ObjectBehavior;

class MyInspiringSpec extends ObjectBehavior
{
    function it_inspires_backwards()
    {
        $this->quoteBackwards()->shouldBeString();
    }
}

you'll get Fatal error: Class 'Inspiring' not found.... But extending from PhpSpec\Laravel\LaravelObjectBehavior:, (*19)

<?php
namespace spec\App;

use PhpSpec\Laravel\LaravelObjectBehavior;

class MyInspiringSpec extends LaravelObjectBehavior
{
    function it_inspires_backwards()
    {
        $this->quoteBackwards()->shouldBeString();
    }
}

you'll get ✔ inspires backwards., (*20)

and this class uses a helper function:, (*21)

<?php
namespace App;

class MyEncryptor
{
    public function encrypt($arg)
    {
        return bcrypt($arg);
    }
}

and without extending from PhpSpec\Laravel\LaravelObjectBehavior:, (*22)

<?php
namespace spec\App;

use PhpSpec\ObjectBehavior;

class MyEncryptor extends ObjectBehavior
{
    function it_encrypts_a_string()
    {
        $this->encrypt()->shouldBeString();
    }
}

you'll get Fatal error: Call to a member function make() on a non-object.... But extending from PhpSpec\Laravel\LaravelObjectBehavior:, (*23)

<?php
namespace spec\App;

use PhpSpec\Laravel\LaravelObjectBehavior;

class MyEncryptor extends LaravelObjectBehavior
{
    function it_encrypts_a_string()
    {
        $this->encrypt()->shouldBeString();
    }
}

you'll get ✔ encrypts a string., (*24)

Accessing the IoC container

If you need to access the Service Container in your specs, just use the app() helper!, (*25)

Learning more about phpspec and Laravel

Laracasts has some great guides on phpspec and Laravel. 'Laravel, phpspec and refactoring' is a good starting point; it shows how you should use phpspec with Laravel, and covers the basics of writing tests (and it's free!)., (*26)

Contributing

See CONTRIBUTING.md., (*27)

License

MIT © Ben Constable. See LICENSE for more info., (*28)

Thanks

Thanks to..., (*29)

  • @obrignoni for their great work in getting this extension working with Laravel 5
  • @Sam-Burns for their great work in getting this extension working with Phpspec v3 and v4
  • All of the other contributors and to everyone that's reported issues and bugs with the project

The Versions

12/07 2018

dev-master

9999999-dev http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

12/07 2018

v4.0.1

4.0.1.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

28/04 2018

v4.0.0

4.0.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

29/01 2017

v3.0.2

3.0.2.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

29/01 2017

3.0.x-dev

3.0.9999999.9999999-dev http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

13/09 2016

v3.0.1

3.0.1.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

02/08 2016

v3.0.0

3.0.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

02/08 2016

2.0.x-dev

2.0.9999999.9999999-dev http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

15/03 2015

v2.0.0

2.0.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

25/02 2015

dev-laravel-4

dev-laravel-4 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

14/10 2014

v1.6.0

1.6.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

18/09 2014

v1.5.0

1.5.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

17/09 2014

v1.4.0

1.4.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

16/06 2014

v1.3.0

1.3.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

10/06 2014

v1.2.0

1.2.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

13/05 2014

v1.1.1

1.1.1.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

19/04 2014

v1.1.0

1.1.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

27/03 2014

v1.0.1

1.0.1.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec

24/03 2014

v1.0.0

1.0.0.0 http://github.com/BenConstable/phpspec-laravel

Test your Laravel applications with PhpSpec

  Sources   Download

MIT

The Requires

 

laravel test testing phpspec