diff --git a/src/Symfony/Bridge/Twig/Mime/NotificationEmail.php b/src/Symfony/Bridge/Twig/Mime/NotificationEmail.php index 67173a8af8ba1..e8b4a7367b114 100644 --- a/src/Symfony/Bridge/Twig/Mime/NotificationEmail.php +++ b/src/Symfony/Bridge/Twig/Mime/NotificationEmail.php @@ -178,6 +178,23 @@ public function getHtmlTemplate(): ?string return '@email/'.$this->theme.'/notification/body.html.twig'; } + public function context(array $context) + { + $parentContext = []; + + foreach ($context as $key => $value) { + if (\array_key_exists($key, $this->context)) { + $this->context[$key] = $value; + } else { + $parentContext[$key] = $value; + } + } + + parent::context($parentContext); + + return $this; + } + public function getContext(): array { return array_merge($this->context, parent::getContext()); diff --git a/src/Symfony/Bridge/Twig/Tests/Mime/NotificationEmailTest.php b/src/Symfony/Bridge/Twig/Tests/Mime/NotificationEmailTest.php index ceafea1bb6b72..71ced84f7173e 100644 --- a/src/Symfony/Bridge/Twig/Tests/Mime/NotificationEmailTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Mime/NotificationEmailTest.php @@ -128,4 +128,54 @@ public function testPublicMailSubject() $headers = $email->getPreparedHeaders(); $this->assertSame('Foo', $headers->get('Subject')->getValue()); } + + public function testContext() + { + $email = new NotificationEmail(); + $email->context(['some' => 'context']); + + $this->assertSame([ + 'importance' => NotificationEmail::IMPORTANCE_LOW, + 'content' => '', + 'exception' => false, + 'action_text' => null, + 'action_url' => null, + 'markdown' => false, + 'raw' => false, + 'footer_text' => 'Notification e-mail sent by Symfony', + 'some' => 'context', + ], $email->getContext()); + + $context = $email->getContext(); + $context['foo'] = 'bar'; + $email->context($context); + + $this->assertSame([ + 'importance' => NotificationEmail::IMPORTANCE_LOW, + 'content' => '', + 'exception' => false, + 'action_text' => null, + 'action_url' => null, + 'markdown' => false, + 'raw' => false, + 'footer_text' => 'Notification e-mail sent by Symfony', + 'some' => 'context', + 'foo' => 'bar', + ], $email->getContext()); + + $email->action('Action Text', 'Action URL'); + + $this->assertSame([ + 'importance' => NotificationEmail::IMPORTANCE_LOW, + 'content' => '', + 'exception' => false, + 'action_text' => 'Action Text', + 'action_url' => 'Action URL', + 'markdown' => false, + 'raw' => false, + 'footer_text' => 'Notification e-mail sent by Symfony', + 'some' => 'context', + 'foo' => 'bar', + ], $email->getContext()); + } }