bug #38433 [Mime] Fix serialization of RawMessage (gilbertsoft)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Mime] Fix serialization of RawMessage

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #38430, Related #33394 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | - <!-- required for new features -->

The serialization of RawMessage is currently broken if using a generator for message like done by `Symfony\Component\Mailer\SentMessage` see 5f1c3a7972/src/Symfony/Component/Mailer/SentMessage.php (L45)
This patch converts the message to a string so further serialization can be done.

This patch probably also solves #33394.

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch 5.x.
-->

Commits
-------

fd99eb26d8 [Mime] Fix serialization of RawMessage
This commit is contained in:
Fabien Potencier 2020-10-06 16:40:04 +02:00
commit 87f328428e
2 changed files with 15 additions and 1 deletions

View File

@ -78,7 +78,7 @@ class RawMessage implements \Serializable
public function __serialize(): array
{
return [$this->message];
return [$this->toString()];
}
public function __unserialize(array $data): void

View File

@ -32,4 +32,18 @@ class RawMessageTest extends TestCase
$this->assertEquals('some string', $message->toString());
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
public function testSerialization()
{
$message = new RawMessage('string');
$this->assertEquals('string', unserialize(serialize($message))->toString());
// calling methods more than once work
$this->assertEquals('string', unserialize(serialize($message))->toString());
$message = new RawMessage(new \ArrayObject(['some', ' ', 'string']));
$message = new RawMessage($message->toIterable());
$this->assertEquals('some string', unserialize(serialize($message))->toString());
// calling methods more than once work
$this->assertEquals('some string', unserialize(serialize($message))->toString());
}
}