From db132d85a0bec4ac23249eb4bada540405e2e683 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Tue, 5 Mar 2024 15:35:33 +0000 Subject: [PATCH] Issue #3425660 by mondrake: [11.x] Remove deprecated ExternalCommandRequirementsTrait --- core/phpstan-baseline.neon | 36 ---- .../ExternalCommandRequirementsTrait.php | 117 ----------- .../Tests/ExternalCommandRequirementTest.php | 190 ------------------ 3 files changed, 343 deletions(-) delete mode 100644 core/tests/Drupal/BuildTests/Framework/ExternalCommandRequirementsTrait.php delete mode 100644 core/tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index 8f8403102410..516166a16f9e 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -2151,42 +2151,6 @@ parameters: count: 1 path: tests/Drupal/BuildTests/Composer/Template/ComposerProjectTemplatesTest.php - - - message: """ - #^Usage of deprecated trait Drupal\\\\BuildTests\\\\Framework\\\\ExternalCommandRequirementsTrait in class Drupal\\\\BuildTests\\\\Framework\\\\Tests\\\\ClassRequiresAvailable\\: - in drupal\\:10\\.2\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use - Drupal\\\\\\\\TestTools\\\\\\\\Extension\\\\\\\\RequiresComposerTrait instead\\.$# - """ - count: 1 - path: tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php - - - - message: """ - #^Usage of deprecated trait Drupal\\\\BuildTests\\\\Framework\\\\ExternalCommandRequirementsTrait in class Drupal\\\\BuildTests\\\\Framework\\\\Tests\\\\ClassRequiresUnavailable\\: - in drupal\\:10\\.2\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use - Drupal\\\\\\\\TestTools\\\\\\\\Extension\\\\\\\\RequiresComposerTrait instead\\.$# - """ - count: 1 - path: tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php - - - - message: """ - #^Usage of deprecated trait Drupal\\\\BuildTests\\\\Framework\\\\ExternalCommandRequirementsTrait in class Drupal\\\\BuildTests\\\\Framework\\\\Tests\\\\MethodRequires\\: - in drupal\\:10\\.2\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use - Drupal\\\\\\\\TestTools\\\\\\\\Extension\\\\\\\\RequiresComposerTrait instead\\.$# - """ - count: 1 - path: tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php - - - - message: """ - #^Usage of deprecated trait Drupal\\\\BuildTests\\\\Framework\\\\ExternalCommandRequirementsTrait in class Drupal\\\\BuildTests\\\\Framework\\\\Tests\\\\UsesCommandRequirements\\: - in drupal\\:10\\.2\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use - Drupal\\\\\\\\TestTools\\\\\\\\Extension\\\\\\\\RequiresComposerTrait instead\\.$# - """ - count: 1 - path: tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php - - message: "#^Variable \\$found might not be defined\\.$#" count: 1 diff --git a/core/tests/Drupal/BuildTests/Framework/ExternalCommandRequirementsTrait.php b/core/tests/Drupal/BuildTests/Framework/ExternalCommandRequirementsTrait.php deleted file mode 100644 index 68635e21676e..000000000000 --- a/core/tests/Drupal/BuildTests/Framework/ExternalCommandRequirementsTrait.php +++ /dev/null @@ -1,117 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Drupal\BuildTests\Framework; - -use PHPUnit\Framework\SkippedTestError; -use PHPUnit\Util\Test; -use Symfony\Component\Process\ExecutableFinder; - -/** - * Allows test classes to require external command line applications. - * - * Use annotation such as '(at)requires externalCommand git'. - * - * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use - * Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. - * - * @see https://www.drupal.org/node/3362239 - */ -trait ExternalCommandRequirementsTrait { - - /** - * A list of existing external commands we've already discovered. - * - * @var string[] - */ - private static $existingCommands = []; - - /** - * Checks whether required external commands are available per test class. - * - * @throws \PHPUnit\Framework\SkippedTestError - * Thrown when the requirements are not met, and this test should be - * skipped. Callers should not catch this exception. - */ - private static function checkClassCommandRequirements() { - @trigger_error(__METHOD__ . "() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239", E_USER_DEPRECATED); - $annotations = Test::parseTestMethodAnnotations(static::class); - if (!empty($annotations['class']['requires'])) { - static::checkExternalCommandRequirements($annotations['class']['requires']); - } - } - - /** - * Checks whether required external commands are available per method. - * - * @throws \PHPUnit\Framework\SkippedTestError - * Thrown when the requirements are not met, and this test should be - * skipped. Callers should not catch this exception. - */ - private static function checkMethodCommandRequirements($name) { - @trigger_error(__METHOD__ . "() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239", E_USER_DEPRECATED); - $annotations = Test::parseTestMethodAnnotations(static::class, $name); - if (!empty($annotations['method']['requires'])) { - static::checkExternalCommandRequirements($annotations['method']['requires']); - } - } - - /** - * Checks missing external command requirements. - * - * @param string[] $annotations - * A list of requires annotations from either a method or class annotation. - * - * @throws \PHPUnit\Framework\SkippedTestError - * Thrown when the requirements are not met, and this test should be - * skipped. Callers should not catch this exception. - */ - private static function checkExternalCommandRequirements(array $annotations) { - @trigger_error(__METHOD__ . "() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239", E_USER_DEPRECATED); - // Make a list of required commands. - $required_commands = []; - foreach ($annotations as $requirement) { - if (str_starts_with($requirement, 'externalCommand ')) { - @trigger_error("The '@require externalCommand' annotation for tests is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239", E_USER_DEPRECATED); - $command = trim(str_replace('externalCommand ', '', $requirement)); - // Use named keys to avoid duplicates. - $required_commands[$command] = $command; - } - } - - // Figure out which commands are not available. - $unavailable = []; - foreach ($required_commands as $required_command) { - if (!in_array($required_command, self::$existingCommands)) { - if (static::externalCommandIsAvailable($required_command)) { - // Cache existing commands so we don't have to ask again. - self::$existingCommands[] = $required_command; - } - else { - $unavailable[] = $required_command; - } - } - } - - // Skip the test if there were some we couldn't find. - if (!empty($unavailable)) { - throw new SkippedTestError('Required external commands: ' . implode(', ', $unavailable)); - } - } - - /** - * Determine if an external command is available. - * - * @param $command - * The external command. - * - * @return bool - * TRUE if external command is available, else FALSE. - */ - private static function externalCommandIsAvailable($command) { - $finder = new ExecutableFinder(); - return (bool) $finder->find($command); - } - -} diff --git a/core/tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php b/core/tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php deleted file mode 100644 index 8d260cb827e1..000000000000 --- a/core/tests/Drupal/BuildTests/Framework/Tests/ExternalCommandRequirementTest.php +++ /dev/null @@ -1,190 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Drupal\BuildTests\Framework\Tests; - -use Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait; -use PHPUnit\Framework\SkippedTestError; -use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; - -/** - * @coversDefaultClass \Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait - * @group Build - * @group legacy - */ -class ExternalCommandRequirementTest extends TestCase { - - use ExpectDeprecationTrait; - - /** - * @covers ::checkExternalCommandRequirements - */ - public function testCheckExternalCommandRequirementsNotAvailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkExternalCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $this->expectDeprecation('The \'@require externalCommand\' annotation for tests is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new UsesCommandRequirements(); - $ref_check_requirements = new \ReflectionMethod($requires, 'checkExternalCommandRequirements'); - - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $ref_check_requirements->invokeArgs($requires, [ - ['externalCommand not_available', 'externalCommand available_command'], - ]); - $this->fail('Unavailable external command requirement should throw a skipped test error exception.'); - } - catch (SkippedTestError $exception) { - $this->assertEquals('Required external commands: not_available', $exception->getMessage()); - } - } - - /** - * @covers ::checkExternalCommandRequirements - */ - public function testCheckExternalCommandRequirementsAvailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkExternalCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $this->expectDeprecation('The \'@require externalCommand\' annotation for tests is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new UsesCommandRequirements(); - $ref_check_requirements = new \ReflectionMethod($requires, 'checkExternalCommandRequirements'); - - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $this->assertNull( - $ref_check_requirements->invokeArgs($requires, [['externalCommand available_command']]) - ); - } - catch (SkippedTestError $exception) { - $this->fail(sprintf('The external command should be available: %s', $exception->getMessage())); - } - } - - /** - * @covers ::checkClassCommandRequirements - */ - public function testClassRequiresAvailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkClassCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new ClassRequiresAvailable(); - $ref_check = new \ReflectionMethod($requires, 'checkClassCommandRequirements'); - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $this->assertNull($ref_check->invoke($requires)); - } - catch (SkippedTestError $exception) { - $this->fail(sprintf('The external command should be available: %s', $exception->getMessage())); - } - } - - /** - * @covers ::checkClassCommandRequirements - */ - public function testClassRequiresUnavailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkClassCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new ClassRequiresUnavailable(); - $ref_check = new \ReflectionMethod($requires, 'checkClassCommandRequirements'); - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $this->assertNull($ref_check->invoke($requires)); - $this->fail('Unavailable external command requirement should throw a skipped test error exception.'); - } - catch (SkippedTestError $exception) { - $this->assertEquals('Required external commands: unavailable_command', $exception->getMessage()); - } - } - - /** - * @covers ::checkMethodCommandRequirements - */ - public function testMethodRequiresAvailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkMethodCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new MethodRequires(); - $ref_check = new \ReflectionMethod($requires, 'checkMethodCommandRequirements'); - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $this->assertNull($ref_check->invoke($requires, 'testRequiresAvailable')); - } - catch (SkippedTestError $exception) { - $this->fail(sprintf('The external command should be available: %s', $exception->getMessage())); - } - } - - /** - * @covers ::checkMethodCommandRequirements - */ - public function testMethodRequiresUnavailable() { - $this->expectDeprecation('Drupal\BuildTests\Framework\ExternalCommandRequirementsTrait::checkMethodCommandRequirements() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\\TestTools\\Extension\\RequiresComposerTrait instead. See https://www.drupal.org/node/3362239'); - $requires = new MethodRequires(); - $ref_check = new \ReflectionMethod($requires, 'checkMethodCommandRequirements'); - // Use a try/catch block because otherwise PHPUnit might think this test is - // legitimately skipped. - try { - $this->assertNull($ref_check->invoke($requires, 'testRequiresUnavailable')); - $this->fail('Unavailable external command requirement should throw a skipped test error exception.'); - } - catch (SkippedTestError $exception) { - $this->assertEquals('Required external commands: unavailable_command', $exception->getMessage()); - } - } - -} - -class UsesCommandRequirements { - - use ExternalCommandRequirementsTrait; - - protected static function externalCommandIsAvailable($command) { - return in_array($command, ['available_command']); - } - -} - -/** - * @requires externalCommand available_command - */ -class ClassRequiresAvailable { - - use ExternalCommandRequirementsTrait; - - protected static function externalCommandIsAvailable($command) { - return in_array($command, ['available_command']); - } - -} - -/** - * @requires externalCommand unavailable_command - */ -class ClassRequiresUnavailable { - - use ExternalCommandRequirementsTrait; - -} - -class MethodRequires { - - use ExternalCommandRequirementsTrait; - - /** - * @requires externalCommand available_command - */ - public function testRequiresAvailable() { - - } - - /** - * @requires externalCommand unavailable_command - */ - public function testRequiresUnavailable() { - - } - - protected static function externalCommandIsAvailable($command) { - return in_array($command, ['available_command']); - } - -} -- GitLab