[PHPUnit 10] Provide a static alternative to @dataproviders using PHPUnit mocks in ProtectedUserFieldConstraintValidatorTest

Created on 23 June 2023, about 1 year ago
Updated 8 May 2024, 4 months ago

Problem/Motivation

PHPUnit 10 deprecates use of non-static @dataProvider methods. This means that @dataProvider methods can no longer make calls to non-static methods. Any non-static call ($this->someMethod()) needs to be replaced by a call to a static alternative (static::someOtherMethod()), that would have to be implemented if not available.

In this issue, focus on replacing usage of PHPUnit mocks in ProtectedUserFieldConstraintValidatorTest

Proposed resolution

Following up on 📌 [PHPUnit 10] Provide a static alternative to ConfigMapperManagerTest::providerTestHasTranslatable Fixed , replace usage of PHPUnit mocks with PHPSpec prophecies instead, wherever possible.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

10.3

Component
PHPUnit 

Last updated about 11 hours ago

Created by

🇮🇹Italy mondrake 🇮🇹

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mondrake
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    29,487 pass, 21 fail
  • 🇮🇹Italy mondrake 🇮🇹

    In general, Prophecy does not allow mocking magic methods.

    PHPUnit mocks would, but in this case it seemed to me that

    +++ b/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php
    @@ -69,241 +74,130 @@ public function testValidate($items, $expected_violation, $name = FALSE) {
    -    $items->expects($this->any())
    -      ->method('getValue')
    -      ->willReturn('changed-value');
    -    $items->expects($this->once())
    -      ->method('__get')
    -      ->with('value')
    -      ->willReturn('changed-value');
    

    ->getValue() and ->value are totally overlapping.

    So here I removed the expectation to access ->value via __get, leaving only the one for ->getValue().

    But, this has an impact on runtime code that needs to be assessed:

    +++ b/core/modules/user/src/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidator.php
    @@ -83,7 +83,7 @@ public function validate($items, Constraint $constraint) {
           // Special case for the password, it being empty means that the existing
           // password should not be changed, ignore empty password fields.
    -      $value = $items->value;
    +      $value = $items->getValue();
           if ($field->getName() != 'pass' || !empty($value)) {
             // Compare the values of the field this is being validated on.
             $changed = $items->getValue() != $account_unchanged->get($field->getName())->getValue();
    
  • Status changed to Needs work 5 months ago
  • 🇬🇧United Kingdom longwave UK
  • Status changed to Needs review 5 months ago
  • 🇬🇧United Kingdom longwave UK

    Couldn't see any easy way of doing this one as the mocks are all so complex, so converted the data provider into the test method itself. Thought about splitting it up into 11 separate test methods, but doesn't seem worth the effort.

  • Pipeline finished with Success
    5 months ago
    Total: 957s
    #154789
  • 🇺🇸United States smustgrave

    should validate() be protected

  • 🇬🇧United Kingdom longwave UK

    Thanks, implemented #8.

  • 🇮🇹Italy mondrake 🇮🇹
  • Status changed to RTBC 5 months ago
  • 🇺🇸United States smustgrave

    Rest looks good to me.

  • Pipeline finished with Success
    5 months ago
    Total: 3323s
    #155468
  • 🇮🇹Italy mondrake 🇮🇹

    Looks good.

    Nit - I think it would be more readable if instead of

    protected function validate($items, string $name = NULL): void

    it were

    protected function assertValidation($items, string $constraintMessageOnValidationFailure = NULL): void

    But it's just me having had the difficulty to understand what was going on.

    • catch committed 9898e1ed on 10.3.x
      Issue #3369099 by longwave, mondrake, smustgrave: [PHPUnit 10] Provide a...
    • catch committed bceee809 on 11.x
      Issue #3369099 by longwave, mondrake, smustgrave: [PHPUnit 10] Provide a...
  • Status changed to Fixed 5 months ago
  • 🇬🇧United Kingdom catch

    Looks good. Took me a minute to realise we're just doing away with a test provider here, good idea.

    Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

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

Production build 0.71.5 2024