From 2db2c009438cfd22f729dcd67248eda52408d606 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 29 Aug 2021 21:59:18 +0200 Subject: [PATCH] [VarExporter] Suppress deprecations for legacy fixtures Signed-off-by: Alexander M. Turek --- .../Tests/Fixtures/FooSerializable.php | 37 +++++++++++ .../Tests/Fixtures/MySerializable.php | 25 +++++++ .../Tests/Fixtures/foo-serializable.php | 2 +- .../Tests/Fixtures/serializable.php | 2 +- .../VarExporter/Tests/VarExporterTest.php | 65 +++++++------------ 5 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/FooSerializable.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/MySerializable.php diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/FooSerializable.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooSerializable.php new file mode 100644 index 0000000000000..63d9fd230c790 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooSerializable.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarExporter\Tests\Fixtures; + +class FooSerializable implements \Serializable +{ + private $foo; + + public function __construct(string $foo) + { + $this->foo = $foo; + } + + public function getFoo(): string + { + return $this->foo; + } + + public function serialize(): string + { + return serialize([$this->getFoo()]); + } + + public function unserialize($str) + { + [$this->foo] = unserialize($str); + } +} diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/MySerializable.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/MySerializable.php new file mode 100644 index 0000000000000..8d649a22a944d --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/MySerializable.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarExporter\Tests\Fixtures; + +class MySerializable implements \Serializable +{ + public function serialize(): string + { + return '123'; + } + + public function unserialize($data): void + { + // no-op + } +} diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/foo-serializable.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/foo-serializable.php index fd4e2671010b3..c6285fd1fbebe 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/foo-serializable.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/foo-serializable.php @@ -2,7 +2,7 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( $o = \Symfony\Component\VarExporter\Internal\Registry::unserialize([], [ - 'C:51:"Symfony\\Component\\VarExporter\\Tests\\FooSerializable":20:{a:1:{i:0;s:3:"bar";}}', + 'C:60:"Symfony\\Component\\VarExporter\\Tests\\Fixtures\\FooSerializable":20:{a:1:{i:0;s:3:"bar";}}', ]), null, [], diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/serializable.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/serializable.php index fcf32278b0685..9e7b52803d924 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/serializable.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/serializable.php @@ -2,7 +2,7 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( $o = \Symfony\Component\VarExporter\Internal\Registry::unserialize([], [ - 'C:50:"Symfony\\Component\\VarExporter\\Tests\\MySerializable":3:{123}', + 'C:59:"Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MySerializable":3:{123}', ]), null, [], diff --git a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php index cd10c7af37a8a..c10a87761ce8c 100644 --- a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php +++ b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php @@ -16,7 +16,9 @@ use Symfony\Component\VarExporter\Exception\ClassNotFoundException; use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException; use Symfony\Component\VarExporter\Internal\Registry; +use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable; use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum; +use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable; use Symfony\Component\VarExporter\VarExporter; class VarExporterTest extends TestCase @@ -137,9 +139,28 @@ public function provideExport() yield ['array-iterator', new \ArrayIterator([123], 1)]; yield ['array-object-custom', new MyArrayObject([234])]; - $value = new MySerializable(); + $errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) { + if (\E_DEPRECATED === $errno && str_contains($errstr, 'implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead')) { + // We're testing if the component handles deprecated Serializable implementations well. + // This kind of implementation triggers a deprecation warning since PHP 8.1 that we explicitly want to + // ignore here. We probably need to reevaluate this piece of code for PHP 9. + return true; + } + + return $errorHandler ? $errorHandler(...\func_get_args()) : false; + }); - yield ['serializable', [$value, $value]]; + try { + $mySerializable = new MySerializable(); + $fooSerializable = new FooSerializable('bar'); + } finally { + restore_error_handler(); + } + + yield ['serializable', [$mySerializable, $mySerializable]]; + yield ['foo-serializable', $fooSerializable]; + + unset($mySerializable, $fooSerializable, $errorHandler); $value = new MyWakeup(); $value->sub = new MyWakeup(); @@ -211,8 +232,6 @@ public function provideExport() yield ['abstract-parent', new ConcreteClass()]; - yield ['foo-serializable', new FooSerializable('bar')]; - yield ['private-constructor', PrivateConstructor::create('bar')]; yield ['php74-serializable', new Php74Serializable()]; @@ -223,19 +242,6 @@ public function provideExport() } } -class MySerializable implements \Serializable -{ - public function serialize(): string - { - return '123'; - } - - public function unserialize($data) - { - // no-op - } -} - class MyWakeup { public $sub; @@ -384,31 +390,6 @@ public function __construct() } } -class FooSerializable implements \Serializable -{ - private $foo; - - public function __construct(string $foo) - { - $this->foo = $foo; - } - - public function getFoo(): string - { - return $this->foo; - } - - public function serialize(): string - { - return serialize([$this->getFoo()]); - } - - public function unserialize($str) - { - [$this->foo] = unserialize($str); - } -} - class Php74Serializable implements \Serializable { public function __serialize(): array