highly extensible, highly enjoyable, BDD testing framework for PHP.

Scopes

Peridot is able to safely use $this inside of test closures because of scopes. Scopes allow us to place state on a test without conflicts.

Scopes also allow mixing in behavior to tests via a powerful concept called child scopes. Consider the following:

<?php //peridot.php
class WebDriverScope extends Scope
{
    protected $driver;
    protected $emitter;

    public function __construct(RemoteWebDriver $driver, EventEmitterInterface $emitter)
    {
        $this->driver = $driver;
        $this->emitter = $emitter;
        $this->emitter->on('runner.end', function() {
            $this->driver->quit();
        });
    }

    public function getPage($url)
    {
        $this->driver->get($url);
    }

    public function findElementById($id)
    {
        return $this->driver->findElement(\WebDriverBy::id($id));
    }
}

return function(EventEmitterInterface $emitter) {
    $driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());
    $driverScope = new WebDriverScope($driver, $emitter);

    $emitter->on('suite.start', function($test) use ($driverScope) {
        $test->getScope()->peridotAddChildScope($driverScope);
    });
}

By mixing the WebDriverScope in to our test's scopes, we have made the following possible in our tests:

<?php
describe('The home page', function() {
    it('should have a greeting', function() {
        $this->getPage('http://localhost:4000');
        $greeting = $this->findElementById('greeting');
        assert($greeting->getText() === "Hello", "should be Hello");
    });
});

Scopes are a powerful concept, and one that should be used liberally to simplify your tests. You can use them to easily include helpers and behavior for your tests, whether that is across the entire suite, or for a few select tests.