Sitemap

Available Assertions In PHPUnit Testing

2 min readMay 24, 2022

--

Today in this article we will be learning about some of the assertions used in PHPUnit testing….

Press enter or click to view image in full size

assertDatabaseHas()

First, you may use the assertDatabaseHas() helper to assert that data exists in the database matching a given set of criteria.

$this->assertDatabaseHas($table, array $data);

assertDatabaseMissing()

You can also use the assertDatabaseMissing() helper to assert that data does not exist in the database.

$this->assertDatabaseMissing($table, array $data);

assertSoftDeleted()

assertSoftDeleted() helper to assert that the given record has been soft deleted.

$this->assertSoftDeleted($table, array $data);

assertTrue()

Asserts that a condition is true. If it isn’t it throws a AssertionError without a message.

$this->assertTrue(boolean condition);

assertFalse()

Asserts that a condition is false. If it isn’t it throws an AssertionError without a message.

$this->assertFalse(boolean condition);

assertEquals()

Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message.

Note:- If expected and actual are null, they are considered equal.

Get Rasif Sahl’s stories in your inbox

Join Medium for free to get updates from this writer.

message - the identifying message for the AssertionError (null okay)

expected - expected value

actual - actual value

$this->assertEquals(String message, Object expected, Object actual);

assertArrayEquals()

Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message.

Note:- If expecteds and actuals are null, they are considered equal.

message - the identifying message for the AssertionError (null okay)

expecteds - Object array or array of arrays (multi-dimensional array) with expected values.

actuals - Object array or array of arrays (multi-dimensional array) with actual values

$this->assertArrayEquals(String message, Object[] expecteds, Object[] actuals);

assertNotNull()

Asserts that an object isn’t null. If it is an AssertionError is thrown.

object - Object to check or null

$this->assertNotNull(Object object);

assertNull()

Asserts that an object is null. If it isn’t an AssertionError is thrown.

object - Object to check or null

$this->assertNull(Object object);

These are some of the commonly used assertions I will be updating this blog post in the future too. So feel free to save this story in your feed.

Thanks for reading…

--

--

Rasif Sahl
Rasif Sahl

Written by Rasif Sahl

Learn — Grow — Focus — Improve