Skip to content

Commit

Permalink
Merge pull request #11065 from kerbert101/fix-sleep-buhh
Browse files Browse the repository at this point in the history
AbstractSqlExecutor::__sleep should return property names
  • Loading branch information
greg0ire authored Nov 17, 2023
2 parents 0a7b939 + 6be65eb commit 1a4fe6e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Query/Exec/AbstractSqlExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

use function array_diff;
use function array_keys;
use function array_map;
use function array_values;
use function str_replace;

/**
* Base class for SQL statement executors.
Expand Down Expand Up @@ -84,7 +86,9 @@ public function __sleep(): array
serialized representation becomes compatible with 3.0.x, meaning
there will not be a deprecation warning about a missing property
when unserializing data */
return array_values(array_diff(array_keys((array) $this), ["\0*\0_sqlStatements"]));
return array_values(array_diff(array_map(static function (string $prop): string {
return str_replace("\0*\0", '', $prop);
}, array_keys((array) $this)), ['_sqlStatements']));
}

public function __wakeup(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Closure;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Exec\SingleSelectExecutor;
use Doctrine\ORM\Query\ParserResult;
Expand All @@ -12,6 +13,8 @@
use Generator;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Component\VarExporter\Instantiator;
use Symfony\Component\VarExporter\VarExporter;

use function file_get_contents;
use function rtrim;
Expand All @@ -27,21 +30,46 @@ protected function setUp(): void
parent::setUp();
}

public function testSerializeParserResult(): void
/**
* @param Closure(ParserResult): ParserResult $toSerializedAndBack
*
* @dataProvider provideToSerializedAndBack
*/
public function testSerializeParserResult(Closure $toSerializedAndBack): void
{
$query = $this->_em
->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u.name = :name');

$parserResult = self::parseQuery($query);
$serialized = serialize($parserResult);
$unserialized = unserialize($serialized);
$unserialized = $toSerializedAndBack($parserResult);

$this->assertInstanceOf(ParserResult::class, $unserialized);
$this->assertInstanceOf(ResultSetMapping::class, $unserialized->getResultSetMapping());
$this->assertEquals(['name' => [0]], $unserialized->getParameterMappings());
$this->assertInstanceOf(SingleSelectExecutor::class, $unserialized->getSqlExecutor());
}

/** @return Generator<string, array{Closure(ParserResult): ParserResult}> */
public function provideToSerializedAndBack(): Generator
{
yield 'native serialization function' => [
static function (ParserResult $parserResult): ParserResult {
return unserialize(serialize($parserResult));
},
];

$instantiatorMethod = new ReflectionMethod(Instantiator::class, 'instantiate');
if ($instantiatorMethod->getReturnType() === null) {
$this->markTestSkipped('symfony/var-exporter 5.4+ is required.');
}

yield 'symfony/var-exporter' => [
static function (ParserResult $parserResult): ParserResult {
return eval('return ' . VarExporter::export($parserResult) . ';');
},
];
}

public function testItSerializesParserResultWithAForwardCompatibleFormat(): void
{
$query = $this->_em
Expand Down

0 comments on commit 1a4fe6e

Please sign in to comment.