Skip to content

Commit

Permalink
make test autoloader (bootstrap.php) more robust (only loads C0DE8 Na…
Browse files Browse the repository at this point in the history
…mspace); optimize code coverage
  • Loading branch information
C0DE8 committed May 13, 2017
1 parent 226a9ed commit 8a2d085
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 41 deletions.
13 changes: 6 additions & 7 deletions src/C0DE8/MatchMaker/KeyMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ public function get(array $pattern) : \Closure
$keys[$patternKey] = (\count($range) === 1)
? [$range[0], $range[0]]
: [
$range[0] === ''
? 0
: $range[0],
$range[1] === ''
? PHP_INT_MAX
: $range[1]
$range[0] === '' ? 0 : $range[0],
$range[1] === '' ? PHP_INT_MAX : $range[1]
];

} else {
$keys[$patternKey] = $chars[$patternKey[0] === ':' ? '*' : '!'];
}

\array_push($keys[$patternKey], $patternValue, 0);
$keys[$patternKey][] = $patternValue; // index 2 (3rd element)
$keys[$patternKey][] = 0; // index 3 (4th element)
}

// return the recursive \Closure
Expand All @@ -92,6 +90,7 @@ public function get(array $pattern) : \Closure
}
}
}

return true;
};
}
Expand Down
28 changes: 14 additions & 14 deletions src/C0DE8/MatchMaker/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,45 @@ public function matchAgainst($value, $pattern) : bool
// get the key matcher closure
$keyMatcher = (new KeyMatcher())->get($pattern);

/** @var array $value **/
/** @var string $key **/
/** @var mixed $item **/
/** @var array $value **/
/** @var string $key **/
/** @var mixed $item **/
foreach ($value as $key => $item) {
if (!$keyMatcher($key, $item)) {
throw new KeyMatcherFailException(
'$keyMatcher FAIL by ' . $key . ' => '
. \var_export($item, true)
'$keyMatcher FAIL by key [' . $key . '] => item ['
. \var_export($item, true) . ' ]'
);
}
}

if (!$keyMatcher()) {
throw new KeyMatcherFailException(
'$keyMatcher() call FAIL (returned FALSE)'
'$keyMatcher() call FAIL (returned FALSE) '
. '{possibly wrong count}'
);
}

} elseif (!(new Matcher)->match($value, $pattern)) {

$valueType = gettype($value);
$valueType = \gettype($value);

if (is_array($value)) {
$value = json_encode($value);
if (\is_array($value)) {
$value = \json_encode($value);
}

if (is_object($value)) {
$value = get_class($value);
if (\is_object($value)) {
$value = \get_class($value);
}

throw new MatcherException(
'Matcher FAIL by value [' . $value . '] (' . $valueType . ') =>'
. ' [expected type: ' . var_export($pattern, true) . ']'
. ' [expected pattern/type: '
. \var_export($pattern, true) . ']'
);

}

return true;
}


}
3 changes: 1 addition & 2 deletions src/C0DE8/MatchMaker/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Matcher
{

/**
* @param $value
* @param $value
* @param $pattern
* @return bool
* @throws \InvalidArgumentException
Expand All @@ -28,7 +28,6 @@ public function match($value, $pattern)
$args = \explode(',', rtrim($args, ')'));
}


if (\is_callable((new Rules)->get($name))) {

if (!\call_user_func_array((new Rules)->get($name), \array_merge([$value], $args))) {
Expand Down
4 changes: 3 additions & 1 deletion src/C0DE8/MatchMaker/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public function get($key = null)
$key = 'any';
}
if (!isset($this->_data[$key])) {
throw new \InvalidArgumentException("Matcher $key not found");
throw new \InvalidArgumentException(
'[Rules]: rule "'.$key.'" not found'
);
}
return $this->_data[$key];
}
Expand Down
47 changes: 31 additions & 16 deletions tests/C0DE8/MatchMaker/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ public function testScalar()
$this->assertFalse($this->_instance->matchAgainst('string', ':integer'));
}

/**
* @expectedException \C0DE8\MatchMaker\Exception\KeyMatcherFailException
*/
public function testWrongCountRaisesKeyMatcherException()
{
$this->_instance->matchAgainst(
[
'foo' => 123
],
[
':string {2,3}' => ':int'
]
);
}

public function testAutoCountFromZeroToPhpMaxInt()
{
$this->assertTrue(
$this->_instance->matchAgainst(
[
'foo' => 123,
'bar' => 456,
'baz' => 789
],
[
':string {,}' => ':int'
]
)
);
}

/**
* @return array
*/
Expand Down Expand Up @@ -108,26 +139,10 @@ public function testArrayWithInvalidArguments()
);
}

/**
*
*/
public function XtestArrayWithInvalidKeys()
{
$this->_instance->matchAgainst(
[

],
[
':string' => ':bar'
]
);
}

/**
* @dataProvider arrayPatternDataProvider
* @param array $pattern
* @expectedException \C0DE8\MatchMaker\Exception\MatcherException
* @expectedException \C0DE8\MatchMaker\Exception\KeyMatcherFailException
*/
public function testArrayWithInvalidValues(array $pattern)
{
Expand Down
8 changes: 7 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

// micro autoloader
spl_autoload_register(function($class) {
require __DIR__ . '/../src/' . str_replace('\\', '/', $class) . '.php';

if (0 === strpos('C0DE8', $class)) {
require __DIR__ . '/../src/' . str_replace('\\', '/', $class) . '.php';
return true;
}

return false;
});

0 comments on commit 8a2d085

Please sign in to comment.