Skip to content

Commit 466fcac

Browse files
Replace __sleep/wakeup() by __(un)serialize() for throwing and internal usages
1 parent 4ff50a1 commit 466fcac

File tree

10 files changed

+192
-100
lines changed

10 files changed

+192
-100
lines changed

Tests/Fixtures/GoodNight.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class GoodNight
15+
{
16+
public $good;
17+
protected $foo;
18+
private $bar;
19+
20+
public function __construct()
21+
{
22+
unset($this->good);
23+
$this->foo = 'afternoon';
24+
$this->bar = 'morning';
25+
}
26+
27+
public function __sleep(): array
28+
{
29+
$this->good = 'night';
30+
31+
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
32+
}
33+
}

Tests/Fixtures/MyWakeup.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class MyWakeup
15+
{
16+
public $sub;
17+
public $bis;
18+
public $baz;
19+
public $def = 234;
20+
21+
public function __sleep(): array
22+
{
23+
return ['sub', 'baz'];
24+
}
25+
26+
public function __wakeup()
27+
{
28+
if (123 === $this->sub) {
29+
$this->bis = 123;
30+
$this->baz = 123;
31+
}
32+
}
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class Php74Serializable implements \Serializable
15+
{
16+
public $foo;
17+
18+
public function __serialize(): array
19+
{
20+
return [$this->foo = new \stdClass()];
21+
}
22+
23+
public function __unserialize(array $data)
24+
{
25+
[$this->foo] = $data;
26+
}
27+
28+
public function __sleep(): array
29+
{
30+
throw new \BadMethodCallException();
31+
}
32+
33+
public function __wakeup()
34+
{
35+
throw new \BadMethodCallException();
36+
}
37+
38+
public function serialize(): string
39+
{
40+
throw new \BadMethodCallException();
41+
}
42+
43+
public function unserialize($ser)
44+
{
45+
throw new \BadMethodCallException();
46+
}
47+
}

Tests/Fixtures/php74-serializable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Php74Serializable'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Php74Serializable')),
5+
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\Php74Serializable'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\Php74Serializable')),
66
clone ($p['stdClass'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('stdClass')),
77
],
88
null,

Tests/Fixtures/var-on-sleep.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\GoodNight'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\GoodNight')),
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight')),
66
],
77
null,
88
[
@@ -11,7 +11,7 @@
1111
'night',
1212
],
1313
],
14-
'Symfony\\Component\\VarExporter\\Tests\\GoodNight' => [
14+
'Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight' => [
1515
'foo' => [
1616
'afternoon',
1717
],

Tests/Fixtures/wakeup-refl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyWakeup')),
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup')),
66
],
77
null,
88
[],

Tests/Fixtures/wakeup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyWakeup')),
6-
clone $p['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'],
5+
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup')),
6+
clone $p['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'],
77
],
88
null,
99
[

Tests/LazyGhostTraitTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\VarExporter\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
16+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
1517
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1618
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
1719
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -30,6 +32,28 @@
3032
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\HookedWithDefaultValue;
3133
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;
3234

35+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
36+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'serialize()')) {
37+
// We're testing if the component handles deprecated Serializable and __sleep/wakeup implementations well.
38+
// This kind of implementation triggers a deprecation warning that we explicitly want to ignore here.
39+
return true;
40+
}
41+
42+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
43+
});
44+
45+
try {
46+
foreach ([
47+
MagicClass::class,
48+
ClassMetadata::class,
49+
AttributeMetadata::class,
50+
] as $class) {
51+
class_exists($class);
52+
}
53+
} finally {
54+
restore_error_handler();
55+
}
56+
3357
class LazyGhostTraitTest extends TestCase
3458
{
3559
public function testGetPublic()

Tests/LazyProxyTraitTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@
3030
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
3131
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;
3232

33+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
34+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'serialize()')) {
35+
// We're testing if the component handles deprecated Serializable and __sleep/wakeup implementations well.
36+
// This kind of implementation triggers a deprecation warning that we explicitly want to ignore here.
37+
return true;
38+
}
39+
40+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
41+
});
42+
43+
try {
44+
foreach ([
45+
TestWakeupClass::class,
46+
] as $class) {
47+
class_exists($class);
48+
}
49+
} finally {
50+
restore_error_handler();
51+
}
52+
3353
class LazyProxyTraitTest extends TestCase
3454
{
3555
public function testGetter()

Tests/VarExporterTest.php

Lines changed: 29 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,36 @@
2020
use Symfony\Component\VarExporter\Tests\Fixtures\FooReadonly;
2121
use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable;
2222
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
23+
use Symfony\Component\VarExporter\Tests\Fixtures\GoodNight;
2324
use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable;
25+
use Symfony\Component\VarExporter\Tests\Fixtures\MyWakeup;
26+
use Symfony\Component\VarExporter\Tests\Fixtures\Php74Serializable;
2427
use Symfony\Component\VarExporter\VarExporter;
2528

29+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
30+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'serialize()')) {
31+
// We're testing if the component handles deprecated Serializable and __sleep/wakeup implementations well.
32+
// This kind of implementation triggers a deprecation warning that we explicitly want to ignore here.
33+
return true;
34+
}
35+
36+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
37+
});
38+
39+
try {
40+
foreach ([
41+
MySerializable::class,
42+
FooSerializable::class,
43+
GoodNight::class,
44+
Php74Serializable::class,
45+
MyWakeup::class,
46+
] as $class) {
47+
class_exists($class);
48+
}
49+
} finally {
50+
restore_error_handler();
51+
}
52+
2653
class VarExporterTest extends TestCase
2754
{
2855
use VarDumperTestTrait;
@@ -142,23 +169,8 @@ public static function provideExport()
142169
yield ['array-iterator', new \ArrayIterator([123], 1)];
143170
yield ['array-object-custom', new MyArrayObject([234])];
144171

145-
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
146-
if (\E_DEPRECATED === $errno && str_contains($errstr, 'implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead')) {
147-
// We're testing if the component handles deprecated Serializable implementations well.
148-
// This kind of implementation triggers a deprecation warning since PHP 8.1 that we explicitly want to
149-
// ignore here. We probably need to reevaluate this piece of code for PHP 9.
150-
return true;
151-
}
152-
153-
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
154-
});
155-
156-
try {
157-
$mySerializable = new MySerializable();
158-
$fooSerializable = new FooSerializable('bar');
159-
} finally {
160-
restore_error_handler();
161-
}
172+
$mySerializable = new MySerializable();
173+
$fooSerializable = new FooSerializable('bar');
162174

163175
yield ['serializable', [$mySerializable, $mySerializable]];
164176
yield ['foo-serializable', $fooSerializable];
@@ -254,27 +266,6 @@ public function testUnicodeDirectionality()
254266
}
255267
}
256268

257-
class MyWakeup
258-
{
259-
public $sub;
260-
public $bis;
261-
public $baz;
262-
public $def = 234;
263-
264-
public function __sleep(): array
265-
{
266-
return ['sub', 'baz'];
267-
}
268-
269-
public function __wakeup()
270-
{
271-
if (123 === $this->sub) {
272-
$this->bis = 123;
273-
$this->baz = 123;
274-
}
275-
}
276-
}
277-
278269
class MyCloneable
279270
{
280271
public function __clone()
@@ -337,27 +328,6 @@ public function setFlags($flags): void
337328
}
338329
}
339330

340-
class GoodNight
341-
{
342-
public $good;
343-
protected $foo;
344-
private $bar;
345-
346-
public function __construct()
347-
{
348-
unset($this->good);
349-
$this->foo = 'afternoon';
350-
$this->bar = 'morning';
351-
}
352-
353-
public function __sleep(): array
354-
{
355-
$this->good = 'night';
356-
357-
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
358-
}
359-
}
360-
361331
final class FinalError extends \Error
362332
{
363333
public function __construct(bool $throw = true)
@@ -413,41 +383,6 @@ public function __construct()
413383
}
414384
}
415385

416-
class Php74Serializable implements \Serializable
417-
{
418-
public $foo;
419-
420-
public function __serialize(): array
421-
{
422-
return [$this->foo = new \stdClass()];
423-
}
424-
425-
public function __unserialize(array $data)
426-
{
427-
[$this->foo] = $data;
428-
}
429-
430-
public function __sleep(): array
431-
{
432-
throw new \BadMethodCallException();
433-
}
434-
435-
public function __wakeup()
436-
{
437-
throw new \BadMethodCallException();
438-
}
439-
440-
public function serialize(): string
441-
{
442-
throw new \BadMethodCallException();
443-
}
444-
445-
public function unserialize($ser)
446-
{
447-
throw new \BadMethodCallException();
448-
}
449-
}
450-
451386
#[\AllowDynamicProperties]
452387
class ArrayObject extends \ArrayObject
453388
{

0 commit comments

Comments
 (0)