9630fa23f49fe4a12328f1ec4888f32152fdab13
6 * Be sure to always pass an error string as last argument to the assertion:
9 * ->that("name")->isLen(3, 40, "must be between 3 and 40 characters long")
10 * ->that("email")->isEmail("is not valid");
22 * The name of the currently inspected property
25 private $inspectedProperty;
28 * Whether the argument to when() was true
31 private $inspectCondition = true;
34 * Whether assertions cause exceptions of this type
37 private $exceptionClass;
43 private $validationErrors = array();
46 * @param \ascertain\Testable $testable
47 * @param string $exceptionClass
49 function __construct(Testable
$testable, $exceptionClass = null) {
50 $this->properties
= $testable->export();
51 $this->exceptionClass
= $exceptionClass;
55 * @return int count of failed assertions
57 function hasErrors() {
58 return count($this->validationErrors
);
62 * @return array of failed assertions
64 function getErrors() {
65 return $this->validationErrors
;
69 * Reset failed assertions
70 * @return \ascertain\Assert
72 function resetErrors() {
73 $this->validationErrors
= array();
78 * Set the currently inspected property
79 * @param string $property
80 * @return \ascertain\Assert
82 function that($property) {
83 $this->inspectCondition
= true;
84 $this->inspectedProperty
= $property;
89 * The following assertions are only tested if the argument is true
90 * @param bool $condition
91 * @return \ascertain\Assert
93 function when($condition) {
94 $this->inspectCondition
= $condition;
104 function test($v, $c) {
105 return !strcmp($v, $c);
113 function testNothing($v) {
122 function testNumeric($v) {
123 return is_numeric($v);
127 * Test wheter string representations of original and the int-cast equal
131 function testInteger($v) {
132 return ((string) $v) === ((string)(int) $v);
136 * Test whether the argument is scalar
138 * @param bool $strictNulls
141 function testScalar($v, $strictNulls = false) {
142 return is_scalar($v) && (!$strictNulls ||
!isset($v));
146 * Test wheter the argument constists only of printable characters
150 function testPrintable($v) {
151 return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v) > 0;
155 * Test wheter the string length is between a certain range
161 function testLen($v, $min, $max) {
162 return $this->testRange(function_exists("mb_strlen") ?
mb_strlen($v) : strlen($v), $min, $max);
166 * Test wheter a value is between a certain range
172 function testRange($v, $min, $max) {
173 return $v >= $min && $v <= $max;
177 * Test for a valid email address with FILTER_VALIDATE_EMAIL
179 * @param int $options
182 function testEmail($v, $options = null) {
183 return filter_var($v, FILTER_VALIDATE_EMAIL
, $options) !== false;
187 * Test for a valid URL with FILTER_VALIDATE_URL
189 * @param int $options
192 function testUrl($v, $options = null) {
193 return filter_var($v, FILTER_VALIDATE_URL
, $options) !== false;
197 * Test whether a string contains another string
198 * @param type $v haystack
199 * @param type $n needle
200 * @param bool $ci case-sensitive
203 function testContains($v, $n, $ci = true) {
204 return ($ci ?
strpos($v, $n) : stripos($v, $n)) !== false;
208 * Thest if a regular expression matches
210 * @param stirng $regex
214 function testRegex($v, $regex, $flags = 0) {
215 return preg_match($regex, $v, null, $flags);
219 * Semantic is(Not) wrapper to the assertions
220 * @param string $method
222 * @return \ascertain\Assert
223 * @throws InvalidArgumentException (or rahter the configured exception)
225 function __call($method, $args) {
226 if ($this->inspectCondition
&& preg_match("/^is(Not)?(.*)\$/i", $method, $match)) {
227 list(, $not, $test) = $match;
229 $error = array_pop($args);
230 array_unshift($args, $this->properties
[$this->inspectedProperty
]);
231 if (call_user_func_array(array($this, "test$test"), $args) == !!$not) {
232 if (($exception = $this->exceptionClass
)) {
233 throw new $exception("$this->inspectedProperty $error");
235 $this->validationErrors
[$this->inspectedProperty
][] = $error;