PHPUnit
PHPUnit 11 is the one which is still receiving support and bug fixes in 2025
The new[1] PHPUnit 10 is full of new features and changes. See the CHANGELOG and also the overview Release Announcement The videos below go into more depth.
Dec 16, 2023
Version 10 is probably the biggest update so far for PHPUnit. Almost all internals of PHP's most widely used testing framework and test runner have undergone long overdue changes to improve the developer experience of those who work on PHPUnit. This presentation, however, focuses on the improvements that were made for those that write and run tests with PHPUnit every day. Join Sebastian Bergmann, creator, and maintainer of PHPUnit, to learn everything you need to know about PHPUnit 10. You will experience new features, big and small, in action and see how they can support you to effectively and efficiently test your software.
phpday 2023 happened in Verona, Italy on 18th and 19th May 2023
Event based[edit]
PHPUnit 10 takes a whole new approach by using an event system - which replaces the old (bad) Listener and Hook system. You can read about it at https://github.com/sebastianbergmann/phpunit/issues/4676
Recommendations[edit]
- summary from Andreas Möller's Documenting the system under test (Mar 2023)
If you use phpunit/phpunit:9.0.0
and below, use @covers
annotations to document the system under test on the class level. If there is no clear system under test, use the @coversNothing annotation
annotation on the class level. Do not document the system under test on the method level, and do not bother with documenting individual methods - this functionality is not present in phpunit/phpunit:10.0.0
. Set the forceCoversAnnotation
attribute to true
in phpunit.xml
.
If you use phpunit/phpunit:10.0.0
and above, use CoversClass
attributes to document the system under test on the class level. If there is no clear system under test, use the CoversNothing
attribute on the class level. Set the requireCoverageMetadata
attribute to true
in phpunit.xml
.
If you use @covers
annotations in phpunit/phpunit
and friendsofphp/php-cs-fixer
, enable the php_unit_test_class_requires_covers
fixer.
Stash[edit]
I'm just writing this because my IDE crashed and I need to stash it.
Example test
<?php
namespace SMW\Tests\Exporter\Controller;
use MediaWiki\MediaWikiServices;
use PHPUnit\Framework\TestCase;
/**
* @covers \SMW\Exporter\SMWExportController
* @group semantic-mediawiki
*
* @license GPL-2.0-or-later
* @since 5.0-alpha
*
* @author freephile
*/
class SMW_ExportControllerTest extends TestCase {
public function testGetDBHandle() {
$mwVersion = MW_VERSION;
if (version_compare($mwVersion, '1.42', '>=')) {
$connectionProvider = $this->createMock(\Wikimedia\Rdbms\ConnectionProvider::class);
$replicaDatabase = $this->createMock(\Wikimedia\Rdbms\IDatabase::class);
$connectionProvider->method('getReplicaDatabase')->willReturn($replicaDatabase);
$mediaWikiServices = $this->createMock(MediaWikiServices::class);
$mediaWikiServices->method('getConnectionProvider')->willReturn($connectionProvider);
MediaWikiServices::setInstanceForTesting($mediaWikiServices);
$this->assertSame($replicaDatabase, SMWExportController::getDBHandle());
} else {
$dbLoadBalancer = $this->createMock(\Wikimedia\Rdbms\LoadBalancer::class);
$replicaDatabase = $this->createMock(\Wikimedia\Rdbms\IDatabase::class);
$dbLoadBalancer->method('getConnection')->with(DB_REPLICA)->willReturn($replicaDatabase);
$mediaWikiServices = $this->createMock(MediaWikiServices::class);
$mediaWikiServices->method('getDBLoadBalancer')->willReturn($dbLoadBalancer);
MediaWikiServices::setInstanceForTesting($mediaWikiServices);
$this->assertSame($replicaDatabase, SMWExportController::getDBHandle());
}
}
}
Subpages[edit]
References[edit]
- ↑ PHPUnit 10 was released in Feb 2023 PHPUnit 11 was released in Feb 2024 https://phpunit.de/supported-versions.html