Current Location: Home> Latest Articles> How to test the behavior of array_change_key_case() in PHP unit test (PHPUnit)?

How to test the behavior of array_change_key_case() in PHP unit test (PHPUnit)?

M66 2025-04-25

array_change_key_case() is a common function in PHP that changes the case of all keys in an array. This function is very useful when you need to ensure the case consistency of array key names. However, in unit testing, we need to make sure that this function can be executed correctly in different situations. This article will explain how to unit test the array_change_key_case() function using PHPUnit.

1. A brief introduction to the array_change_key_case() function

The array_change_key_case() function accepts two parameters:

  • array : The array to be processed.

  • case (optional): Specifies the conversion method of upper case. The default value is CASE_LOWER , which means conversion to lower case; CASE_UPPER can be passed to upper case.

grammar :

 array_change_key_case(array $array, int $case = CASE_LOWER): array

Example :

 $array = [
    "FirstName" => "John",
    "LastName"  => "Doe"
];

print_r(array_change_key_case($array, CASE_LOWER));

Output:

 Array
(
    [firstname] => John
    [lastname] => Doe
)

2. Create PHPUnit test cases

Next, let's write a PHPUnit test case to ensure array_change_key_case() works properly. Suppose what we want to test is to convert all keys of the array to lowercase or uppercase.

First, make sure you have PHPUnit installed. If you haven't installed it yet, you can install it through Composer:

 composer require --dev phpunit/phpunit ^9

In PHPUnit testing, we need to create a test class and define some test methods in it. Here is a simple example:

 <?php

use PHPUnit\Framework\TestCase;

class ArrayChangeKeyCaseTest extends TestCase
{
    // Test convert array key names to lowercase
    public function testArrayChangeKeyCaseToLower()
    {
        $input = [
            "FirstName" => "John",
            "LastName"  => "Doe"
        ];

        $expected = [
            "firstname" => "John",
            "lastname"  => "Doe"
        ];

        $result = array_change_key_case($input, CASE_LOWER);

        $this->assertEquals($expected, $result);
    }

    // Test convert array key names to uppercase
    public function testArrayChangeKeyCaseToUpper()
    {
        $input = [
            "firstName" => "John",
            "lastName"  => "Doe"
        ];

        $expected = [
            "FIRSTNAME" => "John",
            "LASTNAME"  => "Doe"
        ];

        $result = array_change_key_case($input, CASE_UPPER);

        $this->assertEquals($expected, $result);
    }

    // Test empty array
    public function testArrayChangeKeyCaseWithEmptyArray()
    {
        $input = [];

        $result = array_change_key_case($input, CASE_UPPER);

        $this->assertEquals([], $result);
    }

    // Test with URL Array of keys
    public function testArrayChangeKeyCaseWithURL()
    {
        $input = [
            "UserURL" => "http://m66.net/profile",
            "ApiURL"  => "https://m66.net/api"
        ];

        $expected = [
            "userurl" => "http://m66.net/profile",
            "apiurl"  => "https://m66.net/api"
        ];

        $result = array_change_key_case($input, CASE_LOWER);

        $this->assertEquals($expected, $result);
    }
}

3. Explain test cases

  1. testArrayChangeKeyCaseToLower() : Tests to convert keys of an array to lowercase.

  2. testArrayChangeKeyCaseToUpper() : Tests to convert keys of an array to uppercase.

  3. testArrayChangeKeyCaseWithEmptyArray() : Tests behavior to empty arrays.

  4. testArrayChangeKeyCaseWithURL() : Tests an array with URL keys to ensure that the converted key name is lowercase and the URL's value remains unchanged.

4. Run PHPUnit tests

Save the test file in the tests directory and run the PHPUnit test using the following command:

 ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ArrayChangeKeyCaseTest

If everything works fine, you should see the output of all tests passing.

5. Summary

By using PHPUnit, we are able to ensure that the array_change_key_case() function works as expected in multiple scenarios. Whether it is a simple key name case conversion or a complex array containing URLs, correct unit testing can ensure that our needs are met in different situations. By testing different types of inputs, we can better understand the behavior of functions and ensure the robustness of the code.