|
19 | 19 | use Symfony\Component\Mime\Part\Multipart\MixedPart;
|
20 | 20 | use Symfony\Component\Mime\Part\Multipart\RelatedPart;
|
21 | 21 | use Symfony\Component\Mime\Part\TextPart;
|
| 22 | +use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
| 23 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 24 | +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 25 | +use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer; |
| 26 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 27 | +use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
| 28 | +use Symfony\Component\Serializer\Serializer; |
22 | 29 |
|
23 | 30 | class EmailTest extends TestCase
|
24 | 31 | {
|
@@ -384,4 +391,71 @@ public function testSerialize()
|
384 | 391 | $this->assertEquals($expected->getHeaders(), $n->getHeaders());
|
385 | 392 | $this->assertEquals($e->getBody(), $n->getBody());
|
386 | 393 | }
|
| 394 | + |
| 395 | + public function testSymfonySerialize() |
| 396 | + { |
| 397 | + // we don't add from/sender to check that validation is not triggered to serialize an email |
| 398 | + $e = new Email(); |
| 399 | + $e->to('you@example.com'); |
| 400 | + $e->text('Text content'); |
| 401 | + $e->html('HTML <b>content</b>'); |
| 402 | + $e->attach('Some Text file', 'test.txt'); |
| 403 | + $expected = clone $e; |
| 404 | + |
| 405 | + $expectedJson = <<<EOF |
| 406 | +{ |
| 407 | + "text": "Text content", |
| 408 | + "textCharset": "utf-8", |
| 409 | + "html": "HTML <b>content</b>", |
| 410 | + "htmlCharset": "utf-8", |
| 411 | + "attachments": [ |
| 412 | + { |
| 413 | + "body": "Some Text file", |
| 414 | + "name": "test.txt", |
| 415 | + "content-type": null, |
| 416 | + "inline": false |
| 417 | + } |
| 418 | + ], |
| 419 | + "headers": { |
| 420 | + "to": [ |
| 421 | + { |
| 422 | + "addresses": [ |
| 423 | + { |
| 424 | + "address": "you@example.com", |
| 425 | + "name": "" |
| 426 | + } |
| 427 | + ], |
| 428 | + "name": "To", |
| 429 | + "lineLength": 76, |
| 430 | + "lang": null, |
| 431 | + "charset": "utf-8" |
| 432 | + } |
| 433 | + ] |
| 434 | + }, |
| 435 | + "body": null, |
| 436 | + "message": null |
| 437 | +} |
| 438 | +EOF; |
| 439 | + |
| 440 | + $extractor = new PhpDocExtractor(); |
| 441 | + $propertyNormalizer = new PropertyNormalizer(null, null, $extractor); |
| 442 | + $serializer = new Serializer([ |
| 443 | + new ArrayDenormalizer(), |
| 444 | + new MimeMessageNormalizer($propertyNormalizer), |
| 445 | + new ObjectNormalizer(null, null, null, $extractor), |
| 446 | + $propertyNormalizer, |
| 447 | + ], [new JsonEncoder()]); |
| 448 | + |
| 449 | + $serialized = $serializer->serialize($e, 'json'); |
| 450 | + $this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 451 | + |
| 452 | + $n = $serializer->deserialize($serialized, Email::class, 'json'); |
| 453 | + $serialized = $serializer->serialize($e, 'json'); |
| 454 | + $this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 455 | + |
| 456 | + $n->from('fabien@symfony.com'); |
| 457 | + $expected->from('fabien@symfony.com'); |
| 458 | + $this->assertEquals($expected->getHeaders(), $n->getHeaders()); |
| 459 | + $this->assertEquals($expected->getBody(), $n->getBody()); |
| 460 | + } |
387 | 461 | }
|
0 commit comments