Home >Backend Development >PHP Tutorial >How to unit test in PHP array pagination?

How to unit test in PHP array pagination?

WBOY
WBOYOriginal
2024-05-01 13:18:01867browse

PHP array paging unit testing guide includes: testing edge cases (empty array, invalid page number); testing whether the paging results are correct (first page, middle page, last page); testing whether the total page number calculation is correct (the number of elements is equal to The number of elements per page, the number of elements is greater than the number of elements per page).

How to unit test in PHP array pagination?

PHP Array Paging Unit Testing Guide

Preface

Array paging is a This is a common operation that is often encountered in development. In order to ensure the correctness of the paging functionality, it is very important to perform unit testing. This article will guide you on how to write unit tests for PHP array paging, including practical examples.

Practical case

Suppose we have an array $items, which needs to be paginated and display 10 elements on each page. Pagination function paginate() is as follows:

function paginate(array $items, int $page = 1, int $perPage = 10): array
{
    $totalPages = ceil(count($items) / $perPage);
    
    if ($page < 1) {
        $page = 1;
    } elseif ($page > $totalPages) {
        $page = $totalPages;
    }
    
    $offset = ($page - 1) * $perPage;
    
    return array_slice($items, $offset, $perPage);
}

Unit test

  1. Testing edge cases
  • Empty array: Ensure that the paging function returns an empty array when passed in an empty array.
$this->assertEquals([], paginate([]));
  • Invalid page number: Test the behavior of the paging function when negative page numbers and page numbers outside the total page number range are passed in.
$this->assertEquals([], paginate([], -1));
$this->assertEquals([], paginate([], 11, 10));
  1. Test whether the paging result is correct
  • First page: Make sure the paging function returns The first page element of the array.
$items = range(1, 100);
$this->assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], paginate($items));
  • Middle page: Check whether the paging function returns the element with the specified page number.
$this->assertEquals([51, 52, 53, 54, 55, 56, 57, 58, 59, 60], paginate($items, 6));
  • Last page: Ensure that the paging function returns the last page element of the array.
$this->assertEquals([91, 92, 93, 94, 95, 96, 97, 98, 99, 100], paginate($items, 10));
  1. Test whether the total page number calculation is correct
  • The number of elements is equal to the number of elements per page: When the number of elements is equal to the number of elements per page, the total page number should be 1.
$items = range(1, 10);
$this->assertEquals(1, paginate($items, 1, 10));
  • The number of elements is greater than the number of elements per page: When the number of elements is greater than the number of elements per page, the total page number should be rounded.
$items = range(1, 100);
$this->assertEquals(10, paginate($items, 1, 10));

The above is the detailed content of How to unit test in PHP array pagination?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn