PHPackages                             kirkbowers/sphec - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kirkbowers/sphec

ActiveProject

kirkbowers/sphec
================

A Behavior Driven Development (BDD) toolkit in PHP.

v0.2.2(8y ago)020MITPHP

Since Jul 31Pushed 7y ago1 watchersCompare

[ Source](https://github.com/kirkbowers/sphec)[ Packagist](https://packagist.org/packages/kirkbowers/sphec)[ RSS](/packages/kirkbowers-sphec/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (1)Versions (9)Used By (0)

Sphec
=====

[](#sphec)

A Behavior-Driven Development (BDD) toolkit in PHP.

Introduction
------------

[](#introduction)

Sphec is a Behavior-Driven Development (BDD) toolkit for PHP that is largely inspired by [RSpec](http://rspec.info/), [Jasmine](https://jasmine.github.io/), and to a degree by [Cucumber](https://cucumber.io/). The name is somewhat of a contraction of "spec" and "PHP". It provides a domain specific language (DSL) that is a mixture of natural language shorthand and quasi-PHP for describing how a part of your software should behave and writing tests to ensure it does, in fact, behave that way.

Some features are:

- Hierarchical contexts for grouping related tests and setting up different pre-test conditions.
- `before` and `after` actions to set up and tear down pre-test conditions.
- "Local" variables that propagate from the outer context up to each contained example.
- Tests that are phrase as expectations.

Here's what an example Sphec file looks like, with comments to point out some of the features:

```
require_once __DIR__ . '/../vendor/autoload.php';

// Specify the behavior of a class
specify TestProject\Accumulator

  // Describe how a method of that class is expected to behave
  describe add

    // Set up a subcontext with pre-test conditions shared by multiple examples.
    context with default starting value

      // Set up those pre-test conditions in a before action
      before
        // "Local" variables to be shared by blocks within a context are declared
        // using the @ symbol
        @accumulator = new TestProject\Accumulator;

      // Provide examples of what the expected behavior is.
      it should start with a zero value
        // Note, the "local" member variable accumulator that was create in `before` is
        // available inside the examples.

        // Write tests as expectations.
        expect(@accumulator->get_value())->to_be(0);

      it should accumulate a single value
        @accumulator->add(3);
        expect(@accumulator->get_value())->to_be(3);

      it should accumulate more than one value
        @accumulator->add(3);
        @accumulator->add(5);
        expect(@accumulator->get_value())->to_be(8);

    // Set up a different context with different pre-test conditions.
    context with supplied starting value
      before
        @accumulator = new TestProject\Accumulator(2);

      it should start with a zero value
        expect(@accumulator->get_value())->to_be(2);

      it should accumulate a single value
        @accumulator->add(3);
        expect(@accumulator->get_value())->to_be(5);

      it should accumulate more than one value
        @accumulator->add(3);
        @accumulator->add(5);
        expect(@accumulator->get_value())->to_be(10);

```

Under the hood, this gets converted to PHP and then eval'd. Since PHP does not have the notion of blocks like Ruby does, a lot has to be done with anonymous functions that are passed the working scope as a parameter. If you don't mind working with that bit of extra verbosity, you can write your Sphec specs in straight PHP.

Here's what the above sphec shorthand gets converted into in actual PHP:

```
