Skip to content

Commit 6a93d11

Browse files
bug #46366 [Mime] Add null check for EmailHeaderSame (magikid)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Mime] Add null check for EmailHeaderSame When asserting that an email header matches some text and the header does not exist on the message, an `Error` was being thrown because the method `getBodyAsString()` was being called on a `null` object. This commit allows a missing header and will show the user that the header's value is `null`. Issue #44190 | Q | A | ------------- | --- | Branch? | 4.4 | 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 #44190 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - 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 the latest branch. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- a6ec675 [Mime] Add null check for EmailHeaderSame
2 parents bb6d26a + a6ec675 commit 6a93d11

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Symfony/Component/Mime/Test/Constraint/EmailHeaderSame.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ protected function matches($message): bool
5555
*/
5656
protected function failureDescription($message): string
5757
{
58-
return sprintf('the Email %s (value is %s)', $this->toString(), $this->getHeaderValue($message));
58+
return sprintf('the Email %s (value is %s)', $this->toString(), $this->getHeaderValue($message) ?? 'null');
5959
}
6060

61-
private function getHeaderValue($message): string
61+
private function getHeaderValue($message): ?string
6262
{
63-
$header = $message->getHeaders()->get($this->headerName);
63+
if (null === $header = $message->getHeaders()->get($this->headerName)) {
64+
return null;
65+
}
6466

6567
return $header instanceof UnstructuredHeader ? $header->getValue() : $header->getBodyAsString();
6668
}

src/Symfony/Component/Mime/Tests/EmailTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Mime\Tests;
1313

14+
use PHPUnit\Framework\ExpectationFailedException;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Component\Mime\Address;
1617
use Symfony\Component\Mime\Email;
@@ -19,6 +20,7 @@
1920
use Symfony\Component\Mime\Part\Multipart\MixedPart;
2021
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
2122
use Symfony\Component\Mime\Part\TextPart;
23+
use Symfony\Component\Mime\Test\Constraint\EmailHeaderSame;
2224

2325
class EmailTest extends TestCase
2426
{
@@ -384,4 +386,14 @@ public function testSerialize()
384386
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
385387
$this->assertEquals($e->getBody(), $n->getBody());
386388
}
389+
390+
public function testMissingHeaderDoesNotThrowError()
391+
{
392+
$this->expectException(ExpectationFailedException::class);
393+
$this->expectExceptionMessage('Failed asserting that the Email has header "foo" with value "bar" (value is null).');
394+
395+
$e = new Email();
396+
$emailHeaderSame = new EmailHeaderSame('foo', 'bar');
397+
$emailHeaderSame->evaluate($e);
398+
}
387399
}

0 commit comments

Comments
 (0)