I thought that the assert() assertion function was provided by unit test components like PHPUnit, but I found out after reading the manual that it was a built-in function in PHP. That is, we do simple tests in code without completely introducing the entire unit test component.

The assert() assert function


assert(1= =1);

assert(1= =2);
// assert.exception = 0, Warning: assert(): assert(1 == 2)
// Assert. exception = 1, Fatal Error: Uncaught AssertionError: Authentication fails
Copy the code

Obviously, the second piece of code fails assertion validation. At this point, PHP returns a warning or exception error. Why is it possible to have two wrong forms? When we set assert.exception in php.ini to off or 0, which turns off the ability to use this parameter, the program will still return a warning in PHP5, just like the comment in the code above. Also, try… Catch is also unable to catch exceptions. This parameter controls whether or not a valid exception object is thrown. If this parameter is left as the default, on or 1, an exception is thrown and the program aborts.

As you can see from the above code, the first argument to the assertion is an expression that requires an object of type bool. What if we pass a string or a number?

// Set assert.exception = 0 for multiple tests

assert("");
// Deprecated: assert(): Calling assert() with a string argument is deprecated
// Warning: assert(): Assertion " " failed

assert("1");
// Deprecated: assert(): Calling assert() with a string argument is deprecated

assert(0);
// Warning: assert(): assert(0) failed

assert(1);

assert("1 = = 2");
// Deprecated: assert(): Calling assert() with a string argument is deprecated
// Warning: assert(): Assertion "1==2" failed 
Copy the code

Obviously the expression for the first argument is cast, but the string type has an out-of-date reminder, indicating that the expression type passed to assert() is out of date. The current test version is 7.3. In the future, abort errors or exceptions may be reported directly. The main problem is that if the string passed is itself an expression, it will be judged based on the content of that expression, which can lead to ambiguity, just like the last piece of code. Of course, the use of outdated or not recommended, here is just to do an understanding.

Now let’s look at the other arguments to assert(). Its second argument is of two types, either a string to define the error message or an exception class to throw an exception.

assert(1= =1."Verification failed");

assert(1= =2."Verification failed");
// Warning: Assert (): Failed
Copy the code

If given a string, the warning message will display the content of the error message we defined. That’s pretty easy to understand.

// Note assert.exception sets different differences

assert(1= =1.new Exception("Verification failed"));

assert(1= =2.new Exception("Verification failed"));
// Assert. exception = 1, Fatal Error: Uncaught Exception: Authentication fails
// assert.exception = 0, Warning: Assert (): exception: validation failed
Copy the code

Of course, we can also give an exception class an assertion to throw an exception. By default, this exception is thrown to abort the program. As a normal exception throwing process, we can use try… Catch Catches an exception.

try{
    assert(1= =2.new Exception("Verification failed"));
}catch(Exception $e) {echo "Verification failed! :".$e->getMessage(), PHP_EOL;
}
// Validation failed! : The verification fails
Copy the code

Another parameter that can have an impact on the overall running of an assertion is the zend.assertions parameter in php.ini. It contains three values:

  • Generate and execute code, generally used in test environments
  • 0, generates code but passes by at run time
  • -1. No code is generated and is generally used in formal environments

This parameter is configurable for testing, and by default in php.ini it defaults to 1, which is the normal way to assert().

Assert_options () and the corresponding parameter configuration in php.ini

The assertion functionality in PHP also provides an assert_options() function that makes it easy to set and get some parameter configurations related to assertion capabilities. The assertion flags it can set include:

Mark | | | INI setting default value

  • | : – : | : – | – :

ASSERT_ACTIVE | assert. Active | 1 | enable assert () assert ASSERT_WARNING | assert. Warning | | 1 for each assertion failure to produce a PHP warning (warning) ASSERT_BAIL | assert. Bail | | 0 when an assertion fails to suspend execution ASSERT_QUIET_EVAL | assert. Quiet_eval | | 0 to disable the error_reporting when assertion expression evaluation ASSERT_CALLBACK | assert. Callback | | (NULL) assertion failure when the callback function is invoked

The meaning of these parameters is very easy to understand, you can test yourself. Let’s look at the last ASSERT_CALLBACK in action. The instructions are very clear: if the assertion fails, enter the callback function defined by this option.

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 1);
assert_options(ASSERT_BAIL, 1);

assert_options(ASSERT_CALLBACK, function($params){
    echo "====faild====", PHP_EOL;
    var_dump($params);
    echo "====faild====", PHP_EOL;
});

assert(1! =1);
// ====faild====
// string(105) "... /source/ learn how to use assertion functions in PHP.
// ====faild====
Copy the code

When the assertion fails, we enter the callback function, which simply prints the contents of the arguments passed to the callback function. As you can see, this callback is passing file information that cannot be asserted.

conclusion

Learn to master the use and configuration of assertion functions, you can learn PHPUnit in the future to lay a foundation, of course, itself this ability is not a lot of things, we remember good!

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…