uuid
uuid copied to clipboard
UuidInterface generates deprecated message with PHP 8.1
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
possible fix
Add to UuidInterface
public function __serialize(): array;
public function __unserialize(array $data): void;
This will require a major version bump, since it's a change to an interface. 😕
@ramsey maybe this ticket should be closed and new one should be opened with 'Php 8.1 compatibility' subject?
I'm not following. Why does this need a new ticket?
Seems to me a lot of things will be changed to be compatible with PHP 8.1. Not only interface fixing but also the phpdocs for phpstan. That’s why I’ve suggested a new ticket.
Of course everything could be done in this ticket :)
Best regards, Dmytro Kovalenko @.*** 18 янв. 2022 г., 23:25 +0100, Ben Ramsey @.***>, писал:
I'm not following. Why does this need a new ticket? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>
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.
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.
@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.
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.
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.
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.
@ramsey Agree with you. Unfortunately, I need to do this override, because my project have too many (thousands) test-cases that uses mocked UuidInterface :(
Can't you just bulk replace $this->getMockBuilder(UuidInterface::class)->disableOriginalConstructor()->getMock()
with Uuid::random()
or something?
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? )))