8000 [VarExporter] Suppress deprecations for legacy fixtures by derrabus · Pull Request #42782 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarExporter] Suppress deprecations for legacy fixtures #42782

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[],
Expand Down
65 changes: 23 additions & 42 deletions src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()];
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
0