Skip to content

[Mime] EmailHeaderSame doesn't check for header nullability #44190

Closed
@garak

Description

@garak

Symfony version(s) affected

4.4

Description

in class Symfony\Component\Mime\Test\Constraint\EmailHeaderSame there is this method:

private function getHeaderValue($message): string
    {
        $header = $message->getHeaders()->get($this->headerName);

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

in the last line, the value of $header is only checked on UnstructuredHeader class. It should be checked at least for not being null (but I guess indeed it should be checked for any class containing the getBodyAsString method)

How to reproduce

Make a test where you check an e-mail header, for example:

public function testEmailIsSentWithHeader(): void
{
    // do something where your email is sent...
    // [...]
    self::assertEmailCount(1);
    $email = self::getMailerMessage();
    self::assertNotNull($email);
    self::assertEmailHeaderSame($email, 'Content-Type', 'X-foo');
}

in the last line you get

Error: Call to a member function getBodyAsString() on null
/prj/vendor/symfony/mime/Test/Constraint/EmailHeaderSame.php:67
/prj/vendor/symfony/mime/Test/Constraint/EmailHeaderSame.php:48
/prj/vendor/symfony/framework-bundle/Test/MailerAssertionsTrait.php:80
/prj/tests/MyTest.php:42

Possible Solution

Check for type of $header variable (see above)

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions