diff --git a/composer.json b/composer.json index 9ce7358439..bf9738755b 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,7 @@ "post-package-install": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup", "post-package-update": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup", "drupal-phpunit-upgrade-check": "Drupal\\Core\\Composer\\Composer::upgradePHPUnit", - "drupal-phpunit-upgrade": "@composer update phpunit/phpunit phpspec/prophecy symfony/yaml --with-dependencies --no-progress", + "drupal-phpunit-upgrade": "@composer update phpunit/phpunit symfony/phpunit-bridge phpspec/prophecy symfony/yaml --with-dependencies --no-progress", "phpcs": "phpcs --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer --", "phpcbf": "phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer --" }, diff --git a/core/composer.json b/core/composer.json index 7964dee573..a0dc91a0e7 100644 --- a/core/composer.json +++ b/core/composer.json @@ -59,7 +59,7 @@ "jcalderonzumba/gastonjs": "^1.0.2", "jcalderonzumba/mink-phantomjs-driver": "^0.3.1", "mikey179/vfsStream": "^1.2", - "phpunit/phpunit": "^4.8.35 || ^6.5", + "phpunit/phpunit": "^4.8.35 || ^6.5 || ^7", "phpspec/prophecy": "^1.7", "symfony/css-selector": "^3.4.0", "symfony/phpunit-bridge": "^3.4.3", diff --git a/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php index ba47d91307..6c1bbf81e3 100644 --- a/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php @@ -72,8 +72,8 @@ public function testNumberItem() { $this->assertEqual($entity->field_float[0]->value, $float); $this->assertTrue($entity->field_decimal instanceof FieldItemListInterface, 'Field implements interface.'); $this->assertTrue($entity->field_decimal[0] instanceof FieldItemInterface, 'Field item implements interface.'); - $this->assertEqual($entity->field_decimal->value, $decimal); - $this->assertEqual($entity->field_decimal[0]->value, $decimal); + $this->assertEqual((float) $entity->field_decimal->value, (float) $decimal); + $this->assertEqual((float) $entity->field_decimal[0]->value, (float) $decimal); // Verify changing the number value. $new_integer = rand(11, 20); diff --git a/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php index 9f004abba7..7ece636a2a 100644 --- a/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php @@ -88,7 +88,7 @@ public function testAnonymousNodeWithFile() { $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $node = Node::load($nid); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); - $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); + $this->assertManagedFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); } } @@ -162,7 +162,7 @@ protected function doTestNodeWithFileWithoutTitle() { $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $node = Node::load($nid); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); - $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); + $this->assertManagedFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); } } diff --git a/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php index 332afc9ea7..cb393d1a11 100644 --- a/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php @@ -46,7 +46,7 @@ public function testRevisions() { $node = $node_storage->load($nid); $node_file_r1 = File::load($node->{$field_name}->target_id); $node_vid_r1 = $node->getRevisionId(); - $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.'); + $this->assertManagedFileExists($node_file_r1, 'New file saved to disk on node creation.'); $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.'); $this->assertFileIsPermanent($node_file_r1, 'File is permanent.'); @@ -56,7 +56,7 @@ public function testRevisions() { $node = $node_storage->load($nid); $node_file_r2 = File::load($node->{$field_name}->target_id); $node_vid_r2 = $node->getRevisionId(); - $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.'); + $this->assertManagedFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.'); $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.'); $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.'); @@ -64,7 +64,7 @@ public function testRevisions() { $node = node_revision_load($node_vid_r1); $current_file = File::load($node->{$field_name}->target_id); $this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.'); - $this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.'); + $this->assertManagedFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.'); $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision'); $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.'); @@ -89,7 +89,7 @@ public function testRevisions() { // Delete the second revision and check that the file is kept (since it is // still being used by the third revision). $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete')); - $this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.'); + $this->assertManagedFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.'); $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.'); $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.'); @@ -102,7 +102,7 @@ public function testRevisions() { // Delete the third revision and check that the file is not deleted yet. $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete')); - $this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.'); + $this->assertManagedFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.'); $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.'); $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.'); diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php index 13f4d84148..1b00326170 100644 --- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php +++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php @@ -203,7 +203,7 @@ public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) * @param string $message * (optional) A message to display with the assertion. */ - public static function assertFileExists($file, $message = NULL) { + public static function assertManagedFileExists($file, $message = NULL) { $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); $filename = $file instanceof FileInterface ? $file->getFileUri() : $file; parent::assertFileExists($filename, $message); @@ -230,7 +230,7 @@ public function assertFileEntryExists($file, $message = NULL) { * @param string $message * (optional) A message to display with the assertion. */ - public static function assertFileNotExists($file, $message = NULL) { + public static function assertManagedFileNotExists($file, $message = NULL) { $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); $filename = $file instanceof FileInterface ? $file->getFileUri() : $file; parent::assertFileNotExists($filename, $message); diff --git a/core/modules/file/tests/src/Functional/FileFieldValidateTest.php b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php index c307ce0fb2..4761647fc1 100644 --- a/core/modules/file/tests/src/Functional/FileFieldValidateTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php @@ -40,7 +40,7 @@ public function testRequired() { $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading to the required field.'); + $this->assertManagedFileExists($node_file, 'File exists after uploading to the required field.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.'); // Try again with a multiple value field. @@ -58,7 +58,7 @@ public function testRequired() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.'); + $this->assertManagedFileExists($node_file, 'File exists after uploading to the required multiple value field.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.'); } @@ -92,7 +92,7 @@ public function testFileMaxSize() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize])); + $this->assertManagedFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize])); $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize])); // Check that uploading the large file fails (1M limit). @@ -109,7 +109,7 @@ public function testFileMaxSize() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())])); + $this->assertManagedFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())])); $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())])); } @@ -133,7 +133,7 @@ public function testFileExtension() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); + $this->assertManagedFileExists($node_file, 'File exists after uploading a file with no extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); // Enable extension checking for text files. @@ -152,7 +152,7 @@ public function testFileExtension() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.'); + $this->assertManagedFileExists($node_file, 'File exists after uploading a file with extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.'); } @@ -175,7 +175,7 @@ public function testFileRemoval() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); + $this->assertManagedFileExists($node_file, 'File exists after uploading a file with no extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); // Enable extension checking for text files. diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 993957eaba..5fb8d0b6eb 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -1538,8 +1538,11 @@ protected function assert406Response(ResponseInterface $response) { else { // This is the desired response. $this->assertSame(406, $response->getStatusCode()); - $this->stringContains('?_format=' . static::$format . '>; rel="alternate"; type="' . static::$mimeType . '"', $response->getHeader('Link')); - $this->stringContains('?_format=foobar>; rel="alternate"', $response->getHeader('Link')); + // @todo what are the two lines below intending to test? ::stringContains + // 2nd parameter is expected to be bool also prior to PHPUnit7, but here + // it's an array. Temporarily commenting out. + //$this->stringContains('?_format=' . static::$format . '>; rel="alternate"; type="' . static::$mimeType . '"', $response->getHeader('Link')); + //$this->stringContains('?_format=foobar>; rel="alternate"', $response->getHeader('Link')); } } diff --git a/core/phpunit.xml.dist b/core/phpunit.xml.dist index 4f81a2d533..3c7a067762 100644 --- a/core/phpunit.xml.dist +++ b/core/phpunit.xml.dist @@ -65,7 +65,7 @@ ../modules ../sites - */tests/* + diff --git a/core/tests/Drupal/KernelTests/AssertLegacyTrait.php b/core/tests/Drupal/KernelTests/AssertLegacyTrait.php index f63c4bf0b6..965608dced 100644 --- a/core/tests/Drupal/KernelTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/KernelTests/AssertLegacyTrait.php @@ -23,30 +23,6 @@ protected function assert($actual, $message = '') { parent::assertTrue((bool) $actual, $message); } - /** - * @see \Drupal\simpletest\TestBase::assertTrue() - */ - public static function assertTrue($actual, $message = '') { - if (is_bool($actual)) { - parent::assertTrue($actual, $message); - } - else { - parent::assertNotEmpty($actual, $message); - } - } - - /** - * @see \Drupal\simpletest\TestBase::assertFalse() - */ - public static function assertFalse($actual, $message = '') { - if (is_bool($actual)) { - parent::assertFalse($actual, $message); - } - else { - parent::assertEmpty($actual, $message); - } - } - /** * @see \Drupal\simpletest\TestBase::assertEqual() * @@ -54,7 +30,7 @@ public static function assertFalse($actual, $message = '') { * instead. */ protected function assertEqual($actual, $expected, $message = '') { - $this->assertEquals($expected, $actual, $message); + $this->assertEquals($expected, $actual, !empty($message) ? $message : ''); } /** @@ -64,7 +40,7 @@ protected function assertEqual($actual, $expected, $message = '') { * self::assertNotEquals() instead. */ protected function assertNotEqual($actual, $expected, $message = '') { - $this->assertNotEquals($expected, $actual, $message); + $this->assertNotEquals($expected, $actual, !empty($message) ? $message : ''); } /** @@ -74,7 +50,7 @@ protected function assertNotEqual($actual, $expected, $message = '') { * instead. */ protected function assertIdentical($actual, $expected, $message = '') { - $this->assertSame($expected, $actual, $message); + $this->assertSame($expected, $actual, !empty($message) ? $message : ''); } /** @@ -84,7 +60,7 @@ protected function assertIdentical($actual, $expected, $message = '') { * self::assertNotSame() instead. */ protected function assertNotIdentical($actual, $expected, $message = '') { - $this->assertNotSame($expected, $actual, $message); + $this->assertNotSame($expected, $actual, !empty($message) ? $message : ''); } /** @@ -97,7 +73,7 @@ protected function assertIdenticalObject($actual, $expected, $message = '') { // Note: ::assertSame checks whether its the same object. ::assertEquals // though compares - $this->assertEquals($expected, $actual, $message); + $this->assertEquals($expected, $actual, !empty($message) ? $message : ''); } /** diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index dad4c97ea1..f90a703f1e 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -19,15 +19,26 @@ use Drupal\Core\Test\TestDatabase; use Drupal\Tests\AssertHelperTrait; use Drupal\Tests\ConfigTestTrait; +use Drupal\Tests\PhpunitForwardCompatibilityTrait; use Drupal\Tests\PhpunitCompatibilityTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\TestRequirementsTrait; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpFoundation\Request; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\visitor\vfsStreamPrintVisitor; +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a compatibility trait dependent on the PHPUnit runner +// version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + require_once __DIR__ . '/../Tests/PhpunitCompatibility7Trait.php'; +} else { + require_once __DIR__ . '/../Tests/PhpunitCompatibilityPre7Trait.php'; +} + /** * Base class for functional integration tests. * @@ -75,6 +86,7 @@ use RandomGeneratorTrait; use ConfigTestTrait; use TestRequirementsTrait; + use PhpunitForwardCompatibilityTrait; use PhpunitCompatibilityTrait; /** @@ -1112,7 +1124,7 @@ public function __sleep() { /** * {@inheritdoc} */ - public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) { + public static function assertEquals($expected, $actual, string $message = '', float $delta = 0, int $maxDepth = 10, bool $canonicalize = FALSE, bool $ignoreCase = FALSE): void { // Cast objects implementing MarkupInterface to string instead of // relying on PHP casting them to string depending on what they are being // comparing with. diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 204eacb4f5..097cb79b25 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -19,10 +19,20 @@ use Drupal\Tests\user\Traits\UserCreationTrait; use GuzzleHttp\Cookie\CookieJar; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\CssSelector\CssSelectorConverter; +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a compatibility trait dependent on the PHPUnit runner +// version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + require_once __DIR__ . '/PhpunitCompatibility7Trait.php'; +} else { + require_once __DIR__ . '/PhpunitCompatibilityPre7Trait.php'; +} + /** * Provides a test case for functional Drupal tests. * @@ -62,6 +72,7 @@ createUser as drupalCreateUser; } use XdebugRequestTrait; + use PhpunitForwardCompatibilityTrait; use PhpunitCompatibilityTrait; /** @@ -690,7 +701,7 @@ protected function getDrupalSettings() { /** * {@inheritdoc} */ - public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) { + public static function assertEquals($expected, $actual, string $message = '', float $delta = 0, int $maxDepth = 10, bool $canonicalize = FALSE, bool $ignoreCase = FALSE): void { // Cast objects implementing MarkupInterface to string instead of // relying on PHP casting them to string depending on what they are being // comparing with. diff --git a/core/tests/Drupal/Tests/Core/Test/TestSuiteBaseTest.php b/core/tests/Drupal/Tests/Core/Test/TestSuiteBaseTest.php index 853a788f5f..fdcc901511 100644 --- a/core/tests/Drupal/Tests/Core/Test/TestSuiteBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Test/TestSuiteBaseTest.php @@ -142,7 +142,7 @@ protected function findExtensionDirectories($root) { /** * {@inheritdoc} */ - public function addTestFiles($filenames) { + public function addTestFiles($filenames): void { // We stub addTestFiles() because the parent implementation can't deal with // vfsStream-based filesystems due to an error in // stream_resolve_include_path(). See diff --git a/core/tests/Drupal/Tests/Listeners/DrupalListener.php b/core/tests/Drupal/Tests/Listeners/DrupalListener.php index cafaa2292b..8a00e55ecb 100644 --- a/core/tests/Drupal/Tests/Listeners/DrupalListener.php +++ b/core/tests/Drupal/Tests/Listeners/DrupalListener.php @@ -2,42 +2,20 @@ namespace Drupal\Tests\Listeners; -use PHPUnit\Framework\BaseTestListener; -use PHPUnit\Framework\Test; +use PHPUnit\Runner\Version; -if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a class dependent on the PHPUnit runner version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + class_alias('Drupal\Tests\Listeners\DrupalListener7', 'Drupal\Tests\Listeners\DrupalListener'); +} +elseif (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '6.0.0', '>=')) { + class_alias('Drupal\Tests\Listeners\DrupalListener6', 'Drupal\Tests\Listeners\DrupalListener'); +} +else if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { class_alias('Drupal\Tests\Listeners\Legacy\DrupalListener', 'Drupal\Tests\Listeners\DrupalListener'); // Using an early return instead of a else does not work when using the // PHPUnit phar due to some weird PHP behavior (the class gets defined without // executing the code before it and so the definition is not properly // conditional). } -else { - /** - * Listens to PHPUnit test runs. - * - * @internal - */ - class DrupalListener extends BaseTestListener { - use DeprecationListenerTrait; - use DrupalComponentTestListenerTrait; - use DrupalStandardsListenerTrait; - - /** - * {@inheritdoc} - */ - public function startTest(Test $test) { - $this->deprecationStartTest($test); - } - - /** - * {@inheritdoc} - */ - public function endTest(Test $test, $time) { - $this->deprecationEndTest($test, $time); - $this->componentEndTest($test, $time); - $this->standardsEndTest($test, $time); - } - - } -} diff --git a/core/tests/Drupal/Tests/Listeners/DrupalListener6.php b/core/tests/Drupal/Tests/Listeners/DrupalListener6.php new file mode 100644 index 0000000000..450073733b --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/DrupalListener6.php @@ -0,0 +1,35 @@ +deprecationStartTest($test); + } + + /** + * {@inheritdoc} + */ + public function endTest(Test $test, $time) { + $this->deprecationEndTest($test, $time); + $this->componentEndTest($test, $time); + $this->standardsEndTest($test, $time); + } + +} diff --git a/core/tests/Drupal/Tests/Listeners/DrupalListener7.php b/core/tests/Drupal/Tests/Listeners/DrupalListener7.php new file mode 100644 index 0000000000..032e6f2a64 --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/DrupalListener7.php @@ -0,0 +1,37 @@ +deprecationStartTest($test); + } + + /** + * {@inheritdoc} + */ + public function endTest(Test $test, $time): void { + $this->deprecationEndTest($test, $time); + $this->componentEndTest($test, $time); + $this->standardsEndTest($test, $time); + } + +} diff --git a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php index 49618b8172..a488a92af2 100644 --- a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php +++ b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php @@ -2,42 +2,20 @@ namespace Drupal\Tests\Listeners; -use PHPUnit\Framework\TestResult; -use PHPUnit\TextUI\ResultPrinter; +use PHPUnit\Runner\Version; -if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a class dependent on the PHPUnit runner version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + class_alias('Drupal\Tests\Listeners\HtmlOutputPrinter7', 'Drupal\Tests\Listeners\HtmlOutputPrinter'); +} +elseif (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '6.0.0', '>=')) { + class_alias('Drupal\Tests\Listeners\HtmlOutputPrinter6', 'Drupal\Tests\Listeners\HtmlOutputPrinter'); +} +else if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { class_alias('Drupal\Tests\Listeners\Legacy\HtmlOutputPrinter', 'Drupal\Tests\Listeners\HtmlOutputPrinter'); // Using an early return instead of a else does not work when using the // PHPUnit phar due to some weird PHP behavior (the class gets defined without // executing the code before it and so the definition is not properly // conditional). } -else { - /** - * Defines a class for providing html output results for functional tests. - * - * @internal - */ - class HtmlOutputPrinter extends ResultPrinter { - use HtmlOutputPrinterTrait; - - /** - * {@inheritdoc} - */ - public function __construct($out = NULL, $verbose = FALSE, $colors = self::COLOR_DEFAULT, $debug = FALSE, $numberOfColumns = 80, $reverse = FALSE) { - parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse); - - $this->setUpHtmlOutput(); - } - - /** - * {@inheritdoc} - */ - public function printResult(TestResult $result) { - parent::printResult($result); - - $this->printHtmlOutput(); - } - - } -} diff --git a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter6.php b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter6.php new file mode 100644 index 0000000000..492c872c49 --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter6.php @@ -0,0 +1,26 @@ +printHtmlOutput(); + } + +} diff --git a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter7.php b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter7.php new file mode 100644 index 0000000000..c968e9df2b --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter7.php @@ -0,0 +1,26 @@ +printHtmlOutput(); + } + +} diff --git a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinterTrait.php b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinterTrait.php index 1dd67eb9e9..19afbd503d 100644 --- a/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinterTrait.php +++ b/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinterTrait.php @@ -2,6 +2,9 @@ namespace Drupal\Tests\Listeners; +use PHPUnit\Framework\TestResult; +use PHPUnit\TextUI\ResultPrinter; + /** * Defines a class for providing html output results for functional tests. * @@ -16,6 +19,15 @@ */ protected $browserOutputFile; + /** + * {@inheritdoc} + */ + public function __construct($out = NULL, $verbose = FALSE, $colors = self::COLOR_DEFAULT, $debug = FALSE, $numberOfColumns = 80, $reverse = FALSE) { + parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse); + + $this->setUpHtmlOutput(); + } + /** * Creates the file to list the HTML output created during the test. * diff --git a/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter.php b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter.php index bfb91d7b9f..2cb824d296 100644 --- a/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter.php +++ b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter.php @@ -2,25 +2,13 @@ namespace Drupal\Tests\Listeners; -use Drupal\Component\Utility\Html; - -/** - * Defines a class for providing html output links in the Simpletest UI. - */ -class SimpletestUiPrinter extends HtmlOutputPrinter { - - /** - * {@inheritdoc} - */ - public function write($buffer) { - $buffer = Html::escape($buffer); - // Turn HTML output URLs into clickable link tags. - $url_pattern = '@https?://[^\s]+@'; - $buffer = preg_replace($url_pattern, '$0', $buffer); - // Make the output readable in HTML by breaking up lines properly. - $buffer = nl2br($buffer); - - print $buffer; - } +use PHPUnit\Runner\Version; +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a class dependent on the PHPUnit runner version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + class_alias('Drupal\Tests\Listeners\SimpletestUiPrinter7', 'Drupal\Tests\Listeners\SimpletestUiPrinter'); +} +else { + class_alias('Drupal\Tests\Listeners\SimpletestUiPrinterPre7', 'Drupal\Tests\Listeners\SimpletestUiPrinter7'); } diff --git a/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter7.php b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter7.php new file mode 100644 index 0000000000..fca0b5952c --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinter7.php @@ -0,0 +1,26 @@ + tags. + $url_pattern = '@https?://[^\s]+@'; + $buffer = preg_replace($url_pattern, '$0', $buffer); + // Make the output readable in HTML by breaking up lines properly. + $buffer = nl2br($buffer); + + print $buffer; + } + +} diff --git a/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinterPre7.php b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinterPre7.php new file mode 100644 index 0000000000..c8d0f13aec --- /dev/null +++ b/core/tests/Drupal/Tests/Listeners/SimpletestUiPrinterPre7.php @@ -0,0 +1,26 @@ + tags. + $url_pattern = '@https?://[^\s]+@'; + $buffer = preg_replace($url_pattern, '$0', $buffer); + // Make the output readable in HTML by breaking up lines properly. + $buffer = nl2br($buffer); + + print $buffer; + } + +} diff --git a/core/tests/Drupal/Tests/PhpunitCompatibility7Trait.php b/core/tests/Drupal/Tests/PhpunitCompatibility7Trait.php new file mode 100644 index 0000000000..32153bea69 --- /dev/null +++ b/core/tests/Drupal/Tests/PhpunitCompatibility7Trait.php @@ -0,0 +1,61 @@ +supports('createMock')) { + return parent::createMock($originalClassName); + } + else { + return $this->getMock($originalClassName, [], [], '', FALSE, FALSE); + } + } + + /** + * @see \Drupal\simpletest\TestBase::assertTrue() + */ + public static function assertTrue($actual, string $message = ''): void { + if (is_bool($actual)) { + parent::assertTrue($actual, $message); + } + else { + parent::assertNotEmpty($actual, $message); + } + } + + /** + * @see \Drupal\simpletest\TestBase::assertFalse() + */ + public static function assertFalse($actual, string $message = ''): void { + if (is_bool($actual)) { + parent::assertFalse($actual, $message); + } + else { + parent::assertEmpty($actual, $message); + } + } + +} diff --git a/core/tests/Drupal/Tests/PhpunitCompatibilityPre7Trait.php b/core/tests/Drupal/Tests/PhpunitCompatibilityPre7Trait.php new file mode 100644 index 0000000000..79f9a9c95c --- /dev/null +++ b/core/tests/Drupal/Tests/PhpunitCompatibilityPre7Trait.php @@ -0,0 +1,60 @@ +supports('createMock')) { + return parent::createMock($originalClassName); + } + else { + return $this->getMock($originalClassName, [], [], '', FALSE, FALSE); + } + } + + /** + * @see \Drupal\simpletest\TestBase::assertTrue() + */ + public static function assertTrue($actual, $message = '') { + if (is_bool($actual)) { + parent::assertTrue($actual, $message); + } + else { + parent::assertNotEmpty($actual, $message); + } + } + + /** + * @see \Drupal\simpletest\TestBase::assertFalse() + */ + public static function assertFalse($actual, $message = '') { + if (is_bool($actual)) { + parent::assertFalse($actual, $message); + } + else { + parent::assertEmpty($actual, $message); + } + } + +} diff --git a/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php b/core/tests/Drupal/Tests/PhpunitForwardCompatibilityTrait.php similarity index 86% rename from core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php rename to core/tests/Drupal/Tests/PhpunitForwardCompatibilityTrait.php index e0a5f72846..5101d46905 100644 --- a/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php +++ b/core/tests/Drupal/Tests/PhpunitForwardCompatibilityTrait.php @@ -5,7 +5,7 @@ /** * Makes Drupal's test API forward compatible with multiple versions of PHPUnit. */ -trait PhpunitCompatibilityTrait { +trait PhpunitForwardCompatibilityTrait { /** * Returns a mock object for the specified class using the available method. @@ -92,30 +92,6 @@ public function getMock($originalClassName, $methods = [], array $arguments = [] } } - /** - * Returns a mock object for the specified class using the available method. - * - * The createMock method does not exist in PHPUnit 4. To provide forward - * compatibility this trait provides the createMock method and uses createMock - * if this method is available on the parent class or falls back to getMock if - * it isn't. - * - * @param string $originalClassName - * Name of the class to mock. - * - * @see \PHPUnit_Framework_TestCase::getMock - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - public function createMock($originalClassName) { - if ($this->supports('createMock')) { - return parent::createMock($originalClassName); - } - else { - return $this->getMock($originalClassName, [], [], '', FALSE, FALSE); - } - } - /** * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. * diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index bd183c7961..1a4a9dfb0c 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -10,6 +10,16 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; + +// In order to manage different method signatures between PHPUnit versions, we +// dynamically load a compatibility trait dependent on the PHPUnit runner +// version. +if (class_exists('PHPUnit\Runner\Version') && version_compare(Version::id(), '7.0.0', '>=')) { + require_once __DIR__ . '/PhpunitCompatibility7Trait.php'; +} else { + require_once __DIR__ . '/PhpunitCompatibilityPre7Trait.php'; +} /** * Provides a base class and helpers for Drupal unit tests. @@ -18,6 +28,7 @@ */ abstract class UnitTestCase extends TestCase { + use PhpunitForwardCompatibilityTrait; use PhpunitCompatibilityTrait; /**