Problem/Motivation

Sebastian Bergmann added a forward compability layer for phpunit 4.8, see https://github.com/sebastianbergmann/phpunit/commit/a55235c033c2dc18db63...

This allows us to ease a migration to phpunit 6, once we decide to jump onto php 7 or phpunit 5 when we go to 5.6

Proposed resolution

Use the new classes instead of the old ones.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

dawehner created an issue. See original summary.

maxocub’s picture

Status: Active » Needs review
StatusFileSize
new30.72 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice fix! I cannot think about a reason why this should break BC. I wonder though whether we should update the documents about how to write phpunit tests ... ideally we would not yet, given that the new base class requires the most uptodate phpunit version, and the old documentation still supports what existed before.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +1 on the change - looks great - Symfony did something very similar.
  2. +++ b/core/modules/content_moderation/tests/src/Unit/ContentPreprocessTest.php
    @@ -5,13 +5,14 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class ContentPreprocessTest extends \PHPUnit_Framework_TestCase {
    +class ContentPreprocessTest extends TestCase {
    
    +++ b/core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php
    @@ -9,13 +9,14 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class LatestRevisionCheckTest extends \PHPUnit_Framework_TestCase {
    +class LatestRevisionCheckTest extends TestCase {
    
    +++ b/core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
    @@ -11,12 +11,13 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class ModerationInformationTest extends \PHPUnit_Framework_TestCase {
    +class ModerationInformationTest extends TestCase {
    
    +++ b/core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php
    @@ -10,13 +10,14 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class StateTransitionValidationTest extends \PHPUnit_Framework_TestCase {
    +class StateTransitionValidationTest extends TestCase {
    
    +++ b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php
    @@ -3,12 +3,13 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class EventBaseTest extends \PHPUnit_Framework_TestCase {
    +class EventBaseTest extends TestCase {
    
    +++ b/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php
    @@ -3,12 +3,13 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class MigrateImportEventTest extends \PHPUnit_Framework_TestCase {
    +class MigrateImportEventTest extends TestCase {
    
    +++ b/core/modules/serialization/src/Tests/RegisterSerializationClassesCompilerPassTest.php
    @@ -4,6 +4,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -11,7 +12,7 @@
    -class RegisterSerializationClassesCompilerPassTest extends \PHPUnit_Framework_TestCase {
    +class RegisterSerializationClassesCompilerPassTest extends TestCase {
    

    So I think for core unit tests we should ensure that they are using \Drupal\Tests\UnitTestCase it is just a mistake that they are not.

  3. +++ b/core/modules/simpletest/simpletest.install
    @@ -18,7 +18,7 @@
    -  $has_phpunit = class_exists('\PHPUnit_Framework_TestCase');
    +  $has_phpunit = class_exists('\PHPUnit\Framework\TestCase');
    
    +++ b/core/modules/simpletest/simpletest.module
    @@ -412,7 +412,7 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
    -  if (is_subclass_of($test_class, \PHPUnit_Framework_TestCase::class)) {
    +  if (is_subclass_of($test_class, '\PHPUnit\Framework\TestCase')) {
    

    Let's add a use statement and do TestCase::class.

  4. +++ b/core/modules/simpletest/tests/fixtures/simpletest_phpunit_run_command_test.php
    @@ -11,7 +13,7 @@
    -class SimpletestPhpunitRunCommandTestWillDie extends \PHPUnit_Framework_TestCase {
    +class SimpletestPhpunitRunCommandTestWillDie extends TestCase {
    
    +++ b/core/modules/simpletest/tests/src/Unit/SimpletestPhpunitRunCommandTest.php
    @@ -4,6 +4,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -12,7 +13,7 @@
    -class SimpletestPhpunitRunCommandTest extends \PHPUnit_Framework_TestCase {
    +class SimpletestPhpunitRunCommandTest extends TestCase {
    

    This might be an exception to the use \Drupal\Tests\UnitTestCase rule - if it doesn't work then it should be documented why.

  5. +++ b/core/scripts/run-tests.sh
    @@ -36,7 +36,7 @@
    -if (!class_exists('\PHPUnit_Framework_TestCase')) {
    +if (!class_exists('\PHPUnit\Framework\TestCase')) {
    
    @@ -783,7 +783,7 @@ function simpletest_script_run_one_test($test_id, $test_class) {
    -    if (is_subclass_of($test_class, '\PHPUnit_Framework_TestCase')) {
    +    if (is_subclass_of($test_class, '\PHPUnit\Framework\TestCase')) {
    
    @@ -865,7 +865,7 @@ function simpletest_script_command($test_id, $test_class) {
    -  if (is_subclass_of($test_class, '\PHPUnit_Framework_TestCase')) {
    +  if (is_subclass_of($test_class, '\PHPUnit\Framework\TestCase')) {
    
    @@ -1020,7 +1020,7 @@ function simpletest_script_get_test_list() {
    -            if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, '\PHPUnit_Framework_TestCase')) {
    +            if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, '\PHPUnit\Framework\TestCase')) {
    
    @@ -1074,7 +1074,7 @@ function simpletest_script_get_test_list() {
    -            if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, '\PHPUnit_Framework_TestCase')) {
    +            if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, '\PHPUnit\Framework\TestCase')) {
    

    Use and ::class

  6. +++ b/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
    @@ -7,14 +7,14 @@
    -use PHPUnit_Framework_TestCase;
    +use PHPUnit\Framework\TestCase;
    ...
    -class InspectorTest extends PHPUnit_Framework_TestCase {
    +class InspectorTest extends TestCase {
    

    In component tests this is totally correct - they should NOT use \Drupal\Tests\UnitTestCase

  7. +++ b/core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php
    @@ -3,6 +3,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -10,7 +11,7 @@
    -class SessionCacheContextTest extends \PHPUnit_Framework_TestCase {
    +class SessionCacheContextTest extends TestCase {
    
    +++ b/core/tests/Drupal/Tests/Core/Config/ConfigFactoryOverrideBaseTest.php
    @@ -6,12 +6,13 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class ConfigFactoryOverrideBaseTest extends \PHPUnit_Framework_TestCase {
    +class ConfigFactoryOverrideBaseTest extends TestCase {
    
    +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/AuthenticationProviderPassTest.php
    @@ -4,6 +4,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -11,7 +12,7 @@
    -class AuthenticationProviderPassTest extends \PHPUnit_Framework_TestCase {
    +class AuthenticationProviderPassTest extends TestCase {
    
    +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php
    @@ -6,12 +6,13 @@
    +use PHPUnit\Framework\TestCase;
    ...
    -class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase {
    +class YamlFileLoaderTest extends TestCase {
    
    +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php
    @@ -3,6 +3,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -14,7 +15,7 @@
    -class OptionsRequestSubscriberTest extends \PHPUnit_Framework_TestCase {
    +class OptionsRequestSubscriberTest extends TestCase {
    
    +++ b/core/tests/Drupal/Tests/Core/Routing/MethodFilterTest.php
    @@ -3,6 +3,7 @@
    +use PHPUnit\Framework\TestCase;
    
    @@ -12,7 +13,7 @@
    -class MethodFilterTest extends \PHPUnit_Framework_TestCase {
    +class MethodFilterTest extends TestCase {
    

    These should use \Drupal\Tests\UnitTestCase because it's core (and not component)

maxocub’s picture

Status: Needs work » Needs review
StatusFileSize
new31.76 KB
new17.08 KB

Made all the changes from #4, thanks for the review!

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

@maxocub Do you want to create a followup to remove all the usages of UnitTestCase in the component tests?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 2850797-5.patch, failed testing.

maxocub’s picture

@dawehner: Yes, I'll do that.

maxocub’s picture

Status: Needs work » Needs review
StatusFileSize
new31.83 KB

#2865413: Move RegisterSerializationClassesCompilerPassTest.php to correct location moved RegisterSerializationClassesCompilerPassTest, so the patch didn't apply anymore. Here's a new one. I don't have an interdiff since it's just one class that moved.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -PHPUnit +Bphpunit

Back to RTBC

wim leers’s picture

Issue tags: -Bphpunit +PHPUnit

I think #10 was a typo? :)

martin107’s picture

Title: Prepare out phpunit tests to be BC compatible with phpunit 5.x/6.x » Prepare our phpunit tests to be BC compatible with phpunit 5.x/6.x

out becomes our

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 75f0c02 and pushed to 8.4.x. Thanks!

  • alexpott committed 75f0c02 on 8.4.x
    Issue #2850797 by maxocub, dawehner, alexpott: Prepare our phpunit tests...
wim leers’s picture

FYI: if you're getting errors like this after updating to this commit or later:

$ php vendor/bin/phpunit -c core core/modules/rest/tests/src/Functional/EntityResource/Node/NodeJsonBasicAuthTest.php
PHP Fatal error:  Class 'PHPUnit\Framework\TestCase' not found in /Users/wim.leers/Work/d8/core/tests/Drupal/Tests/BrowserTestBase.php on line 45

Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /Users/wim.leers/Work/d8/core/tests/Drupal/Tests/BrowserTestBase.php on line 45
wim.leers at acquia in ~/Work/d8 on 8.4.x*

Then you should run composer install. Your composer dependencies are outdated. (Mine were, as you can tell.)

maxocub’s picture

I created the follow-up #2866894: Component tests should not use Drupal\Tests\UnitTestCase but PHPUnit\Framework\TestCase to remove all the usages of UnitTestCase in the component tests.

gábor hojtsy’s picture

Issue tags: +8.3.0 release notes

Adding to release notes.

dagmar’s picture

Issue tags: -8.3.0 release notes

@Gábor Hojtsy this seems to be only available for 8.4.x

gábor hojtsy’s picture

Uhm, indeed, sorry.

xjm’s picture

Issue tags: +8.4.0 release notes

:)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

maxocub’s picture