Skip to content

Commit 01dcc62

Browse files
committed
[VarExporter] Suppress deprecations for legacy fixtures
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 1a59dc5 commit 01dcc62

File tree

5 files changed

+85
-44
lines changed

5 files changed

+85
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 FooSerializable implements \Serializable
15+
{
16+
private $foo;
17+
18+
public function __construct(string $foo)
19+
{
20+
$this->foo = $foo;
21+
}
22+
23+
public function getFoo(): string
24+
{
25+
return $this->foo;
26+
}
27+
28+
public function serialize(): string
29+
{
30+
return serialize([$this->getFoo()]);
31+
}
32+
33+
public function unserialize($str)
34+
{
35+
[$this->foo] = unserialize($str);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 MySerializable implements \Serializable
15+
{
16+
public function serialize(): string
17+
{
18+
return '123';
19+
}
20+
21+
public function unserialize($data): void
22+
{
23+
// no-op
24+
}
25+
}

src/Symfony/Component/VarExporter/Tests/Fixtures/foo-serializable.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = \Symfony\Component\VarExporter\Internal\Registry::unserialize([], [
5-
'C:51:"Symfony\\Component\\VarExporter\\Tests\\FooSerializable":20:{a:1:{i:0;s:3:"bar";}}',
5+
'C:60:"Symfony\\Component\\VarExporter\\Tests\\Fixtures\\FooSerializable":20:{a:1:{i:0;s:3:"bar";}}',
66
]),
77
null,
88
[],

src/Symfony/Component/VarExporter/Tests/Fixtures/serializable.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = \Symfony\Component\VarExporter\Internal\Registry::unserialize([], [
5-
'C:50:"Symfony\\Component\\VarExporter\\Tests\\MySerializable":3:{123}',
5+
'C:59:"Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MySerializable":3:{123}',
66
]),
77
null,
88
[],

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

+21-42
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
1717
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
1818
use Symfony\Component\VarExporter\Internal\Registry;
19+
use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable;
1920
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
21+
use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable;
2022
use Symfony\Component\VarExporter\VarExporter;
2123

2224
class VarExporterTest extends TestCase
@@ -137,9 +139,26 @@ public function provideExport()
137139
yield ['array-iterator', new \ArrayIterator([123], 1)];
138140
yield ['array-object-custom', new MyArrayObject([234])];
139141

140-
$value = new MySerializable();
142+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
143+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead')) {
144+
// Skip the expected error.
145+
return true;
146+
}
147+
148+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
149+
});
141150

142-
yield ['serializable', [$value, $value]];
151+
try {
152+
$mySerializable = new MySerializable();
153+
$fooSerializable = new FooSerializable('bar');
154+
} finally {
155+
restore_error_handler();
156+
}
157+
158+
yield ['serializable', [$mySerializable, $mySerializable]];
159+
yield ['foo-serializable', $fooSerializable];
160+
161+
unset($mySerializable, $fooSerializable, $errorHandler);
143162

144163
$value = new MyWakeup();
145164
$value->sub = new MyWakeup();
@@ -211,8 +230,6 @@ public function provideExport()
211230

212231
yield ['abstract-parent', new ConcreteClass()];
213232

214-
yield ['foo-serializable', new FooSerializable('bar')];
215-
216233
yield ['private-constructor', PrivateConstructor::create('bar')];
217234

218235
yield ['php74-serializable', new Php74Serializable()];
@@ -223,19 +240,6 @@ public function provideExport()
223240
}
224241
}
225242

226-
class MySerializable implements \Serializable
227-
{
228-
public function serialize(): string
229-
{
230-
return '123';
231-
}
232-
233-
public function unserialize($data)
234-
{
235-
// no-op
236-
}
237-
}
238-
239243
class MyWakeup
240244
{
241245
public $sub;
@@ -384,31 +388,6 @@ public function __construct()
384388
}
385389
}
386390

387-
class FooSerializable implements \Serializable
388-
{
389-
private $foo;
390-
391-
public function __construct(string $foo)
392-
{
393-
$this->foo = $foo;
394-
}
395-
396-
public function getFoo(): string
397-
{
398-
return $this->foo;
399-
}
400-
401-
public function serialize(): string
402-
{
403-
return serialize([$this->getFoo()]);
404-
}
405-
406-
public function unserialize($str)
407-
{
408-
[$this->foo] = unserialize($str);
409-
}
410-
}
411-
412391
class Php74Serializable implements \Serializable
413392
{
414393
public function __serialize(): array

0 commit comments

Comments
 (0)