30 Days of Automated Testing:Using PHPUnit【D04】
In the previous article, we implemented our first test, and we learned about the first Assert function assertEquals. Today let’s take a look at some other commonly used Assert functions!
Generic Assertion Functions
- Signature:
assertEmpty(mixed $actual[, string $message = ''])
- Explanation:This function is used to check if the first input parameter
$actual
isEmpty
, refer to the PHP official function empty() for more information about common empty values in PHP. - Example:
/**
* Example for assertEmpty()
* @return void
*/
public function testAssertEmpty()
{
// 7 types null values
$emptyArray = [];
$emptyString = '';
$null = null;
$zeroInt = 0;
$zeroFloat = 0.0;
$zeroIntString = '0';
$false = FALSE;
// The following assertions will all pass
$this->assertEmpty($emptyArray);
$this->assertEmpty($emptyString);
$this->assertEmpty($null);
$this->assertEmpty($zeroInt);
$this->assertEmpty($zeroFloat);
$this->assertEmpty($zeroIntString);
$this->assertEmpty($false);
}
- It’s worth noting that ‘0’ is considered empty, but ‘0.0’ is not, so be careful with that.
- Signature:
assertEquals(mixed $expected, mixed $actual[, string $message = ''])
- Explanation:This function can be used to check if the value of the first parameter
$expected
is equal to the value of the second parameter$expected
. - Example:
/**
* Example for assertEquals()
* @return void
*/
public function testAssertEquals()
{
// Example values
$string = 'string';
$intOne = 1;
$intZero = 0;
$floatOne = 1.0;
$floatZero = 0.0;
$array1 = [];
$array2 = ['string'];
$stdClass1 = new \stdClass();
$stdClass1->a_field = 'a';
$stdClass2 = new \stdClass();
$stdClass2->a_field = 'a';
// The following assertions will all pas
$this->assertEquals('string', $string);
$this->assertEquals(1, $intOne);
$this->assertEquals(0, $intZero);
$this->assertEquals(1.0, $floatOne);
$this->assertEquals(0.0, $floatZero);
$this->assertEquals([], $array1);
$this->assertEquals(['string'], $array2);
$this->assertEquals($stdClass1, $stdClass2);
// The following are examples of cases where the data types are not the same, but they will be considered equal.
$this->assertEquals($intOne, $floatOne);
$this->assertEquals($intZero, $floatZero);
}
- It’s worth noting that
$intOne
will be considered equal to$floatOne
, and the same is true for$intZero
and$floatZero
.
- Signature:
assertSame(mixed $expected, mixed $actual[, string $message = ''])
- Explanation:This function can be used to check if the data type and value of the first parameter
$expected
is equal to the data type and value of the second parameter$actual
. - Example:
/**
* Example for assertSame()
* @return void
*/
public function testAssertSame()
{
// Example values
$string = 'string';
$intOne = 1;
$intZero = 0;
$floatOne = 1.0;
$floatZero = 0.0;
$array1 = [];
$array2 = ['string'];
// Example objects
$stdClass1 = new \stdClass();
$stdClass1->a_field = 'a';
$stdClass2 = new \stdClass();
$stdClass2->a_field = 'a';
$stdClass3 = $stdClass2;
// The following assertions will all pass
$this->assertSame('string', $string);
$this->assertSame(1, $intOne);
$this->assertSame(0, $intZero);
$this->assertSame(1.0, $floatOne);
$this->assertSame(0.0, $floatZero);
$this->assertSame([], $array1);
$this->assertSame(['string'], $array2);
// The following assertions will pass
// because both point to the same object reference location.
$this->assertSame($stdClass3, $stdClass2);
// The following are examples of cases
// where the data types are not the same,
// but they will be considered equal.
$this->assertSame($intOne, $floatOne);
$this->assertSame($intZero, $floatZero);
// The following assertions will fail
// because the object reference locations they point to
// are different.
$this->assertSame($stdClass1, $stdClass2);
}
- It’s worth noting that like
assertEquals()
,$intOne
will be considered equal to$floatOne
, and the same is true for$intZero
and$floatZero
. However, unlikeassertEquals()
, it will consider two class variables with the same attribute structure to bedifferent
.
- Signature:
assertTrue(bool $condition[, string $message = ''])
- Explanation:This function can be used to check if the first parameter is
TRUE
. - Example:
/**
* Example for assertTrue()
* @return void
*/
public function testAssertTrue()
{
$notEmptyString = 'a';
$notZeroInt = 1;
$notZeroFloat = 0.1;
$stdClass = new \stdClass();
$true = TRUE;
// The following assertions will all pass
$this->assertTrue(true);
$this->assertTrue($true);
$this->assertTrue(1 == 1);
$this->assertTrue(1 === 1);
$this->assertTrue($true);
// The following assertions will fail
$this->assertTrue($notEmptyString);
$this->assertTrue($notZeroInt);
$this->assertTrue($notZeroFloat);
$this->assertTrue($stdClass);
}
- It’s worth noting that the first parameter of
assertTrue
must be of the data typebool
.strings
or other data types will cause the test to fail.
- Signature:
assertFalse(bool $condition[, string $message = ''])
- Explanation:This function can be used to check if the first parameter is
FALSE
. - Example:
/**
* Example for assertFalse()
* @return void
*/
public function testAssertFalse()
{
$notEmptyString = 'a';
$notZeroInt = 1;
$zeroInt = 0;
$notZeroFloat = 0.1;
$zeroFloat = 0.0;
$stdClass = new \stdClass();
$false = FALSE;
// The following assertions will all pass
$this->assertFalse(false);
$this->assertFalse($false);
$this->assertFalse(1 > 1);
$this->assertFalse(1 < 1);
$this->assertFalse(1 != 1);
// The following assertions will fail
$this->assertFalse($notEmptyString);
$this->assertFalse($notZeroInt);
$this->assertFalse($zeroInt);
$this->assertFalse($notZeroFloat);
$this->assertFalse($zeroFloat);
$this->assertFalse($stdClass);
}
- It’s worth noting that the first parameter of
assertTrue
must be of the data typebool
.strings
or other data types will cause the test to fail.
- Signature:
assertIsArray($actual[, $message = ''])
- Explanation:This function can be used to check if the first parameter is an
Array
data type. - Example:
/**
* Example for assertIsArraye()
* @return void
*/
public function testAssertIsArray()
{
$array1 = [];
$array2 = ['2'];
$array3 = ['key' => 'value'];
$collection = collect([]);
// The following assertions will all pass
$this->assertIsArray($array1);
$this->assertIsArray($array2);
$this->assertIsArray($array3);
$this->assertIsArray($collection->toArray());
// The following assertions will fail
$this->assertIsArray($collection);
}
- It’s worth noting that when you want to check if an object may be a
Collection
, directly inputting it intoassertIsArray()
will cause the check to fail.
- Signature:
assertIsInt($actual[, $message = ''])
- Explanation:This function can be used to check if the first parameter is an
Int
data type. - Example:
/**
* Example for assertIsInt()
* @return void
*/
public function testAssertIsInt()
{
$int1 = 0;
$int2 = 1;
$float1 = 0.0;
$float2 = 1.0;
$string1 = '0';
$string2 = '1';
// The following assertions will all pass
$this->assertIsInt($int1);
$this->assertIsInt($int2);
// The following assertions will fail
$this->assertIsInt($float1);
$this->assertIsInt($float2);
$this->assertIsInt($string1);
$this->assertIsInt($string2);
}
- Signature:
assertIsFloat($actual[, $message = ''])
- Explanation:This function can be used to check if the first parameter is an
Float
data type. - Example:
/**
* Example for assertIsFloat()
* @return void
*/
public function testAssertIsFloat()
{
$float1 = 0.0;
$float2 = 1.0;
$float3 = -1.0;
// The following assertions will all pass
$this->assertIsInt($float1);
$this->assertIsInt($float2);
$this->assertIsInt($float3);
}
The above are a few generic Assertion functions. Readers who are interested can refer to the examples to practice.
The next article will introduce Assertion functions related to Cookies, Session, and HTTP.
If you liked this article or found it helpful, feel free to give it some claps and follow the author!