30 Days of Automated Testing:Using PHPUnit【D06】

Common Assertion Functions (Part 3)

WilliamP
3 min readJan 15, 2023

Today we will introduce several database and array assertion functions. Like the previous article, the database assertion functions mentioned below are not built-in to PHPUnit but are extended by Laravel, so make sure you have correctly referenced use Tests\\TestCase. The last two array assertion functions are built-in to PHPUnit.

Before introducing the database assertion functions, please add the following <env> sub-tags within the <php> tag in the phpunit.xml file in the root directory:

<php>
<-- ...skip...->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<-- ...skip...->
</php>

These two settings will temporarily use the database environment variables set in the <env> in phpunit.xml when running PHPUnit tests, to avoid changing the actual database data. At the same time, in the implementation examples of the database assertion functions, the Illuminate\\Foundation\\Testing\\RefreshDatabase trait will be used, but today's text will only show how to use it. We will share more information in the future, so stay tuned.

Database

assertDatabaseCount

  • SignatureassertDatabaseCount($table, int $count, $connection = null)
  • Explanation: This function can verify that the specified table in the specified database connection contains the specified number of data. When no database connection is specified, the default database connection will be used.
  • Example
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DatabaseTest extends TestCase
{
use RefreshDatabase;

/**
* Example for assertDatabaseCount()
* @return void
*/
public function testAssertDatabaseCount()
{
// The assertion will pass the test
// if there is at least 1 record in the users table.
$this->assertDatabaseCount('users', 1);
}
}

assertDatabaseHas

  • SignatureassertDatabaseHas($table, array $data, $connection = null)
  • Explanation: This function can verify that, under the specified database connection, the specified table contains data with the specified data structure and values. Like the previous function, if no database connection is specified, the default database connection will be used.
  • Example
- **Example**:

```php
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DatabaseTest extends TestCase
{
use RefreshDatabase;

/**
* Example for assertDatabaseHas()
* @return void
*/
public function testAssertDatabaseHas()
{
// Assuming that
// the users table has at least one record,
// whose email field value is 'test@test.com',
// then the following Assertion will pass the test.
$this->assertDatabaseHas('users', [
'email' => 'test@test.com'
]);
}
}
```

assertDatabaseMissing

  • SignatureassertDatabaseMissing($table, array $data, $connection = null)
  • Explanation: This function can verify that, under the specified database connection, the specified table does not contain data with the specified data structure and values. Like the previous function, if no database connection is specified, the default database connection will be used.
  • Example
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DatabaseTest extends TestCase
{
use RefreshDatabase;

/**
* Example for assertDatabaseMissing()
* @return void
*/
public function testAssertDatabaseMissing()
{
// Assuming there is no data
// in the "users" table with an email field value of "test@test.com",
// the following assertion will pass the test.
$this->assertDatabaseMissing('users', [
'email' => 'test@test.com'
]);
}
}

Array

assertArrayHasKey

  • SignatureassertArrayHasKey($key, $array, string $message = '')
  • Explanation: This function is quite simple, it checks if the second parameter contains the key described in the first parameter.
  • Example
/**
* Example for assertArrayHasKey()
* @return void
*/
public function testAssertArrayHasKey()
{
$array = ['key' => 'value'];

$this->assertArrayHasKey($array, 'key');
}

assertJsonStringEqualsJsonString

  • SignatureassertJsonStringEqualsJsonString(string $expectedJson, string $actualJson, string $message = '')
  • Explanation: This function is useful when you need to validate complex nested arrays, but PHPUnit does not provide a corresponding nested array assertion function. In this case, you can use this function by first encoding the value to be validated with json_encode, passing it as the second argument, and passing the expected nested array structure and values as a JSON string in the first argument.
  • Example
/**
* Example for assertJsonStringEqualsJsonString()
* @return void
*/
public function testAssertJsonStringEqualsJsonString()
{
$array = [
'key' => 'value',
'object1' => [
'array' => [1, 2, 3,]
],
];

$this->assertJsonStringEqualsJsonString(
'{"object1":{"array":[1,2,3]},"key":"value"}',
json_encode($array)
);
}

In the next article, we will apply the Assertion functions that we have learned in the past few days to writing automated tests for APIs.

If you liked this article or found it helpful, feel free to give it some claps and follow the author!

Reference

Articles of This Series

--

--