PHPackages                             jlippitt/tusk - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. jlippitt/tusk

AbandonedArchivedLibrary[Testing &amp; Quality](/categories/testing)

jlippitt/tusk
=============

A PHP testing framework for elephants

0.2(12y ago)1522[1 issues](https://github.com/jlippitt/tusk/issues)MITPHPPHP &gt;=5.4.0

Since Mar 25Pushed 11y agoCompare

[ Source](https://github.com/jlippitt/tusk)[ Packagist](https://packagist.org/packages/jlippitt/tusk)[ Docs](https://github.com/jlippitt/tusk)[ RSS](/packages/jlippitt-tusk/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

tusk
====

[](#tusk)

A PHP testing framework for elephants

The Basics
----------

[](#the-basics)

**tusk** is a testing framework for PHP in the style of BDD (behaviour-driven development) frameworks such as Jasmine (JavaScript) or RSpec (Ruby).

Each **spec** is denoted by an `it` block in the test file. `describe` blocks are used to group multiple specs into a **suite**. `describe` blocks can be nested to as deep a level as required.

Inside each spec, `expect` is used to create *expectations* which must be satisfied in order for the test to pass.

The canonical example:

```
describe('Bowling', function() {
    describe('getScore()', function() {
        it('returns 0 for an all gutter game', function() {
            $bowling = new Bowling();

            for ($i = 0; $i < 10; ++$i) {
                $bowling->hit(0);
            }

            expect($bowling->getScore())->toBe(0);
        });
    });
});

```

Set Up and Tear Down
--------------------

[](#set-up-and-tear-down)

Often you will have code that you want to run before each spec or after each spec within a `describe` block. This can be achieved using `beforeEach` and `afterEach` blocks:

```
describe('My Test Suite', function() {
    beforeEach(function() {
        echo "This will run before each spec\n";
    });

    afterEach(function() {
        echo "This will run after each spec\n"
    });

    // ...
});

```

Hooks defined on an outer `describe` block will also run before/after each spec inside any nested `describe` blocks.

Variable Scoping
----------------

[](#variable-scoping)

PHP's closures (anonymous functions) have an odd quirk, in that if you want to capture any variables from the outer scope within the closure, you have to explicitly tell PHP with the `use` keyword.

Given our extensive use of anonymous functions for defining suites and specs, this could get quite tedious:

```
describe('Bowling', function() {
    $bowling = null;

    beforeEach(function() use (&$bowling) {
        $bowling = new Bowling();
    });

    describe('getScore()', function() use (&$bowling) {
        it('returns 0 for an all gutter game', function() use (&$bowling) {
            for ($i = 0; $i < 10; ++$i) {
                $bowling->hit(0);
            }

            expect($bowling->getScore())->toBe(0);
        });
    });
});

```

However, as of PHP 5.4, it's possible to bind the '$this' variable inside a closure to an arbitrary value. tusk makes extensive use of this and things work as you would expect:

```
describe('Bowling', function() {
    beforeEach(function() {
        $this->bowling = new Bowling();
    });

    describe('getScore()', function() {
        it('returns 0 for an all gutter game', function() {
            for ($i = 0; $i < 10; ++$i) {
                $this->bowling->hit(0);
            }

            expect($this->bowling->getScore())->toBe(0);
        });
    });
});

```

Expectations and Matchers
-------------------------

[](#expectations-and-matchers)

Expectations provide a number of built-in *matchers*:

- **toBe(expectedValue)** Compares values using the `===` operator
- **toEqual(expectedValue)** Compares values using the `==` operator
- **toBeGreaterThan(expectedValue)** Compares values using the `>` operator
- **toBeGreaterThanOrEqualTo(expectedValue)** Compares values using the `>=` operator
- **toBeLessThan(expectedValue)** Compares values using the `
