Skip to content

Commit ce8a915

Browse files
committed
Build/Test Tools: Add support for PHPUnit 6+.
This adds a compatibility shim for the new namespaced structure of PHPUnit and the removed `setExpectedException()` method. In addition, this updates the Travis config so PHPUnit 6.1 is used where appropriate. Props miyauchi, gitlost. Fixes #39822 git-svn-id: https://develop.svn.wordpress.org/trunk@40536 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 208a5dd commit ce8a915

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ before_script:
6767
# Install the specified version of PHPUnit depending on the PHP version:
6868
if [[ "$WP_TRAVISCI" == "travis:phpunit" ]]; then
6969
case "$TRAVIS_PHP_VERSION" in
70-
7.1|7.0|hhvm|nightly)
70+
7.1|7.0|nightly)
71+
echo "Using PHPUnit 6.1"
72+
composer global require "phpunit/phpunit=6.1.*"
73+
;;
74+
hhvm)
7175
echo "Using PHPUnit 5.7"
7276
composer global require "phpunit/phpunit=5.7.*"
7377
;;

tests/phpunit/includes/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
* Installs WordPress for running the tests and loads WordPress and the test libraries
44
*/
55

6+
/**
7+
* Compatibility with PHPUnit 6+
8+
*/
9+
if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
10+
require_once dirname( __FILE__ ) . '/phpunit6-compat.php';
11+
}
612

713
$config_file_path = dirname( dirname( __FILE__ ) );
814
if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
if ( class_exists( 'PHPUnit\Runner\Version' ) && version_compare( PHPUnit\Runner\Version::id(), '6.0', '>=' ) ) {
4+
5+
class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' );
6+
class_alias( 'PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception' );
7+
class_alias( 'PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException' );
8+
class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' );
9+
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );
10+
class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
11+
class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
12+
class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
13+
class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
14+
class_alias( 'PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState' );
15+
class_alias( 'PHPUnit\Util\Getopt', 'PHPUnit_Util_Getopt' );
16+
17+
class PHPUnit_Util_Test extends PHPUnit\Util\Test {
18+
19+
public static function getTickets( $className, $methodName ) {
20+
$annotations = self::parseTestMethodAnnotations( $className, $methodName );
21+
22+
$tickets = array();
23+
24+
if ( isset( $annotations['class']['ticket'] ) ) {
25+
$tickets = $annotations['class']['ticket'];
26+
}
27+
28+
if ( isset( $annotations['method']['ticket'] ) ) {
29+
$tickets = array_merge( $tickets, $annotations['method']['ticket'] );
30+
}
31+
32+
return array_unique( $tickets );
33+
}
34+
35+
}
36+
37+
}

tests/phpunit/includes/testcase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,27 @@ public function setExpectedIncorrectUsage( $doing_it_wrong ) {
415415
array_push( $this->expected_doing_it_wrong, $doing_it_wrong );
416416
}
417417

418+
/**
419+
* PHPUnit 6+ compatibility shim.
420+
*
421+
* @param mixed $exception
422+
* @param string $message
423+
* @param int|string $code
424+
*/
425+
public function setExpectedException( $exception, $message = '', $code = null ) {
426+
if ( is_callable( 'parent::setExpectedException' ) ) {
427+
parent::setExpectedException( $exception, $message, $code );
428+
} else {
429+
$this->expectException( $exception );
430+
if ( '' !== $message ) {
431+
$this->expectExceptionMessage( $message );
432+
}
433+
if ( null !== $code ) {
434+
$this->expectExceptionCode( $code );
435+
}
436+
}
437+
}
438+
418439
function deprecated_function_run( $function ) {
419440
if ( ! in_array( $function, $this->caught_deprecated ) )
420441
$this->caught_deprecated[] = $function;

0 commit comments

Comments
 (0)