From 7df515daf7543d78017b5a30676642351b7fb28d Mon Sep 17 00:00:00 2001 From: D4rk4ng3l Date: Wed, 12 Sep 2012 20:18:55 +0000 Subject: [PATCH] - Added PHPUnit TestCase for custom assertions. --- .../PHPUnit/Constraint/ArrayHasStructure.php | 89 +++++++++++++++++++ library/Msd/PHPUnit/TestCase.php | 44 +++++++++ 2 files changed, 133 insertions(+) create mode 100644 library/Msd/PHPUnit/Constraint/ArrayHasStructure.php create mode 100644 library/Msd/PHPUnit/TestCase.php diff --git a/library/Msd/PHPUnit/Constraint/ArrayHasStructure.php b/library/Msd/PHPUnit/Constraint/ArrayHasStructure.php new file mode 100644 index 0000000..0557fce --- /dev/null +++ b/library/Msd/PHPUnit/Constraint/ArrayHasStructure.php @@ -0,0 +1,89 @@ +_structure = $structure; + } + + /** + * Checks an array against the expected structure. + * + * @param mixed $other Array to check. + * @param null $structure Expected structure (used fpr recursion). + * + * @return bool|int + */ + protected function matches($other, $structure = null) + { + if ($structure === null) { + $structure = $this->_structure; + } + + $isValid = true; + foreach ($structure as $key => $value) { + if (is_array($value)) { + $isValid = $isValid && isset($other[$key]); + if (isset($other[$key])) { + $isValid = $isValid & $this->matches($other[$key], $value); + } + continue; + } + + $isValid = $isValid && isset($other[$value]); + } + + return $isValid; + } + + /** + * Returns a string representation of the object. + * + * @return string + */ + public function toString() + { + return 'has the structure' . PHPUnit_Util_Type::export($this->_structure); + } + + /** + * Returns the failure message. + * + * @param mixed $other Checked array. + * + * @return string + */ + protected function failureDescription($other) + { + return 'an array ' . $this->toString(); + } +} diff --git a/library/Msd/PHPUnit/TestCase.php b/library/Msd/PHPUnit/TestCase.php new file mode 100644 index 0000000..5728031 --- /dev/null +++ b/library/Msd/PHPUnit/TestCase.php @@ -0,0 +1,44 @@ +