tests
authorMichael Wallner <mike@php.net>
Fri, 8 Mar 2013 14:55:16 +0000 (15:55 +0100)
committerMichael Wallner <mike@php.net>
Fri, 8 Mar 2013 14:55:16 +0000 (15:55 +0100)
lib/ascertain/Assert.php [new file with mode: 0644]
lib/ascertain/Testable.php [new file with mode: 0644]
lib/ascertain/Validator.php [new file with mode: 0644]
src/Assert.php [deleted file]
src/Testable.php [deleted file]
src/Validator.php [deleted file]
tests/lib/ascertain/ValidatorTest.php [new file with mode: 0644]
tests/setup.inc [new file with mode: 0644]

diff --git a/lib/ascertain/Assert.php b/lib/ascertain/Assert.php
new file mode 100644 (file)
index 0000000..6759986
--- /dev/null
@@ -0,0 +1,341 @@
+<?php
+
+namespace ascertain;
+
+/**
+ * Be sure to always pass an error string as last argument to the assertion:
+ * <code>
+ *     $testable->assert()
+ *             ->that("name")->isLen(3, 40, "must be between 3 and 40 characters long")
+ *             ->that("email")->isEmail("is not valid");
+ * </code>
+ */
+class Assert
+{
+       /**
+        * Testable properties
+        * @var array
+        */
+       private $properties;
+       
+       /**
+        * The name of the currently inspected property
+        * @var string
+        */
+       private $inspectedProperty;
+       
+       /**
+        * Whether the argument to when() was true
+        * @var bool
+        */
+       private $inspectCondition = true;
+       
+       /**
+        * Whether assertions cause exceptions of this type
+        * @var string
+        */
+       private $exceptionClass;
+       
+       /**
+        * Failed assertions
+        * @var array
+        */
+       private $validationErrors = array();
+       
+       /**
+        * Succeeded assertions
+        * @var array
+        */
+       private $validationResults = array();
+       
+       /**
+        * @param \ascertain\Testable $testable
+        * @param string $exceptionClass
+        */
+       function __construct(Testable $testable, $exceptionClass = null) {
+               $this->properties = $testable->export();
+               $this->exceptionClass = $exceptionClass;
+       }
+       
+       /**
+        * @return int count of failed assertions
+        */
+       function hasErrors() {
+               return count($this->validationErrors);
+       }
+       
+       /**
+        * @return array of failed assertions
+        */
+       function getErrors() {
+               return $this->validationErrors;
+       }
+       
+       /**
+        * Reset assertions failures/results
+        * @return \ascertain\Assert
+        */
+       function resetErrors() {
+               $this->validationResults = array();
+               $this->validationErrors = array();
+               return $this;
+       }
+       
+       /**
+        * Set the currently inspected property
+        * @param string $property
+        * @return \ascertain\Assert
+        */
+       function that($property) {
+               $this->inspectCondition = true;
+               $this->inspectedProperty = $property;
+               return $this;
+       }
+       
+       /**
+        * The following assertions are only tested if the argument is true
+        * @param bool $condition
+        * @return \ascertain\Assert
+        */
+       function when($condition) {
+               $this->inspectCondition = $condition;
+               return $this;
+       }
+       
+       /**
+        * !strcmp($v, $c)
+        * @param mixed &$r
+        * @param string $v
+        * @param string $c
+        * @return bool
+        */
+       function test(&$r, $v, $c) {
+               $r = !strcmp($v, $c);
+               return $r;
+       }
+       
+       /**
+        * !strlen($v)
+        * @param mixed &$r
+        * @param string $v
+        * @return bool
+        */
+       function testNothing(&$r, $v) {
+               $r = !strlen($v);
+               return $r;
+       }
+       
+       /**
+        * is_numeric($v)
+        * @param mixed &$r
+        * @param string $v
+        * @return bool
+        */
+       function testNumeric(&$r, $v) {
+               $r =  is_numeric($v);
+               return $r;
+       }
+       
+       /**
+        * Test whether the argument is scalar
+        * @param mixed &$r
+        * @param mixed $v
+        * @param bool $strictNulls
+        * @return bool
+        */
+       function testScalar(&$r, $v, $strictNulls = false) {
+               $r = is_scalar($v) && (!$strictNulls || !isset($v));
+               return $r;
+       }
+       
+       /**
+        * Test wheter the argument constists only of printable characters
+        * @param mixed &$r
+        * @param string $v
+        * @return bool
+        */
+       function testPrintable(&$r, $v) {
+               return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v, $r) > 0;
+       }
+       
+       /**
+        * Test wheter the string length is between a certain range
+        * @param mixed &$r
+        * @param string $v
+        * @param int $min
+        * @param int $max
+        * @return bool
+        */
+       function testLen(&$r, $v, $min, $max = PHP_INT_MAX) {
+               return $this->testRange($r, function_exists("mb_strlen") ? mb_strlen($v) : strlen($v), $min, $max);
+       }
+       
+       /**
+        * Test wheter a value is between a certain range
+        * @param mixed &$r
+        * @param mixed $v
+        * @param mixed $min
+        * @param mixed $max
+        * @return bool
+        */
+       function testRange(&$r, $v, $min, $max) {
+               $r = (($v >= $min) && ($v <= $max));
+               return $r;
+       }
+
+       /**
+        * Test for a valid integer with FILTER_VALIDATE_INT
+        * @param mixed &$r
+        * @param mixed $v
+        * @param array $options
+        * @return bool
+        */
+       function testInteger(&$r, $v, array $options = null) {
+               $r = filter_var($v, FILTER_VALIDATE_INT, $options);
+               return $r !== false;
+       }
+       
+       /**
+        * Test for a valid boolean with FILTER_VALIDATE_BOOLEAN
+        * @param mixed &$r
+        * @param type $v
+        * @param array $options
+        * @return type
+        */
+       function testBoolean(&$r, $v, array $options = null) {
+               $options["flags"] = isset($options["flags"]) ? $options["flags"]|FILTER_NULL_ON_FAILURE : FILTER_NULL_ON_FAILURE;
+               $r = filter_var($v, FILTER_VALIDATE_BOOLEAN, $options);
+               return isset($r);
+       }
+       
+       /**
+        * Test for a valid float with FILTER_VALIDATE_FLOAT
+        * @param mixed &$r
+        * @param mixed $v
+        * @param array $options
+        * @return bool
+        */
+       function testFloat(&$r, $v, array $options = null) {
+               $r = filter_var($v, FILTER_VALIDATE_FLOAT, $options);
+               return $r !== false;
+       }
+       
+       /**
+        * Test for a valid URL with FILTER_VALIDATE_URL
+        * @param mixed &$r
+        * @param string $v
+        * @param array $options
+        * @return bool
+        */
+       function testUrl(&$r, $v, array $options = null) {
+               $r = filter_var($v, FILTER_VALIDATE_URL, $options);
+               return $r !== false;
+       }
+       
+       /**
+        * Test for a valid email address with FILTER_VALIDATE_EMAIL
+        * @param mixed &$r
+        * @param string $v
+        * @param array $options
+        * @return bool
+        */
+       function testEmail(&$r, $v, array $options = null) {
+               $r = filter_var($v, FILTER_VALIDATE_EMAIL, $options);
+               return $r !== false;
+       }
+       
+       /**
+        * Test for a valid IP address with FILTER_VALIDATE_IP
+        * @param mixed &$r
+        * @param string $v
+        * @param array $options
+        * @return bool
+        */
+       function testIp(&$r, $v, array $options = null) {
+               $r = filter_var($v, FILTER_VALIDATE_IP, $options);
+               return $r !== false;
+       }
+
+       /**
+        * Test whether a string contains another string or an array contains a key
+        * @param mixed &$r
+        * @param mixed $v haystack
+        * @param string $n needle
+        * @param bool $cs case-sensitive
+        * @return bool
+        */
+       function testContaining(&$r, $v, $n, $cs = true) {
+               if (is_array($v)) {
+                       if (!$cs) {
+                               $v = array_change_key_case($v);
+                               $n = strtolower($n);
+                       }
+                       if (array_key_exists($n, $v)) {
+                               $r = $v[$n];
+                               return true;
+                       }
+                       return $r = false;
+               } else {
+                       if ($cs) {
+                               $r = strstr($v, (string) $n);
+                       } else {
+                               $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
+        * @param string $v
+        * @param stirng $regex
+        * @param int $flags
+        * @return bool
+        */
+       function testMatching(&$r, $v, $regex, $flags = 0) {
+               return preg_match($regex, $v, $r, $flags) > 0;
+       }
+       
+       /**
+        * Semantic is(Not) wrapper to the assertions
+        * @param string $method
+        * @param array $args
+        * @return \ascertain\Assert
+        * @throws InvalidArgumentException (or rahter the configured exception)
+        */
+       function __call($method, $args) {
+               $match = null;
+               if ($this->inspectCondition && preg_match("/^is(Not(?!hing))?(.*)\$/i", $method, $match)) {
+                       list(, $not, $test) = $match;
+                       
+                       $result = null;
+                       $error = array_pop($args);
+                       array_unshift($args, $this->properties[$this->inspectedProperty]);
+                       array_unshift($args, null);
+                       $args[0] = &$result;
+                       $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/lib/ascertain/Testable.php b/lib/ascertain/Testable.php
new file mode 100644 (file)
index 0000000..869d468
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace ascertain;
+
+/**
+ * Implement this interface to use the \ascertain\Validator trait
+ */
+interface Testable {
+       /**
+        * @returns array of properties
+        */
+       function export();
+}
diff --git a/lib/ascertain/Validator.php b/lib/ascertain/Validator.php
new file mode 100644 (file)
index 0000000..68eb9a7
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+namespace ascertain;
+
+/**
+ * Use this trait in an \ascertain\Testable class
+ */
+trait Validator {
+       /**
+        * @param string $e Exception class name
+        * @return \ascertain\Assert
+        */
+       function assert($e = "\\InvalidArgumentException") {
+               return new Assert($this, $e);
+       }
+}
diff --git a/src/Assert.php b/src/Assert.php
deleted file mode 100644 (file)
index 7b5f644..0000000
+++ /dev/null
@@ -1,340 +0,0 @@
-<?php
-
-namespace ascertain;
-
-/**
- * Be sure to always pass an error string as last argument to the assertion:
- * <code>
- *     $testable->assert()
- *             ->that("name")->isLen(3, 40, "must be between 3 and 40 characters long")
- *             ->that("email")->isEmail("is not valid");
- * </code>
- */
-class Assert
-{
-       /**
-        * Testable properties
-        * @var array
-        */
-       private $properties;
-       
-       /**
-        * The name of the currently inspected property
-        * @var string
-        */
-       private $inspectedProperty;
-       
-       /**
-        * Whether the argument to when() was true
-        * @var bool
-        */
-       private $inspectCondition = true;
-       
-       /**
-        * Whether assertions cause exceptions of this type
-        * @var string
-        */
-       private $exceptionClass;
-       
-       /**
-        * Failed assertions
-        * @var array
-        */
-       private $validationErrors = array();
-       
-       /**
-        * Succeeded assertions
-        * @var array
-        */
-       private $validationResults = array();
-       
-       /**
-        * @param \ascertain\Testable $testable
-        * @param string $exceptionClass
-        */
-       function __construct(Testable $testable, $exceptionClass = null) {
-               $this->properties = $testable->export();
-               $this->exceptionClass = $exceptionClass;
-       }
-       
-       /**
-        * @return int count of failed assertions
-        */
-       function hasErrors() {
-               return count($this->validationErrors);
-       }
-       
-       /**
-        * @return array of failed assertions
-        */
-       function getErrors() {
-               return $this->validationErrors;
-       }
-       
-       /**
-        * Reset assertions failures/results
-        * @return \ascertain\Assert
-        */
-       function resetErrors() {
-               $this->validationResults = array();
-               $this->validationErrors = array();
-               return $this;
-       }
-       
-       /**
-        * Set the currently inspected property
-        * @param string $property
-        * @return \ascertain\Assert
-        */
-       function that($property) {
-               $this->inspectCondition = true;
-               $this->inspectedProperty = $property;
-               return $this;
-       }
-       
-       /**
-        * The following assertions are only tested if the argument is true
-        * @param bool $condition
-        * @return \ascertain\Assert
-        */
-       function when($condition) {
-               $this->inspectCondition = $condition;
-               return $this;
-       }
-       
-       /**
-        * !strcmp($v, $c)
-        * @param mixed &$r
-        * @param string $v
-        * @param string $c
-        * @return bool
-        */
-       function test(&$r, $v, $c) {
-               $r = !strcmp($v, $c);
-               return $r;
-       }
-       
-       /**
-        * !strlen($v)
-        * @param mixed &$r
-        * @param string $v
-        * @return bool
-        */
-       function testNothing(&$r, $v) {
-               $r = !strlen($v);
-               return $r;
-       }
-       
-       /**
-        * is_numeric($v)
-        * @param mixed &$r
-        * @param string $v
-        * @return bool
-        */
-       function testNumeric(&$r, $v) {
-               $r =  is_numeric($v);
-               return $r;
-       }
-       
-       /**
-        * Test whether the argument is scalar
-        * @param mixed &$r
-        * @param mixed $v
-        * @param bool $strictNulls
-        * @return bool
-        */
-       function testScalar(&$r, $v, $strictNulls = false) {
-               $r = is_scalar($v) && (!$strictNulls || !isset($v));
-               return $r;
-       }
-       
-       /**
-        * Test wheter the argument constists only of printable characters
-        * @param mixed &$r
-        * @param string $v
-        * @return bool
-        */
-       function testPrintable(&$r, $v) {
-               return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v, $r);
-       }
-       
-       /**
-        * Test wheter the string length is between a certain range
-        * @param mixed &$r
-        * @param string $v
-        * @param int $min
-        * @param int $max
-        * @return bool
-        */
-       function testLen(&$r, $v, $min, $max) {
-               return $this->testRange($r, function_exists("mb_strlen") ? mb_strlen($v) : strlen($v), $min, $max);
-       }
-       
-       /**
-        * Test wheter a value is between a certain range
-        * @param mixed &$r
-        * @param mixed $v
-        * @param mixed $min
-        * @param mixed $max
-        * @return bool
-        */
-       function testRange(&$r, $v, $min, $max) {
-               $r = $v >= $min && $v <= $max;
-               return $r;
-       }
-
-       /**
-        * Test for a valid integer with FILTER_VALIDATE_INT
-        * @param mixed &$r
-        * @param mixed $v
-        * @param array $options
-        * @return bool
-        */
-       function testInteger(&$r, $v, array $options = null) {
-               $r = filter_var($v, FILTER_VALIDATE_INT, $options);
-               return $r !== false;
-       }
-       
-       /**
-        * Test for a valid boolean with FILTER_VALIDATE_BOOLEAN
-        * @param mixed &$r
-        * @param type $v
-        * @param array $options
-        * @return type
-        */
-       function testBoolean(&$r, $v, array $options = null) {
-               $options["flags"] = isset($options["flags"]) ? $options["flags"]|FILTER_NULL_ON_FAILURE : FILTER_NULL_ON_FAILURE;
-               $r = filter_var($v, FILTER_VALIDATE_BOOLEAN, $options);
-               return isset($r);
-       }
-       
-       /**
-        * Test for a valid float with FILTER_VALIDATE_FLOAT
-        * @param mixed &$r
-        * @param mixed $v
-        * @param array $options
-        * @return bool
-        */
-       function testFloat(&$r, $v, array $options = null) {
-               $r = filter_var($v, FILTER_VALIDATE_FLOAT, $options);
-               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
-        * @param string $v
-        * @param array $options
-        * @return bool
-        */
-       function testUrl(&$r, $v, array $options = null) {
-               $r = filter_var($v, FILTER_VALIDATE_URL, $options);
-               return $r !== false;
-       }
-       
-       /**
-        * Test for a valid email address with FILTER_VALIDATE_EMAIL
-        * @param mixed &$r
-        * @param string $v
-        * @param array $options
-        * @return bool
-        */
-       function testEmail(&$r, $v, array $options = null) {
-               $r = filter_var($v, FILTER_VALIDATE_EMAIL, $options);
-               return $r !== false;
-       }
-       
-       /**
-        * Test for a valid IP address with FILTER_VALIDATE_IP
-        * @param mixed &$r
-        * @param string $v
-        * @param array $options
-        * @return bool
-        */
-       function testIp(&$r, $v, array $options = null) {
-               $r = filter_var($v, FILTER_VALIDATE_IP, $options);
-               return $r !== false;
-       }
-
-       /**
-        * Test whether a string contains another string
-        * @param mixed &$r
-        * @param type $v haystack
-        * @param type $n needle
-        * @param bool $cs case-sensitive
-        * @return bool
-        */
-       function testContains(&$r, $v, $n, $cs = true) {
-               if (is_array($v)) {
-                       if (!$cs) {
-                               $v = array_change_key_case($v);
-                               $n = strtolower($n);
-                       }
-                       if (array_key_exists($n, $v)) {
-                               $r = $v[$n];
-                               return true;
-                       }
-                       return $r = false;
-               } else {
-                       if ($cs) {
-                               $r = strstr($v, $n);
-                       } else {
-                               $r = stristr($v, $n);
-                       }
-                       return $r !== false;
-               }
-       }
-       
-       /**
-        * Thest if a regular expression matches
-        * @param mixed &$r
-        * @param string $v
-        * @param stirng $regex
-        * @param int $flags
-        * @return bool
-        */
-       function testMatching(&$r, $v, $regex, $flags = 0) {
-               return preg_match($regex, $v, $r, $flags) > 0;
-       }
-       
-       /**
-        * Semantic is(Not) wrapper to the assertions
-        * @param string $method
-        * @param array $args
-        * @return \ascertain\Assert
-        * @throws InvalidArgumentException (or rahter the configured exception)
-        */
-       function __call($method, $args) {
-               $match = null;
-               if ($this->inspectCondition && preg_match("/^is(Not)?(.*)\$/i", $method, $match)) {
-                       list(, $not, $test) = $match;
-                       
-                       $result = null;
-                       $error = array_pop($args);
-                       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];
-                               $this->validationErrors[$this->inspectedProperty][] = $error;
-                               if (($exception = $this->exceptionClass)) {
-                                       throw new $exception("$this->inspectedProperty $error");
-                               }
-                       }
-               }
-               return $this;
-       }
-       
-}
diff --git a/src/Testable.php b/src/Testable.php
deleted file mode 100644 (file)
index 869d468..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-namespace ascertain;
-
-/**
- * Implement this interface to use the \ascertain\Validator trait
- */
-interface Testable {
-       /**
-        * @returns array of properties
-        */
-       function export();
-}
diff --git a/src/Validator.php b/src/Validator.php
deleted file mode 100644 (file)
index 68eb9a7..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?php
-
-namespace ascertain;
-
-/**
- * Use this trait in an \ascertain\Testable class
- */
-trait Validator {
-       /**
-        * @param string $e Exception class name
-        * @return \ascertain\Assert
-        */
-       function assert($e = "\\InvalidArgumentException") {
-               return new Assert($this, $e);
-       }
-}
diff --git a/tests/lib/ascertain/ValidatorTest.php b/tests/lib/ascertain/ValidatorTest.php
new file mode 100644 (file)
index 0000000..5c70b0e
--- /dev/null
@@ -0,0 +1,302 @@
+<?php
+
+namespace ascertain;
+
+include __DIR__."/../../setup.inc";
+
+class Test implements Testable
+{
+       use Validator;
+       
+       public $good;
+       
+       function __construct($good) {
+               $this->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 (file)
index 0000000..8459513
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+spl_autoload_register(function ($c) {
+       if (substr($c, 0, 10) === "ascertain\\") require_once sprintf("%s/../lib/%s.php", __DIR__, strtr($c, "\\", "/"));
+});