Skip to content

Commit

Permalink
#13 added support for recursive mandatory options
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Oct 3, 2015
1 parent 8905c15 commit 175bba1
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file, in reverse

* [#8](https://github.com/sandrokeil/interop-config/issues/8): Introducing HasOptionalOptions Interface
* [#9](https://github.com/sandrokeil/interop-config/issues/9): Introducing HasDefaultOptions Interface
* [#13](https://github.com/sandrokeil/interop-config/issues/13): Support for recursive mandatory options check
* Benchmark suite

### Deprecated
Expand Down
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ To do so:
- Copy `TestConfig.php.dist` file to `TestConfig.php`
- Edit `TestConfig.php` to enable any specific functionality you want to test, as well as to provide test values to
utilize.

## RUNNING BENCHMARKS

To run benchmarks execute phpbench

```sh
$ ./vendor/bin/phpbench run --report=aggregate
```
59 changes: 59 additions & 0 deletions benchmark/HasMandatoryOptionsRecursiveContainerIdBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Sake
*
* @link http://github.com/sandrokeil/interop-config for the canonical source repository
* @copyright Copyright (c) 2015 Sandro Keil
* @license http://github.com/sandrokeil/interop-config/blob/master/LICENSE.txt New BSD License
*/

namespace InteropBench\Config;

use InteropTest\Config\TestAsset\ConnectionMandatoryRecursiveContainerIdConfiguration;

/**
* @beforeMethod classSetUp
*/
class HasMandatoryOptionsRecursiveContainerId
{
private $config;

/**
* @var ConnectionMandatoryRecursiveContainerIdConfiguration
*/
private $factory;

public function classSetUp()
{
$this->config = $this->getTestConfig();
$this->factory = new ConnectionMandatoryRecursiveContainerIdConfiguration();
}

/**
* Retrieve options
*
* @revs 1000
* @iterations 10
*/
public function options()
{
$this->factory->options($this->config);
}

/**
* Returns test config
*
* @return array
*/
private function getTestConfig()
{
// Load the user-defined test configuration file, if it exists; otherwise, load default
if (is_readable('test/TestConfig.php')) {
$testConfig = require 'test/testing.config.php';
} else {
$testConfig = require 'test/testing.config.php.dist';
}

return $testConfig;
}
}
46 changes: 35 additions & 11 deletions src/ConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,7 @@ public function options($config)
}
// check for mandatory options
if ($this instanceof HasMandatoryOptions) {
foreach ($this->mandatoryOptions() as $option) {
if (!isset($options[$option])) {
throw new Exception\MandatoryOptionNotFoundException(sprintf(
'Mandatory option "%s" was not set for configuration "' . "['%s']['%s']%s",
$option,
$vendorName,
$packageName,
$id ? '[' . $id . ']' : ''
));
}
}
$this->checkMandatoryOptions($this->mandatoryOptions(), $options);
}
// check for default options
if ($this instanceof HasDefaultOptions) {
Expand All @@ -92,6 +82,40 @@ public function options($config)
return $options;
}

/**
* Checks if a mandatory param is missing, supports recursion
*
* @param array|ArrayAccess $mandatoryOptions
* @param array|ArrayAccess $options
* @throws Exception\MandatoryOptionNotFoundException
*/
private function checkMandatoryOptions($mandatoryOptions, $options)
{
foreach ($mandatoryOptions as $key => $mandatoryOption) {
# if a string key exists it indicates a recursive check
if (isset($options[$key])) {
$this->checkMandatoryOptions($mandatoryOption, $options[$key]);
return;
}
if (isset($options[$mandatoryOption])) {
continue;
}
$id = null;

if ($this instanceof HasContainerId) {
$id = $this->containerId();
}

throw new Exception\MandatoryOptionNotFoundException(sprintf(
'Mandatory option "%s" was not set for configuration "' . "['%s']['%s']%s",
$mandatoryOption,
$this->vendorName(),
$this->packageName(),
$id ? '[' . $id . ']' : ''
));
}
}

/**
* @see \Interop\Config\HasConfig::vendorName
*/
Expand Down
22 changes: 22 additions & 0 deletions test/ConfigurationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use InteropTest\Config\TestAsset\ConnectionDefaultOptionsConfiguration;
use InteropTest\Config\TestAsset\ConnectionMandatoryConfiguration;
use InteropTest\Config\TestAsset\ConnectionMandatoryContainerIdConfiguration;
use InteropTest\Config\TestAsset\ConnectionMandatoryRecursiveContainerIdConfiguration;
use PHPUnit_Framework_TestCase as TestCase;

/**
Expand Down Expand Up @@ -230,6 +231,27 @@ public function testOptionsThrowsRuntimeExceptionIfMandatoryOptionIsMissing()
$stub->options($config);
}

/**
* Tests if options() throws a runtime exception if a recursive mandatory option is missing
*
* @covers \Interop\Config\ConfigurationTrait::options
*/
public function testOptionsThrowsRuntimeExceptionIfMandatoryOptionRecursiveIsMissing()
{
$stub = new ConnectionMandatoryRecursiveContainerIdConfiguration();

$this->setExpectedException(
'Interop\Config\Exception\MandatoryOptionNotFoundException',
'Mandatory option "dbname"'
);

$config = $this->getTestConfig();

unset($config['doctrine']['connection']['orm_default']['params']['dbname']);

$stub->options($config);
}

/**
* Returns test config
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Sake
*
* @link http://github.com/sandrokeil/interop-config for the canonical source repository
* @copyright Copyright (c) 2014 - 2015 Sandro Keil
* @license http://github.com/sandrokeil/interop-config/blob/master/LICENSE.txt New BSD License
*/

namespace InteropTest\Config\TestAsset;

use Interop\Config\ConfigurationTrait;
use Interop\Config\HasContainerId;
use Interop\Config\HasMandatoryOptions;
use Interop\Config\ObtainsOptions;

class ConnectionMandatoryRecursiveContainerIdConfiguration implements
ObtainsOptions,
HasContainerId,
HasMandatoryOptions
{
use ConfigurationTrait;

public function vendorName()
{
return 'doctrine';
}

public function packageName()
{
return 'connection';
}

public function containerId()
{
return 'orm_default';
}

public function mandatoryOptions()
{
return ['driverClass', 'params' => ['user', 'dbname']];
}
}

0 comments on commit 175bba1

Please sign in to comment.