From 4cabd94c389b4e01001340d27310ea050b925c69 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 8 Mar 2013 15:55:16 +0100 Subject: [PATCH] tests --- {src => lib/ascertain}/Assert.php | 49 +++-- {src => lib/ascertain}/Testable.php | 0 {src => lib/ascertain}/Validator.php | 0 tests/lib/ascertain/ValidatorTest.php | 302 ++++++++++++++++++++++++++ tests/setup.inc | 5 + 5 files changed, 332 insertions(+), 24 deletions(-) rename {src => lib/ascertain}/Assert.php (88%) rename {src => lib/ascertain}/Testable.php (100%) rename {src => lib/ascertain}/Validator.php (100%) create mode 100644 tests/lib/ascertain/ValidatorTest.php create mode 100644 tests/setup.inc diff --git a/src/Assert.php b/lib/ascertain/Assert.php similarity index 88% rename from src/Assert.php rename to lib/ascertain/Assert.php index 7b5f644..6759986 100644 --- a/src/Assert.php +++ b/lib/ascertain/Assert.php @@ -155,7 +155,7 @@ class Assert * @return bool */ function testPrintable(&$r, $v) { - return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v, $r); + return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v, $r) > 0; } /** @@ -166,7 +166,7 @@ class Assert * @param int $max * @return bool */ - function testLen(&$r, $v, $min, $max) { + function testLen(&$r, $v, $min, $max = PHP_INT_MAX) { return $this->testRange($r, function_exists("mb_strlen") ? mb_strlen($v) : strlen($v), $min, $max); } @@ -179,7 +179,7 @@ class Assert * @return bool */ function testRange(&$r, $v, $min, $max) { - $r = $v >= $min && $v <= $max; + $r = (($v >= $min) && ($v <= $max)); return $r; } @@ -220,18 +220,6 @@ class Assert return $r !== false; } - /** - * Test for a valid regular expression with FILTER_VALIDATE_REGEXP - * @param mixed &$r - * @param string $v - * @param array $options - * @return bool - */ - function testRegexp(&$r, $v, array $options = null) { - $r = filter_var($v, FILTER_VALIDATE_REGEXP, $options); - return $r !== false; - } - /** * Test for a valid URL with FILTER_VALIDATE_URL * @param mixed &$r @@ -269,14 +257,14 @@ class Assert } /** - * Test whether a string contains another string + * Test whether a string contains another string or an array contains a key * @param mixed &$r - * @param type $v haystack - * @param type $n needle + * @param mixed $v haystack + * @param string $n needle * @param bool $cs case-sensitive * @return bool */ - function testContains(&$r, $v, $n, $cs = true) { + function testContaining(&$r, $v, $n, $cs = true) { if (is_array($v)) { if (!$cs) { $v = array_change_key_case($v); @@ -289,14 +277,26 @@ class Assert return $r = false; } else { if ($cs) { - $r = strstr($v, $n); + $r = strstr($v, (string) $n); } else { - $r = stristr($v, $n); + $r = stristr($v, (string) $n); } return $r !== false; } } + /** + * Thest + * @param mixed &$r + * @param mixed $v + * @param array $a + * @param bool $strict + * @return bool + */ + function testAny(&$r, $v, $a, $strict = false) { + return $r = in_array($v, $a, $strict); + } + /** * Thest if a regular expression matches * @param mixed &$r @@ -318,7 +318,7 @@ class Assert */ function __call($method, $args) { $match = null; - if ($this->inspectCondition && preg_match("/^is(Not)?(.*)\$/i", $method, $match)) { + if ($this->inspectCondition && preg_match("/^is(Not(?!hing))?(.*)\$/i", $method, $match)) { list(, $not, $test) = $match; $result = null; @@ -326,13 +326,14 @@ class Assert array_unshift($args, $this->properties[$this->inspectedProperty]); array_unshift($args, null); $args[0] = &$result; - if (call_user_func_array(array($this, "test$test"), $args) === !!$not) { - $this->validationResults[$this->inspectedProperty][] = $args[0]; + $valid = call_user_func_array(array($this, "test$test"), $args); + if ($valid === !!$not) { $this->validationErrors[$this->inspectedProperty][] = $error; if (($exception = $this->exceptionClass)) { throw new $exception("$this->inspectedProperty $error"); } } + $this->validationResults[$this->inspectedProperty][] = $args[0]; } return $this; } diff --git a/src/Testable.php b/lib/ascertain/Testable.php similarity index 100% rename from src/Testable.php rename to lib/ascertain/Testable.php diff --git a/src/Validator.php b/lib/ascertain/Validator.php similarity index 100% rename from src/Validator.php rename to lib/ascertain/Validator.php diff --git a/tests/lib/ascertain/ValidatorTest.php b/tests/lib/ascertain/ValidatorTest.php new file mode 100644 index 0000000..5c70b0e --- /dev/null +++ b/tests/lib/ascertain/ValidatorTest.php @@ -0,0 +1,302 @@ +good = $good; + } + + function export() { + return array_map(function($v) { + return $v[(int)$this->good]; + }, array( + "any" => [0,1], + "boolean" => ["nay", true], + "containing" => ["im 1", "im 2"], + "containing2" => [[1], [1,2]], + "email" => ["@nirvana", "mike@php.net"], + "float" => ["foo", 123.123], + "integer" => [123.1, 123], + "ip" => ["543.234.123.000", "123.234.98.0"], + "len" => ["foo","foobar"], + "matching" => ["foo","foo"], + "nothing" => [0, ""], + "numeric" => ["123foo123", "123.123"], + "printable" => ["\r\n", "easy test"], + "scalar" => [null, 1], + "url" => ["this-::is a h#rd one", "http://because/probably?everything=valid#here"], + )); + } +} + +class ValidatorTest extends \PHPUnit_Framework_TestCase { + + /** + * @var \ascertain\Test + */ + protected $good; + + /** + * @var \ascertain\Test + */ + protected $bad; + + protected function setUp() { + $this->good = new Test(true); + $this->bad = new Test(false); + } + + public function testTestNothing() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("nothing")->isNothing("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("nothing")->isNotNothing("error"); + $this->assertEquals(1, $good->hasErrors()); + $this->assertSame(array("nothing"=>array("error")), $good->getErrors()); + $good->resetErrors(); + $this->assertSame(array(), $good->getErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("nothing")->isNotNothing("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("nothing")->isNothing("error"); + $this->assertEquals(1, $bad->hasErrors()); + $this->assertSame(array("nothing"=>array("error")), $bad->getErrors()); + $bad->resetErrors(); + $this->assertSame(array(), $bad->getErrors()); + } + + public function testTestNumeric() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("numeric")->isNumeric("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("numeric")->isNotNumeric("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("numeric")->isNotNumeric("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("numeric")->isNumeric("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestScalar() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("scalar")->isScalar("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("scalar")->isNotScalar("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("scalar")->isNotScalar("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("scalar")->isScalar("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestPrintable() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("printable")->isPrintable("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("printable")->isNotPrintable("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("printable")->isNotPrintable("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("printable")->isPrintable("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestLen() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("len")->isLen(4, "error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("len")->isNotLen(4, "error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("len")->isNotLen(4, "error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("len")->isLen(4, "error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestInteger() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("integer")->isInteger("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("integer")->isNotInteger("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("integer")->isNotInteger("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("integer")->isInteger("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestBoolean() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("boolean")->isBoolean("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("boolean")->isNotBoolean("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("boolean")->isNotBoolean("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("boolean")->isBoolean("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestFloat() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("float")->isFloat("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("float")->isNotFloat("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("float")->isNotFloat("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("float")->isFloat("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestUrl() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("url")->isUrl("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("url")->isNotUrl("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("url")->isNotUrl("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("url")->isUrl("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestEmail() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("email")->isEmail("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("email")->isNotEmail("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("email")->isNotEmail("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("email")->isEmail("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestIp() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("ip")->isIp("error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("ip")->isNotIp("error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("ip")->isNotIp("error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("ip")->isIp("error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestContaining() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("containing")->isContaining(2, "error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("containing")->isNotContaining(2, "error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("containing")->isNotContaining(2, "error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("containing")->isContaining(2, "error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestContaining2() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("containing2")->isContaining(1, "error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("containing2")->isNotContaining(1, "error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("containing2")->isNotContaining(1, "error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("containing2")->isContaining(1, "error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestMatching() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("matching")->isMatching("/^\w+\$/", "error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("matching")->isNotMatching("/^\w+\$/", "error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("matching")->isNotMatching("/^\$/", "error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("matching")->isMatching("/^\$/", "error"); + $this->assertEquals(1, $bad->hasErrors()); + } + + public function testTestAny() { + $good = $this->good->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $good); + $good->that("any")->isAny(array(1,2), "error"); + $this->assertEquals(0, $good->hasErrors()); + $good->that("any")->isNotAny(array(1,2), "error"); + $this->assertEquals(1, $good->hasErrors()); + + $bad = $this->bad->assert(false); + $this->assertInstanceOf("\\ascertain\\Assert", $bad); + $bad->that("any")->isNotAny(array(1,2), "error"); + $this->assertEquals(0, $bad->hasErrors()); + $bad->that("any")->isAny(array(1,2), "error"); + $this->assertEquals(1, $bad->hasErrors()); + } +} diff --git a/tests/setup.inc b/tests/setup.inc new file mode 100644 index 0000000..8459513 --- /dev/null +++ b/tests/setup.inc @@ -0,0 +1,5 @@ +