7b5f6446ffbb56785d12b09b2d56c62244d7cd0f
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 * Succeeded assertions
49 private $validationResults = array();
52 * @param \ascertain\Testable $testable
53 * @param string $exceptionClass
55 function __construct(Testable
$testable, $exceptionClass = null) {
56 $this->properties
= $testable->export();
57 $this->exceptionClass
= $exceptionClass;
61 * @return int count of failed assertions
63 function hasErrors() {
64 return count($this->validationErrors
);
68 * @return array of failed assertions
70 function getErrors() {
71 return $this->validationErrors
;
75 * Reset assertions failures/results
76 * @return \ascertain\Assert
78 function resetErrors() {
79 $this->validationResults
= array();
80 $this->validationErrors
= array();
85 * Set the currently inspected property
86 * @param string $property
87 * @return \ascertain\Assert
89 function that($property) {
90 $this->inspectCondition
= true;
91 $this->inspectedProperty
= $property;
96 * The following assertions are only tested if the argument is true
97 * @param bool $condition
98 * @return \ascertain\Assert
100 function when($condition) {
101 $this->inspectCondition
= $condition;
112 function test(&$r, $v, $c) {
113 $r = !strcmp($v, $c);
123 function testNothing(&$r, $v) {
134 function testNumeric(&$r, $v) {
140 * Test whether the argument is scalar
143 * @param bool $strictNulls
146 function testScalar(&$r, $v, $strictNulls = false) {
147 $r = is_scalar($v) && (!$strictNulls ||
!isset($v));
152 * Test wheter the argument constists only of printable characters
157 function testPrintable(&$r, $v) {
158 return preg_match("/^[[:print:]\\P{Cc}]*\$/u", $v, $r);
162 * Test wheter the string length is between a certain range
169 function testLen(&$r, $v, $min, $max) {
170 return $this->testRange($r, function_exists("mb_strlen") ?
mb_strlen($v) : strlen($v), $min, $max);
174 * Test wheter a value is between a certain range
181 function testRange(&$r, $v, $min, $max) {
182 $r = $v >= $min && $v <= $max;
187 * Test for a valid integer with FILTER_VALIDATE_INT
190 * @param array $options
193 function testInteger(&$r, $v, array $options = null) {
194 $r = filter_var($v, FILTER_VALIDATE_INT
, $options);
199 * Test for a valid boolean with FILTER_VALIDATE_BOOLEAN
202 * @param array $options
205 function testBoolean(&$r, $v, array $options = null) {
206 $options["flags"] = isset($options["flags"]) ?
$options["flags"]|FILTER_NULL_ON_FAILURE
: FILTER_NULL_ON_FAILURE
;
207 $r = filter_var($v, FILTER_VALIDATE_BOOLEAN
, $options);
212 * Test for a valid float with FILTER_VALIDATE_FLOAT
215 * @param array $options
218 function testFloat(&$r, $v, array $options = null) {
219 $r = filter_var($v, FILTER_VALIDATE_FLOAT
, $options);
224 * Test for a valid regular expression with FILTER_VALIDATE_REGEXP
227 * @param array $options
230 function testRegexp(&$r, $v, array $options = null) {
231 $r = filter_var($v, FILTER_VALIDATE_REGEXP
, $options);
236 * Test for a valid URL with FILTER_VALIDATE_URL
239 * @param array $options
242 function testUrl(&$r, $v, array $options = null) {
243 $r = filter_var($v, FILTER_VALIDATE_URL
, $options);
248 * Test for a valid email address with FILTER_VALIDATE_EMAIL
251 * @param array $options
254 function testEmail(&$r, $v, array $options = null) {
255 $r = filter_var($v, FILTER_VALIDATE_EMAIL
, $options);
260 * Test for a valid IP address with FILTER_VALIDATE_IP
263 * @param array $options
266 function testIp(&$r, $v, array $options = null) {
267 $r = filter_var($v, FILTER_VALIDATE_IP
, $options);
272 * Test whether a string contains another string
274 * @param type $v haystack
275 * @param type $n needle
276 * @param bool $cs case-sensitive
279 function testContains(&$r, $v, $n, $cs = true) {
282 $v = array_change_key_case($v);
285 if (array_key_exists($n, $v)) {
294 $r = stristr($v, $n);
301 * Thest if a regular expression matches
304 * @param stirng $regex
308 function testMatching(&$r, $v, $regex, $flags = 0) {
309 return preg_match($regex, $v, $r, $flags) > 0;
313 * Semantic is(Not) wrapper to the assertions
314 * @param string $method
316 * @return \ascertain\Assert
317 * @throws InvalidArgumentException (or rahter the configured exception)
319 function __call($method, $args) {
321 if ($this->inspectCondition
&& preg_match("/^is(Not)?(.*)\$/i", $method, $match)) {
322 list(, $not, $test) = $match;
325 $error = array_pop($args);
326 array_unshift($args, $this->properties
[$this->inspectedProperty
]);
327 array_unshift($args, null);
329 if (call_user_func_array(array($this, "test$test"), $args) === !!$not) {
330 $this->validationResults
[$this->inspectedProperty
][] = $args[0];
331 $this->validationErrors
[$this->inspectedProperty
][] = $error;
332 if (($exception = $this->exceptionClass
)) {
333 throw new $exception("$this->inspectedProperty $error");