|
7 | 7 |
|
8 | 8 | namespace Mantle\Testing\Concerns; |
9 | 9 |
|
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; |
16 | 11 |
|
17 | 12 | /** |
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. |
25 | 14 | * |
26 | | - * @mixin \Mantle\Testing\Installation_Manager |
| 15 | + * @deprecated 1.14.1 |
27 | 16 | */ |
28 | 17 | 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 | | - |
127 | 18 | /** |
128 | 19 | * Silence the PHPUnit 10+ warning. |
| 20 | + * |
| 21 | + * No longer does anything, kept for backward compatibility. |
129 | 22 | */ |
| 23 | + #[Deprecated( 'This method no longer does anything and will be removed with Mantle 2.0.' )] |
130 | 24 | public function silence_phpunit_warning(): static { |
131 | | - $this->silence_phpunit_warning = true; |
132 | | - |
133 | 25 | return $this; |
134 | 26 | } |
135 | 27 | } |
0 commit comments