From 88cc3af6076927fd299b34d3e5c1ce35feb5b084 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 21 Feb 2022 15:35:47 +0100 Subject: [PATCH] [VarDumper] Fix dumping mysqli_driver instances --- .../VarDumper/Caster/MysqliCaster.php | 33 ++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 2 + .../Tests/Caster/MysqliCasterTest.php | 39 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Caster/MysqliCaster.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/MysqliCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/MysqliCaster.php b/src/Symfony/Component/VarDumper/Caster/MysqliCaster.php new file mode 100644 index 0000000000000..bfe6f0822d65a --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/MysqliCaster.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Caster; + +use Symfony\Component\VarDumper\Cloner\Stub; + +/** + * @author Nicolas Grekas + * + * @internal + */ +final class MysqliCaster +{ + public static function castMysqliDriver(\mysqli_driver $c, array $a, Stub $stub, bool $isNested): array + { + foreach ($a as $k => $v) { + if (isset($c->$k)) { + $a[$k] = $c->$k; + } + } + + return $a; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index eeac88295399e..d2e5039b8daf1 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -144,6 +144,8 @@ abstract class AbstractCloner implements ClonerInterface 'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'], 'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'], + 'mysqli_driver' => ['Symfony\Component\VarDumper\Caster\MysqliCaster', 'castMysqliDriver'], + 'CurlHandle' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], ':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/MysqliCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/MysqliCasterTest.php new file mode 100644 index 0000000000000..e05ae41b1b19f --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/MysqliCasterTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +/** + * @requires extension mysqli + * @group integration + */ +class MysqliCasterTest extends TestCase +{ + use VarDumperTestTrait; + + public function testNotConnected() + { + $driver = new \mysqli_driver(); + $driver->report_mode = 3; + + $xCast = <<assertDumpMatchesFormat($xCast, $driver); + } +}