Skip to content

Commit 57e26e8

Browse files
authored
Clear out PHPUnit_Upgrade_Warning (#835)
* Clear out PHPUnit_Upgrade_Warning * Drop more phpunit 9 things * Deprecate the method
1 parent df322aa commit 57e26e8

File tree

4 files changed

+12
-148
lines changed

4 files changed

+12
-148
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## Unreleased
8+
## v1.14.1
99

1010
### Added
1111

@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Added custom cache key support to the HTTP Client cache middleware and flexible cache
1515
middleware.
1616

17+
### Changed
18+
19+
- Dropped more code support for PHPUnit versions prior to 10.
20+
1721
## v1.14.0
1822

1923
### Added

src/mantle/testing/class-installation-manager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ function () use ( $enable ): void {
294294
* Install the Mantle Testing Framework.
295295
*/
296296
public function install(): static {
297-
$this->warn_if_phpunit_10_or_higher();
298-
299297
require_once __DIR__ . '/core-polyfill.php';
300298

301299
if ( Utils::is_debug_mode() ) {

src/mantle/testing/concerns/trait-phpunit-upgrade-warning.php

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -7,129 +7,21 @@
77

88
namespace Mantle\Testing\Concerns;
99

10-
use PHPUnit\Runner\Version;
11-
use PHPUnit\TextUI\Configuration\Registry;
12-
use PHPUnit\TextUI\Configuration\TestSuite;
13-
14-
use function Mantle\Support\Helpers\collect;
15-
use function Termwind\render;
10+
use Deprecated;
1611

1712
/**
18-
* Provide a warning to users who are attempting to run PHPUnit 10+ against an
19-
* incompatible codebase.
20-
*
21-
* PHPUnit 10+ requires code bases to use PSR-4 standards for test classes. We
22-
* also need to be careful of any possible changes to the PHPUnit classes we're
23-
* referencing because they are not covered by the backward compatibility promise
24-
* for PHPUnit.
13+
* Previously used to warn about PHPUnit 10+ upgrade. No longer does anything and will be removed with Mantle 2.0.
2514
*
26-
* @mixin \Mantle\Testing\Installation_Manager
15+
* @deprecated 1.14.1
2716
*/
2817
trait PHPUnit_Upgrade_Warning {
29-
/**
30-
* Whether to silence the PHPUnit 10+ warning.
31-
*/
32-
public bool $silence_phpunit_warning = false;
33-
34-
/**
35-
* Check if the current codebase is running PHPUnit 10 or higher.
36-
*/
37-
protected function is_running_phpunit_10_or_higher(): bool {
38-
// Prevent conflicts if the internal class API changes.
39-
if ( ! class_exists( Version::class ) || ! method_exists( Version::class, 'id' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
40-
return false;
41-
}
42-
43-
return version_compare( '10.0.0', Version::id(), '<=' );
44-
}
45-
46-
/**
47-
* Warn the user if they're running PHPUnit 10+ against an incompatible test suite.
48-
*
49-
* @param TestSuite $test_suite Test suite configuration.
50-
*/
51-
protected function warn_if_test_suite_contains_legacy_test_cases( TestSuite $test_suite ): void {
52-
if ( $this->silence_phpunit_warning ) {
53-
return;
54-
}
55-
56-
// Prevent conflicts if the internal class API changes.
57-
if ( ! method_exists( $test_suite, 'directories' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
58-
return;
59-
}
60-
61-
// Check if the test suite contains directories with a 'test-' prefix. That
62-
// is a clear sign of a legacy test suite.
63-
$legacy_test_cases = collect( $test_suite->directories() )
64-
->filter(
65-
fn ( $directory ) => method_exists( $directory, 'prefix' ) && 'test-' === $directory->prefix(), // @phpstan-ignore-line function.alreadyNarrowedType
66-
);
67-
68-
if ( $legacy_test_cases->is_empty() ) {
69-
return;
70-
}
71-
72-
render(
73-
<<<'HTML'
74-
<div class="space-y-1">
75-
<div class="bg-red-300 text-red-700 pt-2 py-2">
76-
<strong>🚨 Warning:</strong> You are running PHPUnit 10+ against a test suite that contains legacy test cases.
77-
</div>
78-
<div>
79-
<span class="text-blue-300">Mantle Testing Framework 1.0</span> includes <span class="text-yellow-500 font-bold">✨ PHPUnit 11 ✨</span> which requires test cases to follow PSR-4 standards.
80-
<br />
81-
For example, that would be <span class="italic">tests/Feature/MyExampleTest.php</span> instead of <span class="italic">tests/feature/test-my-example.php</span>.
82-
<br />
83-
Fear not, you don't have to upgrade to PHPUnit 11 right away. You can still keep your test suite as-is and use PHPUnit 9 by running the following code:
84-
85-
<div class="ml-2 mt-1 italic">composer require --dev phpunit/phpunit:^9 nunomaduro/collision:^6 -W</div>
86-
</div>
87-
<div>
88-
For more information and tips on how to upgrade your codebase to PHPUnit 11, please refer to the 1.0 Release Changelog:
89-
90-
<div class="ml-2 my-1 italic">
91-
https://github.com/alleyinteractive/mantle-framework/blob/1.x/CHANGELOG.md#phpunit-10-migration
92-
</div>
93-
</div>
94-
</div>
95-
HTML,
96-
);
97-
98-
$this->silence_phpunit_warning();
99-
}
100-
101-
/**
102-
* Warn the user if they're running PHPUnit 10+ against an incompatible codebase.
103-
*/
104-
protected function warn_if_phpunit_10_or_higher(): void {
105-
// Bail if this is running via Pest.
106-
if ( isset( $_SERVER['PHP_SELF'] ) && str_contains( (string) $_SERVER['PHP_SELF'], 'pest' ) ) { // phpcs:ignore WordPress.Security
107-
return;
108-
}
109-
110-
if ( $this->silence_phpunit_warning || ! $this->is_running_phpunit_10_or_higher() ) {
111-
return;
112-
}
113-
114-
if ( ! class_exists( Registry::class ) || ! method_exists( Registry::class, 'get' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType{
115-
return;
116-
}
117-
118-
$registry = Registry::get();
119-
120-
foreach ( $registry->testSuite()->asArray() as $test_suite ) {
121-
if ( $test_suite instanceof TestSuite ) { // @phpstan-ignore-line true
122-
$this->warn_if_test_suite_contains_legacy_test_cases( $test_suite );
123-
}
124-
}
125-
}
126-
12718
/**
12819
* Silence the PHPUnit 10+ warning.
20+
*
21+
* No longer does anything, kept for backward compatibility.
12922
*/
23+
#[Deprecated( 'This method no longer does anything and will be removed with Mantle 2.0.' )]
13024
public function silence_phpunit_warning(): static {
131-
$this->silence_phpunit_warning = true;
132-
13325
return $this;
13426
}
13527
}

src/mantle/testing/concerns/trait-reads-annotations.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ trait Reads_Annotations {
2626
* Read docblock annotations for the current test case and method.
2727
*/
2828
public function get_annotations_for_method(): array {
29-
// PHPUnit 9.4 and below method.
30-
if ( method_exists( $this, 'getAnnotations' ) ) {
31-
return $this->getAnnotations();
32-
}
33-
34-
// Use the PHPUnit ^9.5 method if available.
35-
if ( method_exists( Test::class, 'parseTestMethodAnnotations' ) ) { // @phpstan-ignore-line
36-
return Test::parseTestMethodAnnotations(
37-
static::class,
38-
$this->getName(), // @phpstan-ignore-line
39-
);
40-
}
41-
4229
// Use the PHPUnit 10.x method if available.
4330
if ( class_exists( Registry::class ) && class_exists( DocBlock::class ) ) {
4431
$registry = Registry::getInstance();
@@ -51,26 +38,12 @@ public function get_annotations_for_method(): array {
5138

5239
// If we are using PHPUnit 12.0.0 or greater, we can bail because
5340
// annotations are no longer supported. Attributes must be used instead.
54-
if ( class_exists( Version::class ) && version_compare( Version::id(), '12.0.0', '>=' ) ) {
55-
return [];
56-
}
57-
58-
// Throw a warning if we can't read annotations.
59-
trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
60-
'Unable to read annotations for test method. Please file an issue with https://github.com/alleyinteractive/mantle-framework',
61-
E_USER_WARNING
62-
);
63-
6441
return [];
6542
}
6643

6744
/**
6845
* Read the attributes for the current test case and method.
6946
*
70-
* Supports PHPUnit 9.5+.
71-
*
72-
* @todo Remove the PHPUnit 9.5+ support in a future major release and require PHPUnit 11+.
73-
*
7447
* @template T of object
7548
*
7649
* @param class-string|null $name Filter the results to include only ReflectionAttribute instances for attributes matching this class name.
@@ -84,10 +57,7 @@ public function get_annotations_for_method(): array {
8457
public function get_attributes_for_method( ?string $name = null, int $flags = 0, bool $inherit = true ): array {
8558
$class = new ReflectionClass( $this );
8659

87-
// Use either the PHPUnit 9.5+ method or the PHPUnit 10.x method to get the method.
88-
if ( method_exists( $this, 'getName' ) ) {
89-
$method = $class->getMethod( $this->getName( false ) );
90-
} elseif ( method_exists( $this, 'name' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
60+
if ( method_exists( $this, 'name' ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
9161
$method = $class->getMethod( $this->name() );
9262
} elseif ( isset( $this->name ) ) {
9363
$method = $class->getMethod( $this->name );

0 commit comments

Comments
 (0)