Skip to content

Commit

Permalink
Added ArrayKeyMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
danhanly committed Feb 7, 2016
1 parent c641ebb commit 7fbba2f
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ $matcher = new \DanHanly\Scientist\UtilityMatchers\ObjectPropertyMatcher('proper
$matcher = new \DanHanly\Scientist\UtilityMatchers\ObjectPropertyMatcher(['propertyName', 'anotherPropertyName']);
```

### ArrayKeyMatcher

This allows you to match specific keys on arrays returned with the control and trial processes.

When initialising the matcher, you can configure it to either match against a single key (via string), or multiple keys (via array)

```php
// initialise matcher
$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher;
// define keys via string
$matcher->setKeys('keyName');
// or via array
$matcher->setKeys(['keyName', 'anotherKeyName']);
```

You can also set keys directly via the matcher constructor.

```php
// define key via string
$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher('keyName');
// or via array
$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher(['keyName', 'anotherKeyName']);
```

## Usage

Once you've initialised and configured your matcher, you can use it within your experiments.
Expand Down
Binary file added scientist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions src/ArrayKeyMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace DanHanly\Scientist\UtilityMatchers;

use Scientist\Matchers\Matcher;

class ArrayKeyMatcher implements Matcher
{
protected $keys = [];

/**
* ArrayKeyMatcher constructor.
* @param null|array $keys
*/
public function __construct($keys = null)
{
if (null !== $keys) {
$this->setKeys($keys);
}
}

/**
* Determine whether two values match.
*
* @param mixed $control
* @param mixed $trial
*
* @return boolean
*/
public function match($control, $trial)
{
$keys = $this->getKeys();
if (null === $keys) {
return false;
}

foreach ($keys as $key) {
if ($control[$key] !== $trial[$key]) {
return false;
}
}

return true;
}

/**
* @param array|string $keys
*
* @return void
*/
public function setKeys($keys)
{
if (true === is_string($keys)) {
$this->keys[] = $keys;
return;
}

if (true === is_array($keys)) {
foreach ($keys as $key) {
// Only add if the property name is a string
if (is_string($key)) {
$this->keys[] = $key;
}
}
return;
}
}

/**
* @return array|null
*/
public function getKeys()
{
if (true === empty($this->keys)) {
return null;
}

return $this->keys;
}
}
125 changes: 125 additions & 0 deletions tests/ArrayKeyMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace DanHanly\Scientist\UtilityMatchers\Tests;

use DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher;

class ArrayKeyMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testNoKeys()
{
$matcher = new ArrayKeyMatcher();
$this->assertFalse($matcher->match([], []));
}

public function testInvalidKeys()
{
$matcher = new ArrayKeyMatcher();
$matcher->setKeys(new \stdClass());
$this->assertFalse($matcher->match([], []));

$matcher->setKeys([new \stdClass()]);
$this->assertFalse($matcher->match([], []));
}

public function testInvalidKeysViaConstructor()
{
$matcher = new ArrayKeyMatcher(new \stdClass());
$this->assertFalse($matcher->match([], []));

$matcher = new ArrayKeyMatcher([new \stdClass()]);
$this->assertFalse($matcher->match([], []));
}

public function testStringProperty()
{
$control = [];
$control['key'] = 'test';

$trial = [];
$trial['key'] = 'test';

$matcher = new ArrayKeyMatcher();
$matcher->setKeys('key');

$this->assertTrue($matcher->match($control, $trial));

$trial['key'] = 'different';

$this->assertFalse($matcher->match($control, $trial));
}

public function testStringPropertyViaConstructor()
{
$control = [];
$control['key'] = 'test';

$trial = [];
$trial['key'] = 'test';

$matcher = new ArrayKeyMatcher('key');

$this->assertTrue($matcher->match($control, $trial));

$trial['key'] = 'different';

$this->assertFalse($matcher->match($control, $trial));
}

public function testArrayProperties()
{
$control = [];
$control['property'] = 'test';
$control['property2'] = 'test';

$trial = [];
$trial['property'] = 'test';
$trial['property2'] = 'test';

$matcher = new ArrayKeyMatcher();
$matcher->setKeys(
[
'property',
'property2'
]
);

$this->assertTrue($matcher->match($control, $trial));

$trial['property'] = 'different';

$this->assertFalse($matcher->match($control, $trial));

$trial['property2'] = 'different';

$this->assertFalse($matcher->match($control, $trial));
}

public function testArrayPropertiesViaConstructor()
{
$control = [];
$control['property'] = 'test';
$control['property2'] = 'test';

$trial = [];
$trial['property'] = 'test';
$trial['property2'] = 'test';

$matcher = new ArrayKeyMatcher(
[
'property',
'property2'
]
);

$this->assertTrue($matcher->match($control, $trial));

$trial['property'] = 'different';

$this->assertFalse($matcher->match($control, $trial));

$trial['property2'] = 'different';

$this->assertFalse($matcher->match($control, $trial));
}
}

0 comments on commit 7fbba2f

Please # to comment.