Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UuidInterface generates deprecated message with PHP 8.1 #399

Closed
jokaorgua opened this issue Jan 18, 2022 · 14 comments
Closed

UuidInterface generates deprecated message with PHP 8.1 #399

jokaorgua opened this issue Jan 18, 2022 · 14 comments
Labels

Comments

@jokaorgua
Copy link

jokaorgua commented Jan 18, 2022

UuidInterface generates deprecated messasge when using in PHP 8.1

Description

Current UuidInterface implementation is

interface UuidInterface extends
    DeprecatedUuidInterface,
    JsonSerializable,
    Serializable

and it generates deprecation message

Mock_UuidInterface_e896a8df implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary)

Steps to reproduce

Mock an UuidInterface with phpunit mock

$this->getMockBuilder(UuidInterface::class)->disableOriginalConstructor()->getMock();

Expected behavior

No deprecation message

Environment details

php: 8.1.1
OS: Linux
Arch: Arm

@jokaorgua jokaorgua added the bug label Jan 18, 2022
@jokaorgua
Copy link
Author

jokaorgua commented Jan 18, 2022

possible fix

Add to UuidInterface

    public function __serialize(): array;

    public function __unserialize(array $data): void;

@ramsey
Copy link
Owner

ramsey commented Jan 18, 2022

This will require a major version bump, since it's a change to an interface. 😕

@jokaorgua
Copy link
Author

@ramsey maybe this ticket should be closed and new one should be opened with 'Php 8.1 compatibility' subject?

@ramsey
Copy link
Owner

ramsey commented Jan 18, 2022

I'm not following. Why does this need a new ticket?

@jokaorgua
Copy link
Author

jokaorgua commented Jan 19, 2022 via email

@agustingomes
Copy link

agustingomes commented Jan 19, 2022

This will require a major version bump, since it's a change to an interface.

Hi @ramsey

Wouldn't using #[\ReturnTypeWillChange] suffice in this case?

Edit: nevermind, initially I assumed it was because the return types, I see now is tied to the Serializable interface.

@ramsey
Copy link
Owner

ramsey commented Jan 19, 2022

The request is to add it to UuidInterface. Any change to the interface requires a major version bump.

#[\ReturnTypeWillChange] won't fix deprecation warnings for Serializable, which is an interface that's going away.

@andrew-demb
Copy link

@jokaorgua probably this can be fixed only in PHPUnit (which generates mock implementation without __serialize/__unserialize methods)

Introducing new methods to the interface would be the wrong choice.

@ramsey
Copy link
Owner

ramsey commented Dec 19, 2022

I'm going to close this issue, since I don't think there's anything that can be done about it in version 4.x of ramsey/uuid.

@ramsey ramsey closed this as completed Dec 19, 2022
@acelot
Copy link

acelot commented Jun 18, 2023

I've found a convenient way to make UuidInterface mockable for PHP 8.1. You can override the original UuidInterface by yours via composer. Just add these two lines in your composer.json:

"autoload-dev": {
    "psr-4": {
        <...>
    },
    "exclude-from-classmap": ["vendor/ramsey/uuid/src/UuidInterface.php"],
    "files": ["src/overrides/ramsey/uuid/UuidInterface.php"]
}

Then copy the original UuidInterface into src/overrides/ramsey/uuid/UuidInterface.php file and append Serializable interface methods:

public function __serialize(): array;

public function __unserialize(array $data): void;

Do not forget to call composer dump-autoload at the end.

@ramsey
Copy link
Owner

ramsey commented Jun 19, 2023

My stance on this matter is that there is no need to mock a UUID. If you need the UUID to have a fixed value, you can create one using Uuid::fromString() to pass to your SUT. If your SUT generates the UUID inside the unit, you shouldn't need to replace it with a mock. Instead, in whatever value your SUT returns, you can assert the UUID property/method/etc. has an instance of Ramsey\Uuid\UuidInterface. That should be enough to validate the behavior.

@acelot
Copy link

acelot commented Jun 19, 2023

@ramsey Agree with you. Unfortunately, I need to do this override, because my project have too many (thousands) test-cases that uses mocked UuidInterface :(

@enumag
Copy link
Contributor

enumag commented Jun 19, 2023

Can't you just bulk replace $this->getMockBuilder(UuidInterface::class)->disableOriginalConstructor()->getMock() with Uuid::random() or something?

@acelot
Copy link

acelot commented Jun 19, 2023

Sure, it's possible. But first you need to set up a rule so that other developers do not write in the old way.

P.S. Random values in test-cases? )))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants