PHPUnit Testing Framework

repository·main·Indexed 12 days ago

https://github.com/sebastianbergmann/phpunit

A programmer-oriented testing framework for PHP based on the xUnit architecture. It includes tools for unit testing, XML configuration management, code coverage caching, and a comprehensive CLI for test execution and filtering. Recent versions include updates to test run history recording and deprecations in versions 12.5.5 through 13.3.0.

Tokens
7.8K
Snippets
17
Records
41
Agent score
92%

What's inside PHPUnit

  1. Install PHPUnit via PHAR

    main

    You can install PHPUnit by downloading a PHP Archive (PHAR) file. This file bundles all required dependencies into a single file. Replace X.Y in the commands below with the specific version of PHPUnit you wish to install.

    $ wget https://phar.phpunit.de/phpunit-X.Y.phar
    
    $ php phpunit-X.Y.phar --version
  2. PHPUnit CLI Configuration Object

    main

    The PHPUnit\TextUI\CliArguments\Configuration class is an immutable object that represents the settings passed to PHPUnit via the Command Line Interface (CLI). It encapsulates all configuration options, including test selection, coverage settings, failure behaviors, and output formatting.

    When using this class programmatically, most properties are accessed via getter methods that follow a pattern: a has{PropertyName}() method to check if the option was provided, followed by a {propertyName}() method to retrieve the value. If the value is not present, the getter will throw an Exception.

  3. Update XML configuration for test ordering

    main

    The following executionOrder attribute values in your XML configuration are hard-deprecated as of version 13.2.0:

    Deprecated ValueReplacement
    executionOrder="duration"executionOrder="duration-ascending"
    executionOrder="size"executionOrder="size-ascending"
  4. Generate a new phpunit.xml configuration file

    main

    You can use the PHPUnit CLI to interactively generate a default phpunit.xml configuration file in your current working directory. The command will prompt you for several configuration values. If you press enter without providing input, the following default values are used:

    • Bootstrap script: vendor/autoload.php (relative to the project root)
    • Tests directory: tests (relative to the project root)
    • Source directory: src (relative to the project root)
    • Cache directory: .phpunit.cache (relative to the project root)

    Important: After generation, ensure that your cache directory (defaulting to .phpunit.cache) is excluded from your version control system (e.g., added to .gitignore).

    # Note: The specific CLI command name is not explicitly defined in this file, but it is invoked via the PHPUnit executable to start the interactive generation process.
    ./vendor/bin/phpunit --generate-configuration