PHPUnit Testing Framework
repository·main·Indexed 12 days ago
https://github.com/sebastianbergmann/phpunitA 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.
What's inside PHPUnit
- PHPUnit is a programmer-oriented testing framework for PHP. It follows the xUnit architecture for unit testing frameworks.
Install PHPUnit via Composer
mainAlternatively, you can use Composer to download and install PHPUnit along with its dependencies. For detailed installation instructions using Composer, refer to the official PHPUnit documentation.Install PHPUnit via PHAR
mainYou can install PHPUnit by downloading a PHP Archive (PHAR) file. This file bundles all required dependencies into a single file. Replace
X.Yin 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 --versionManage PHPUnit Baselines
mainPHPUnit supports using baselines to manage existing issues (like deprecations or coverage gaps) so they don't cause new test runs to fail.
- Generate a baseline: Use
generateBaselineto create a baseline file. - Use a baseline: Use
useBaselineto specify a file to use as a baseline. - Ignore baseline: Use
ignoreBaselineto bypass the baseline during execution.
- Generate a baseline: Use
PHPUnit CLI Configuration Object
mainThe
PHPUnit\TextUI\CliArguments\Configurationclass 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 anException.Update XML configuration for test ordering
mainThe following
executionOrderattribute values in your XML configuration are hard-deprecated as of version 13.2.0:Deprecated Value Replacement executionOrder="duration"executionOrder="duration-ascending"executionOrder="size"executionOrder="size-ascending"Access CLI configuration properties
mainTo safely retrieve a configuration value from a
Configurationinstance, first check for its existence using the correspondinghas*method to avoid anException.Example pattern:
if ($configuration->hasBootstrap()) { $bootstrap = $configuration->bootstrap(); }Generate a new phpunit.xml configuration file
mainYou can use the PHPUnit CLI to interactively generate a default
phpunit.xmlconfiguration 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- Bootstrap script:
Update Configuration API for test run history
mainWhen extending PHPUnit, the following methods in
TextUI\Configuration\Configurationare hard-deprecated as of version 13.3.0:Deprecated Method Replacement cacheResult()recordTestRunHistory()testResultCacheFile()testRunHistoryFile()Fix non-positive arguments in atLeast()
mainAs of version 13.0.2, callingatLeast()with an argument that is not positive is hard-deprecated. Always use a positive argument.Fix usage of with*() without expects()
mainAs of version 13.0.2, using
with*()methods without an accompanyingexpects()call is hard-deprecated. To fix this, either:- Configure an expected invocation count using
expects(). - Use a test stub without the
with*()call.
- Configure an expected invocation count using
Replace deprecated mock object expectation methods
mainThe methodsid()andafter()for mock object expectations are soft-deprecated as of version 13.1.0.