Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/tkotosz/behat-extension-dri…
Browse files Browse the repository at this point in the history
…ver-locator
  • Loading branch information
Tibor Kotosz committed Nov 13, 2015
2 parents e6e6b19 + a295650 commit 95b80c7
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Composer executable files directory
/bin

# Installed Composer packages versions
/composer.lock
55 changes: 55 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
checks:
php: true

tools:
external_code_coverage: false
php_code_coverage: false
php_code_sniffer:
config: { standard: 'Zend' }
php_changetracking: false
php_sim: false
php_cs_fixer: true
php_mess_detector: true
php_pdepend: true
php_analyzer: true
sensiolabs_security_checker: true

filter:
paths:
- 'src/*'
excluded_paths:
- 'features/bootstrap/*'
- 'dev/*'
- 'var/*'
- 'lib/*'
- 'bin/*'
- 'vendor/*'

build:
environment:
php:
version: 5.4
ini:
'always_populate_raw_post_data': '-1'

tests:
override:
- bin/phpspec run --no-interaction --format=pretty
after:
- SCRUTINIZER_START_TIME=$(cat /tmp/build-start-time.txt) sh -c 'curl -sS https://gist.githubusercontent.com/tonypiper/fd3cf9a67b71d4e3928c/raw/152f1d873f98ff4256ca8bc3041443eae7c890b4/keenio-logger.php | php'

dependencies:
before:
- date -u +"%Y-%m-%dT%H:%M:%SZ" > /tmp/build-start-time.txt

override:
- { command: 'composer update --no-interaction --prefer-source', idle_timeout: 600 }

cache:
directories: [ vendor/, bin/ ]

build_failure_conditions:
- 'elements.rating(<= B).new.exists'
- 'issues.label("coding-style").new.exists'
- 'issues.new.exists'
- 'project.metric("scrutinizer.quality", < 9.00)'
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: php

sudo: false

php: [5.4, 5.5, 5.6, 7.0, hhvm]

matrix:
fast_finish: true
allow_failures:
- php: 7.0

cache:
directories:
- $HOME/.composer/cache/files

before_install:
- composer self-update

install:
- composer update --no-interaction --prefer-source

script:
- bin/phpspec run --no-interaction --format=pretty
85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
# behat-extension-driver-locator
Behat-ExtensionDriverLocator
=========================
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/tkotosz/behat-extension-driver-locator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/tkotosz/behat-extension-driver-locator/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/tkotosz/behat-extension-driver-locator/badges/build.png?b=master)](https://scrutinizer-ci.com/g/tkotosz/behat-extension-driver-locator/build-status/master)
[![Build Status](https://travis-ci.org/tkotosz/behat-extension-driver-locator.svg?branch=master)](https://travis-ci.org/tkotosz/behat-extension-driver-locator)

Behat-ExtensionDriverLocator helps you load external drivers/services (like image uploaders, output formatters, etc) dinamically.
The DriverLocator can find you a service in a preconfigured namespace by a given driverkey.
- It validates that the class implements the DriverInterface or your specific interface.
- It will call the configure method of the driver to get the config tree of the driver specific configurations
- It will validate the loaded config against the provided config tree.
- It will pass the valid config and the DI container to the load method of the driver in order to get a properly loaded service.
The package also provide a Driver Node Builder which can create the drivers node for your behat extension. (see usage below)

Installation
------------

Install by adding to your `composer.json`:

```bash
composer require --dev bex/behat-extension-driver-locator
```

Usage
-----

1. In your behat extension's configure method use the Driver Node Builder to build the drivers configuration node:

```php
$driverNodeBuilder = DriverNodeBuilder::getInstance($driverNamespace, $driverParent);
$driverNodeBuilder->buildDriverNodes($builder, $activeDriversNodeName, $driversCofigurationNodeName, $defaultDriverKeys);
```

where:
- the `$driverNamespace` is the namespace where the DriverNodeBuilder should look for the drivers when validating a given driver key
e.g.: `My\\Awesome\\BehatExtension\\Driver`
- the `$driverParent` is the parent class/interface which should be implemented by all driver
e.g.: `My\\Awesome\\BehatExtension\\Driver\\MyAwesomeDriverInterface`
(note that all driver need to implement the `Bex\Behat\ExtensionDriverLocator\DriverInterface`)
- the `$builder` is an `Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition` instance which you get in your behat extension's configure method as a parameter, the DriverNodeBuilder will add the drivers node to this builder.
- the `$activeDriversNodeName` is the name of the node where the user will be able to specify which driver should be used for your extension
e.g.: `active_my_awesome_drivers`
- the `driversCofigurationNodeName` is the name of the drivers node, the additional configuration of all driver will be under this node
e.g.: `my_awesome_drivers`
- the `$defaultDriverKeys` is the driverkey of the default driver(s) which will be used when the config is empty in the behat.yml
e.g.: `first_awesomeness`
- Note: The driverkey is always the lowercased and underlined version of the driver's classname.
e.g. FirstAwesomeness -> first_awesomeness
e.g. First -> first

With the example configurations a valid config would look like this:
```yml
default:
extensions:
My\\Awesome\\BehatExtension: ~
```
or
```yml
default:
extensions:
My\\Awesome\\BehatExtension:
active_my_awesome_drivers: first_awesomeness
my_awesome_drivers:
first_awesomeness:
# ... all driver specific configuration goes here ...
```

1. In your behat extension's load method use the Driver Locator to load the active driver(s):
Note that it will validate the driver specific configs automatically.

```php
$driverLocator = DriverLocator::getInstance($driverNamespace, $driverParent);
$drivers = $driverLocator->findDrivers($container, $activeDrivers, $driverConfigs);
```
where:
- the `$driverNamespace` is the namespace where the DriverLocator should look for the drivers
e.g.: `My\\Awesome\\BehatExtension\\Driver`
- the `$driverParent` is the parent class/interface which should be implemented by all driver
e.g.: `My\\Awesome\\BehatExtension\\Driver\\MyAwesomeDriverInterface`
- the `$container` is an `Symfony\Component\DependencyInjection\ContainerBuilder` instance which you get in your behat extension's load method as a parameter, the DriverLocator will pass this container to the load method of each driver
- the `$activeDrivers` are the active image drivers from the $config param which you get in the load method
e.g.: $config['active_my_awesome_drivers']
- the `$driverConfigs` are the driver specific configuration values from the $config param
e.g.: $config['my_awesome_drivers']
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
}
],
"require": {
"php" : "^5.4",
"php" : ">=5.4",
"behat/behat" : "^3.0.0"
},
"require-dev": {
"phpspec/phpspec" : "^2.3.0"
"phpspec/phpspec" : "2.4.0-alpha2"
},
"config": {
"bin-dir": "bin"
Expand Down
19 changes: 19 additions & 0 deletions spec/Bex/Behat/ExtensionDriverLocator/DriverLocatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace spec\Bex\Behat\ExtensionDriverLocator;

use Bex\Behat\ExtensionDriverLocator\DriverClassNameResolver;
use PhpSpec\ObjectBehavior;

class DriverLocatorSpec extends ObjectBehavior
{
function let(DriverClassNameResolver $classNameResolver)
{
$this->beConstructedWith($classNameResolver);
}

function it_is_initializable()
{
$this->shouldHaveType('Bex\Behat\ExtensionDriverLocator\DriverLocator');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Bex\Behat\ExtensionDriverLocator\DriverInterface;


class DriverClassValidator
{
/**
Expand All @@ -31,7 +30,7 @@ public function isValidDriverClass($className)
return false;
}

if (!is_subclass_of($className, DriverInterface::class)) {
if (!is_subclass_of($className, 'Bex\\Behat\\ExtensionDriverLocator\\DriverInterface')) {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Bex/Behat/ExtensionDriverLocator/DriverNodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function getInstance($namespace, $parent = '')
* @param ArrayNodeDefinition $builder
* @param string $activeDriversNodeName
* @param string $driversNodeName
* @param array $defaultActiveDrivers
* @param array | string $defaultActiveDrivers
*
* @return void
*/
Expand All @@ -46,6 +46,7 @@ public function buildDriverNodes(
$driversNodeName,
$defaultActiveDrivers
) {
$defaultActiveDrivers = (is_array($defaultActiveDrivers)) ? $defaultActiveDrivers : [$defaultActiveDrivers];
$builder
->children()
->arrayNode($activeDriversNodeName)
Expand Down

0 comments on commit 95b80c7

Please sign in to comment.