I'm using PHPUnit to auto-test my app. I want to assert the result of a function call which can return a boolean or a string. My code looks like this:
$myExample = new MyExample();
$value = $myExample->getValue();
if ($value !== false) {
assertNotNull($value);
assertFalse(empty($value));
}
But is it also possible to check whether the method executes successfully? Is "assertTrue($value)" the correct way?
UPDATE: As per mtiziani's comment below, this answer applies for PHPUnit versions below 9.#
If you want to assert the data type of the value, the correct way would be:
$this->assertInternalType('[expected_data_type]', $value);
The [expected_data_type]
PHPUnit can validate can be any of these:
'array'
'boolean'
'bool'
'float'
'integer'
'int'
'null'
'numeric'
'object'
'resource'
'string'
'scalar'
'callable'
So, to assert that the returned value is a boolean, you would:
$this->assertInternalType('bool', $value);
UPDATE: Deprecated methods
Please use the following ones in case you want to check the data type:
assertIsArray()
assertIsBool()
assertIsFloat()
assertIsInt()
assertIsNumeric()
assertIsObject()
assertIsResource()
assertIsString()
assertIsScalar()
assertIsCallable()
assertIsIterable()
assertIsNotArray()
assertIsNotBool()
assertIsNotFloat()
assertIsNotInt()
assertIsNotNumeric()
assertIsNotObject()
assertIsNotResource()
assertIsNotString()
assertIsNotScalar()
assertIsNotCallable()
assertIsNotIterable()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With