From 8a2d0850a57ce98a9dc7460fd455c37d66e8f119 Mon Sep 17 00:00:00 2001 From: C0DE8 Date: Sat, 13 May 2017 18:54:27 +0200 Subject: [PATCH] make test autoloader (bootstrap.php) more robust (only loads C0DE8 Namspace); optimize code coverage --- src/C0DE8/MatchMaker/KeyMatcher.php | 13 ++++--- src/C0DE8/MatchMaker/Manager.php | 28 +++++++-------- src/C0DE8/MatchMaker/Matcher.php | 3 +- src/C0DE8/MatchMaker/Rules.php | 4 ++- tests/C0DE8/MatchMaker/ManagerTest.php | 47 +++++++++++++++++--------- tests/bootstrap.php | 8 ++++- 6 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/C0DE8/MatchMaker/KeyMatcher.php b/src/C0DE8/MatchMaker/KeyMatcher.php index 751e5ac..172775a 100644 --- a/src/C0DE8/MatchMaker/KeyMatcher.php +++ b/src/C0DE8/MatchMaker/KeyMatcher.php @@ -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 @@ -92,6 +90,7 @@ public function get(array $pattern) : \Closure } } } + return true; }; } diff --git a/src/C0DE8/MatchMaker/Manager.php b/src/C0DE8/MatchMaker/Manager.php index 603f7fe..5f8faa4 100644 --- a/src/C0DE8/MatchMaker/Manager.php +++ b/src/C0DE8/MatchMaker/Manager.php @@ -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; } - } diff --git a/src/C0DE8/MatchMaker/Matcher.php b/src/C0DE8/MatchMaker/Matcher.php index 539e61e..3a4a2e3 100644 --- a/src/C0DE8/MatchMaker/Matcher.php +++ b/src/C0DE8/MatchMaker/Matcher.php @@ -10,7 +10,7 @@ class Matcher { /** - * @param $value + * @param $value * @param $pattern * @return bool * @throws \InvalidArgumentException @@ -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))) { diff --git a/src/C0DE8/MatchMaker/Rules.php b/src/C0DE8/MatchMaker/Rules.php index 93b8552..ced0daf 100644 --- a/src/C0DE8/MatchMaker/Rules.php +++ b/src/C0DE8/MatchMaker/Rules.php @@ -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]; } diff --git a/tests/C0DE8/MatchMaker/ManagerTest.php b/tests/C0DE8/MatchMaker/ManagerTest.php index 51b9262..ade47c9 100644 --- a/tests/C0DE8/MatchMaker/ManagerTest.php +++ b/tests/C0DE8/MatchMaker/ManagerTest.php @@ -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 */ @@ -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) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 77f9ad9..8792fb9 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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; });