Current Location: Home> Latest Articles> How to write unit tests for logic using array_column

How to write unit tests for logic using array_column

M66 2025-05-12

In PHP, array_column is a very useful function that extracts data from a specified column from a two-dimensional array. When you are developing, especially when dealing with complex array data, array_column may be used to simplify array operations. To ensure the reliability of the code, we usually need to write unit tests for logic using array_column .

This article will explain how to write effective unit tests for code using the array_column function and provide some practical examples.

1. Understand the array_column function

The main function of the array_column function is to extract all values ​​of a specified column from a multi-dimensional array and return a one-dimensional array containing all values ​​of the column.

grammar:

 array_column(array $input, mixed $column_key, mixed $index_key = null): array
  • $input : The input two-dimensional array.

  • $column_key : The key name of the column that needs to be extracted (can be the key of the index or associative array).

  • $index_key : optional, the index of the returned array. Can be another column in the array.

For example:

 $data = [
    ['id' => 1, 'name' => 'John', 'age' => 28],
    ['id' => 2, 'name' => 'Jane', 'age' => 22],
    ['id' => 3, 'name' => 'Doe', 'age' => 35],
];

$names = array_column($data, 'name');  // ['John', 'Jane', 'Doe']

2. Write unit tests

When writing unit tests, our goal is to make sure array_column works properly in all cases. This includes handling different data structures, empty arrays, and possible exceptions.

Writing tests using PHPUnit

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

 composer require --dev phpunit/phpunit

Then, you can start writing test classes. Suppose we have a function extractColumn which uses array_column to extract a column of the array.

 function extractColumn(array $data, $columnKey)
{
    return array_column($data, $columnKey);
}

We write unit tests for this function.

2.1 Basic Test

 use PHPUnit\Framework\TestCase;

class ArrayColumnTest extends TestCase
{
    public function testExtractColumn()
    {
        $data = [
            ['id' => 1, 'name' => 'John'],
            ['id' => 2, 'name' => 'Jane'],
            ['id' => 3, 'name' => 'Doe'],
        ];

        $result = extractColumn($data, 'name');
        
        // Verify that the return value is correct
        $this->assertEquals(['John', 'Jane', 'Doe'], $result);
    }
}

In this test, we make sure array_column extracts the name column correctly from the array.

2.2 Empty array test

We also need to test array_column 's processing of empty arrays.

 public function testExtractColumnWithEmptyArray()
{
    $data = [];
    
    $result = extractColumn($data, 'name');
    
    // Empty array should return empty array
    $this->assertEmpty($result);
}

2.3 Column does not exist

Sometimes, there may be no columns in the array that you requested. At this time, array_column will return an empty array.

 public function testExtractColumnWithNonExistentColumn()
{
    $data = [
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Jane'],
    ];
    
    $result = extractColumn($data, 'age');
    
    // 'age' The column does not exist,Empty array should be returned
    $this->assertEmpty($result);
}

2.4 Using index_key parameter

We can also test the third parameter of array_column , index_key , which specifies the index of the returned array.

 public function testExtractColumnWithIndexKey()
{
    $data = [
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Jane'],
        ['id' => 3, 'name' => 'Doe'],
    ];
    
    $result = array_column($data, 'name', 'id');
    
    // The returned array should be 'id' As key
    $this->assertEquals([1 => 'John', 2 => 'Jane', 3 => 'Doe'], $result);
}

3. Conclusion

When writing unit tests for logic using array_column , we should consider a variety of situations, including empty arrays, non-existent columns, and possible index key processing. Through these tests, we can ensure the stability and reliability of the code.

With the rich features provided by PHPUnit, we can easily write efficient and comprehensive unit tests for code using array_column to ensure it works properly in different situations.