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
- Host: GitHub
- URL: https://github.com/peridot-php/peridot-dsl-example
- Owner: peridot-php
- Created: 2014-10-19T04:05:46.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-11-08T21:46:00.000Z (over 11 years ago)
- Last Synced: 2025-05-07T21:04:39.856Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 203 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
```

##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.