Problem/Motivation

Discovered in #3063887: Support PHPUnit 8 in Drupal 9, drop support for PHPUnit 7.

Drupal\Tests\Traits\ExpectDeprecationTrait implements

protected function expectDeprecation($message)

This method has a name collision with a PHPUnit 8 method on PHPUnit\Framework\TestCase that has a different signature and a different purpose.

This will prevent future usage of PHPUnit 8.

Proposed resolution

  • Rename expectDeprecation to something else: addExpectedDeprecationMessage
  • Deprecate expectDeprecation
  • Add/adjust tests

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mondrake created an issue. See original summary.

ravi.shankar’s picture

Status: Active » Needs review
FileSize
13.22 KB

I have tried to fix this issue, here is a patch please review it.

mondrake’s picture

Status: Needs review » Needs work

Thanks, I think we cannot just rename the method, we should rather add the new method, and deprecate the old one - see Drupal core deprecation policy. The old method should then just call the new one.

+++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
@@ -25,7 +25,7 @@ trait ExpectDeprecationTrait {
    * @param string $message
    *   The expected deprecation message.
    */
-  protected function expectDeprecation($message) {
+  protected function addExpectedDeprecationMessage($message) {
     $this->expectedDeprecations([$message]);
   }
 
mondrake’s picture

Issue tags: +Drupal 9
mondrake’s picture

Title: ExpectDeprecationTrait is not compatible with PHPUnit8 » ExpectDeprecationTrait is not compatible with PHPUnit 8
longwave’s picture

PHPUnit 8 has expectDeprecationMessage(), should we use the same method name?

mondrake’s picture

@longwave no I don’t think so, we should have a totally different method name here IMHO

Looks like the PHPUnit 8 expectDeprecation* methods overlap with the current implementation of Phpunit-bridge in Phpunit 6 and 7, which is not going to be changed I suppose until those versions won’t have been discontinued in Drupal

I tried renaming to that in the parent issue at some point, but it did not work because of the above - the call is not intercepted by our own implementation

mondrake’s picture

Issue tags: +PHPUnit 8
mondrake’s picture

Status: Needs work » Reviewed & tested by the community

Re #3, ExpectDeprecationTrait is actually marked @internal with the comment

This class should only be used by Drupal core and will be removed once https://github.com/symfony/symfony/pull/25757 is resolved.

, so I think it is OK in this case to proceed without a deprecation.

mondrake’s picture

Issue summary: View changes
mondrake’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
@@ -25,7 +25,7 @@ trait ExpectDeprecationTrait {
-  protected function expectDeprecation($message) {
+  protected function addExpectedDeprecationMessage($message) {

This will break existing code. I think we need to do something more BC friendly here. I think we could make our method conform to the parent and offer a BC layer.

mondrake’s picture

#12 not sure how we could be BC here: the method in PHPUnit 8 is

    public function expectDeprecation(): void
    {
        $this->expectException(Deprecated::class);
    }

so trying to pass a $message argument would break the method signature. Can we overload and use func_get_args() to retrieve the argument passed in?

alexpott’s picture

@mondrake you can add extra args... the problem is the slightly different functionality.

mondrake’s picture

Well I suppose we'd just bypass PHPUnit 8 behavior and make the call to

$this->expectedDeprecations([$message]);

without chaining further to parent::expectDeprecation, right? So that the PHPUnit-bridge thingie keeps working as per BC.

alexpott’s picture

Well at some point PHPUnit is going to take on more of what Symfony's simple phpunit is doing wrt to deprecation testing - hence this method so we might want to give ourselves some wiggle room. I think it might be okay to trigger an unsilenced deprecation and have a new non-clashing method name. So we can move to the new method.

mondrake’s picture

Assigned: Unassigned » mondrake

OK I just tried to use the PHPUnit compatibility layer to have a version dependent trait here so we can have some space to play. See #3063887-34: Support PHPUnit 8 in Drupal 9, drop support for PHPUnit 7 and #35. If that works I'll extract and post here.

mondrake’s picture

#14 is not possible, https://3v4l.org/cB0hY. Working on #16.

longwave’s picture

@mondrake what about https://3v4l.org/TAEae?

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
FileSize
19.11 KB
6.63 KB

@longwave good catch, however now I tend to think deprecation + new method is better fit, per #16. Here's a patch.

alexpott’s picture

  1. +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit6/ExpectDeprecationTrait.php
    @@ -0,0 +1,34 @@
    +      @trigger_error('ExpectDeprecationTrait::expectDeprecation is deprecated in drupal:8.9.0 and is removed from drupal:9.0.0. Use ::addExpectedDeprecationMessage() instead. See https://www.drupal.org/node/3106024', E_USER_DEPRECATED);
    
    +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit7/ExpectDeprecationTrait.php
    @@ -0,0 +1,34 @@
    +      @trigger_error('ExpectDeprecationTrait::expectDeprecation is deprecated in drupal:8.9.0 and is removed from drupal:9.0.0. Use ::addExpectedDeprecationMessage() instead. See https://www.drupal.org/node/3106024', E_USER_DEPRECATED);
    

    I don't this should be silenced. Also I think the fact that we've not changed any calls to expectDeprecation shows that this is not quite working as expected.

  2. +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit8/ExpectDeprecationTrait.php
    @@ -0,0 +1,18 @@
    +trait ExpectDeprecationTrait {
    +
    +    // @todo remove in Drupal 9.
    +
    +}
    

    This needs to do forwards-compatible logic

mondrake’s picture

@alexpott re #21:

1. I do not understand this comment. Maybe you reviewed the interdiff instead of the patch? All calls to expectDeprecation are changed, but one in a legacy test that throws the trigger_error.
2. That one is out of scope here and we should remove, it would be in scope for #3063887: Support PHPUnit 8 in Drupal 9, drop support for PHPUnit 7.

mondrake’s picture

alexpott’s picture

+++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit8/ExpectDeprecationTrait.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit8;
+
+/**
+ * Adds the ability to dynamically set expected deprecation messages in tests.
+ *
+ * @internal
+ *   This class should only be used by Drupal core and will be removed once
+ *   https://github.com/symfony/symfony/pull/25757 is resolved.
+ *
+ * @todo Remove once https://github.com/symfony/symfony/pull/25757 is resolved.
+ */
+trait ExpectDeprecationTrait {
+
+    // @todo remove in Drupal 9.
+
+}

Without a forwards compatibility layer tests that use expectDeprecation in Drupal are not testing what they think they are testing.

mondrake’s picture

Priority: Normal » Critical
Status: Needs review » Needs work
alexpott’s picture

  1. +++ b/core/tests/Drupal/Tests/ExpectDeprecationTest.php
    @@ -14,23 +15,41 @@ class ExpectDeprecationTest extends UnitTestCase {
    +      $this->addExpectedDeprecationMessage('ExpectDeprecationTrait::expectDeprecation is deprecated in drupal:8.9.0 and is removed from drupal:9.0.0. Use ::addExpectedDeprecationMessage() instead. See https://www.drupal.org/node/3106024');
    

    I think we should add this deprecation to 8.8.x - but worth getting a +1 from a release manager.

  2. +++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
    @@ -2,12 +2,20 @@
    +// In order to manage different method signatures between PHPUnit versions, we
    +// dynamically load a compatibility trait dependent on the PHPUnit runner
    +// version.
    +if (!trait_exists(PhpunitVersionDependentExpectDeprecationTrait::class, FALSE)) {
    +  class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit" . RunnerVersion::getMajor() . "\ExpectDeprecationTrait", PhpunitVersionDependentExpectDeprecationTrait::class);
    +}
    
    @@ -19,13 +27,15 @@
    -  protected function expectDeprecation($message) {
    +  protected function addExpectedDeprecationMessage($message) {
    

    I don't think we need to the do the different version dance here - we can just trigger the deprecation from the method and be done - less complexity.

catch’s picture

I think adding the deprecation is OK in the test system in 8.8.x, we're just communicating something that has to happen.

mondrake’s picture

Assigned: Unassigned » mondrake

Working on #26.2.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
FileSize
14.58 KB
7 KB

Here we go. Note this is a Drupal 8.x only patch.

Lendude’s picture

Status: Needs review » Needs work

I have to say that I'm entirely unconvinced by the need for a BC layer, as pointed out in #9

 * @internal
 *   This class should only be used by Drupal core and will be removed once
 *   https://github.com/symfony/symfony/pull/25757 is resolved.

Honestly, if that doesn't signal "DON'T USE THIS", what point is there to marking anything @internal.

But we have one now, so ¯\_(ツ)_/¯
Can we get a quick update to mark it deprecated in 8.8.x per #26.1?

dhirendra.mishra’s picture

longwave’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 31: 3094151-31.patch, failed testing. View results

swatichouhan012’s picture

Status: Needs work » Needs review
FileSize
14.63 KB
958 bytes

Updated deprecated message in test files wrt to patch #31, kindly review

longwave’s picture

Status: Needs review » Needs work
+++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
@@ -25,10 +25,28 @@
+      @trigger_error('ExpectDeprecationTrait::expectDeprecation is deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use ::addExpectedDeprecationMessage() instead. See https://www.drupal.org/node/3106024', E_USER_DEPRECATED);

8.8.x needs to be the exact version this was added - so this should be 8.8.4 as that will be the next release.

swatichouhan012’s picture

Assigned: Unassigned » swatichouhan012
swatichouhan012’s picture

Status: Needs work » Needs review
FileSize
14.63 KB
1.9 KB

@longwave i have updated version to 8.8.4, kindly review new patch

swatichouhan012’s picture

Assigned: swatichouhan012 » Unassigned
longwave’s picture

Status: Needs review » Needs work
  1. +++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
    @@ -25,10 +25,28 @@
    +   * @deprecated in drupal:8.9.0 and is removed from drupal:9.0.0. Use
    +   *   ::addExpectedDeprecationMessage() instead.
    

    This also needs to say 8.8.4 instead of 8.9.0.

  2. +++ b/core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
    @@ -25,10 +25,28 @@
    +    if (strpos($message, 'ExpectDeprecationTrait::expectDeprecation') === FALSE) {
    

    Not sure what this is guarding against, as I don't see how it can be called this way, but I also guess it can't hurt.

mondrake’s picture

@longwave re #39.2 - it's there to prevent endless recursion.

longwave’s picture

OK I see - so let's just fix #39.1 and this is ready to go in I think.

swatichouhan012’s picture

Status: Needs work » Needs review
FileSize
14.63 KB
599 bytes

Updated patchh wrt comment #39.1, kindly review

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, let's try to get this in before commit freeze!

  • alexpott committed 71a80e7 on 8.9.x
    Issue #3094151 by mondrake, swatichouhan012, dhirendra.mishra, ravi....

  • alexpott committed 9c0bf60 on 8.8.x
    Issue #3094151 by mondrake, swatichouhan012, dhirendra.mishra, ravi....
alexpott’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Fixed

AS #42 is only changing comments I think this is safe to commit before it comes back green and is worth getting into 8.8.x asap.

Committed and pushed 71a80e785a to 8.9.x and 9c0bf60beb to 8.8.x. Thanks!

Status: Fixed » Closed (fixed)

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