-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |