An open API service indexing awesome lists of open source software.

https://github.com/peridot-php/peridot-dsl-example

Demonstrating a custom test DSL with Peridot
https://github.com/peridot-php/peridot-dsl-example

Last synced: about 1 year ago
JSON representation

Demonstrating a custom test DSL with Peridot

Awesome Lists containing this project

README

          

Peridot Custom DSL Example
==========================

This repo demonstrates creating a custom DSL for use with the [Peridot](https://github.com/peridot-php/peridot) testing framework for PHP.

##Peridot acceptance testing DSL

This DSL will allow us to write acceptance tests like so:

```php
cwd = getcwd();
});

Then('I should get this directory', function() {
if ($this->cwd != __DIR__) {
throw new \Exception("Should be current directory");
}
});

});

});

```

##The DSL file

Our DSL defines a small set of feature based functions. `Context` is the only singleton in the `Peridot` ecosystem,
and we use it to add suites and tests. You can browse it's documentation [here](http://peridot-php.github.io/docs/class-Peridot.Runner.Context.html).

```php
addSuite($description, $fn);
}

function Scenario(callable $fn)
{
Context::getInstance()->addSuite("Scenario:", $fn);
}

function Given($description, callable $fn)
{
$test = Context::getInstance()->addTest($description, $fn);
$test->getScope()->acceptanceDslTitle = "Given";
}

function When($description, callable $fn)
{
$test = Context::getInstance()->addTest($description, $fn);
$test->getScope()->acceptanceDslTitle = "When";
}

function Then($description, callable $fn)
{
$test = Context::getInstance()->addTest($description, $fn);
$test->getScope()->acceptanceDslTitle = "Then";
}
```

Notice the use of `Scope` to store additional information about our tests and our DSL.

##Configuring Peridot

We wire up our custom DSL via the Peridot configuration file.

```php
on('peridot.configure', function($config) {
$config->setDsl(__DIR__ . '/src/feature.dsl.php');

//this fixes the file pattern, you could just as easily use the -g option from the cli
$config->setGrep('*.feature.php');
});

//register a more appropriate reporter for our DSL
$emitter->on('peridot.reporters', function($input, $reporters) {
$reporters->register('feature', 'A feature reporter', 'Peridot\Example\FeatureReporter');
});
};
```

To complement our DSL, we have also extended the `SpecReporter`
with the `FeatureReporter`.

```php
handleGivenWhen($test);

$this->output->writeln(sprintf(
" %s%s %s",
$this->indent(),
$this->color('success', $title),
$this->color('muted', $test->getDescription())
));
}

/**
* Given and When don't represent true tests themselves, so we decrement
* the "passing" count that is reported for each one
*
* @param Test $test
* @return string
*/
protected function handleGivenWhen(Test $test)
{
$scope = $test->getScope();
$title = $scope->acceptanceDslTitle;
if (preg_match('/Given|When/', $title)) {
$this->passing--;
}
return $title;
}
}
```

##Running the features

```
$ vendor/bin/peridot features/ -r feature
```

![Peridot acceptance testing](https://raw.githubusercontent.com/peridot-php/peridot-dsl-example/master/output.png "Peridot acceptance testing")

##Note

This is just an example of creating a custom DSL for Peridot. It probably isn't the most robust solution in it's current state, but it is instead meant to demonstrate what Peridot is capable of.