|
17 | 17 | use Symfony\Component\Mime\Header\MailboxListHeader; |
18 | 18 | use Symfony\Component\Mime\Header\UnstructuredHeader; |
19 | 19 | use Symfony\Component\Mime\Message; |
| 20 | +use Symfony\Component\Mime\Part\DataPart; |
| 21 | +use Symfony\Component\Mime\Part\Multipart\AlternativePart; |
| 22 | +use Symfony\Component\Mime\Part\Multipart\MixedPart; |
20 | 23 | use Symfony\Component\Mime\Part\TextPart; |
| 24 | +use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
| 25 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 26 | +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 27 | +use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer; |
| 28 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 29 | +use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
| 30 | +use Symfony\Component\Serializer\Serializer; |
21 | 31 |
|
22 | 32 | class MessageTest extends TestCase |
23 | 33 | { |
@@ -147,4 +157,109 @@ public function testToString() |
147 | 157 | $this->assertStringMatchesFormat($expected, str_replace("\r\n", "\n", $message->toString())); |
148 | 158 | $this->assertStringMatchesFormat($expected, str_replace("\r\n", "\n", implode('', iterator_to_array($message->toIterable(), false)))); |
149 | 159 | } |
| 160 | + |
| 161 | + public function testSymfonySerialize() |
| 162 | + { |
| 163 | + // we don't add from/sender to check that it's not needed to serialize an email |
| 164 | + $body = new MixedPart( |
| 165 | + new AlternativePart( |
| 166 | + new TextPart('Text content'), |
| 167 | + new TextPart('HTML content', 'utf-8', 'html') |
| 168 | + ), |
| 169 | + new DataPart('text data', 'text.txt') |
| 170 | + ); |
| 171 | + $e = new Message((new Headers())->addMailboxListHeader('To', ['you@example.com']), $body); |
| 172 | + $expected = clone $e; |
| 173 | + |
| 174 | + $expectedJson = <<<EOF |
| 175 | +{ |
| 176 | + "headers": { |
| 177 | + "to": [ |
| 178 | + { |
| 179 | + "addresses": [ |
| 180 | + { |
| 181 | + "address": "you@example.com", |
| 182 | + "name": "" |
| 183 | + } |
| 184 | + ], |
| 185 | + "name": "To", |
| 186 | + "lineLength": 76, |
| 187 | + "lang": null, |
| 188 | + "charset": "utf-8" |
| 189 | + } |
| 190 | + ] |
| 191 | + }, |
| 192 | + "body": { |
| 193 | + "boundary": null, |
| 194 | + "parts": [ |
| 195 | + { |
| 196 | + "boundary": null, |
| 197 | + "parts": [ |
| 198 | + { |
| 199 | + "body": "Text content", |
| 200 | + "charset": "utf-8", |
| 201 | + "subtype": "plain", |
| 202 | + "disposition": null, |
| 203 | + "name": null, |
| 204 | + "encoding": "quoted-printable", |
| 205 | + "seekable": null, |
| 206 | + "headers": [], |
| 207 | + "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\TextPart" |
| 208 | + }, |
| 209 | + { |
| 210 | + "body": "HTML content", |
| 211 | + "charset": "utf-8", |
| 212 | + "subtype": "html", |
| 213 | + "disposition": null, |
| 214 | + "name": null, |
| 215 | + "encoding": "quoted-printable", |
| 216 | + "seekable": null, |
| 217 | + "headers": [], |
| 218 | + "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\TextPart" |
| 219 | + } |
| 220 | + ], |
| 221 | + "headers": [], |
| 222 | + "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\AlternativePart" |
| 223 | + }, |
| 224 | + { |
| 225 | + "filename": "text.txt", |
| 226 | + "mediaType": "application", |
| 227 | + "cid": null, |
| 228 | + "handle": null, |
| 229 | + "body": "text data", |
| 230 | + "charset": null, |
| 231 | + "subtype": "octet-stream", |
| 232 | + "disposition": "attachment", |
| 233 | + "name": "text.txt", |
| 234 | + "encoding": "base64", |
| 235 | + "seekable": null, |
| 236 | + "headers": [], |
| 237 | + "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\DataPart" |
| 238 | + } |
| 239 | + ], |
| 240 | + "headers": [], |
| 241 | + "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\MixedPart" |
| 242 | + }, |
| 243 | + "message": null |
| 244 | +} |
| 245 | +EOF; |
| 246 | + |
| 247 | + $extractor = new PhpDocExtractor(); |
| 248 | + $propertyNormalizer = new PropertyNormalizer(null, null, $extractor); |
| 249 | + $serializer = new Serializer([ |
| 250 | + new ArrayDenormalizer(), |
| 251 | + new MimeMessageNormalizer($propertyNormalizer), |
| 252 | + new ObjectNormalizer(null, null, null, $extractor), |
| 253 | + $propertyNormalizer |
| 254 | + ], [new JsonEncoder()]); |
| 255 | + |
| 256 | + $serialized = $serializer->serialize($e, 'json'); |
| 257 | + $this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 258 | + |
| 259 | + $n = $serializer->deserialize($serialized, Message::class, 'json'); |
| 260 | + $this->assertEquals($expected->getHeaders(), $n->getHeaders()); |
| 261 | + |
| 262 | + $serialized = $serializer->serialize($e, 'json'); |
| 263 | + $this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 264 | + } |
150 | 265 | } |
0 commit comments