Skip to content

Commit

Permalink
[Mime] Fix body validity check in Email when using `Message::setBod…
Browse files Browse the repository at this point in the history
…y()`
  • Loading branch information
alexandre-daubois committed Jan 23, 2025
1 parent ea87c88 commit 917d779
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function ensureValidity()

private function ensureBodyValid(): void
{
if (null === $this->text && null === $this->html && !$this->attachments) {
if (null === $this->text && null === $this->html && !$this->attachments && null === parent::getBody()) {
throw new LogicException('A message must have a text or an HTML part or attachments.');
}
}
Expand Down
56 changes: 56 additions & 0 deletions Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,60 @@ public function testEmailsWithAttachmentsWhichAreAFileInstanceCanBeUnserialized(
$this->assertCount(1, $attachments);
$this->assertStringContainsString('foo_bar_xyz_123', $attachments[0]->getBody());
}

public function testInvalidBodyWithEmptyEmail()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('A message must have a text or an HTML part or attachments.');

(new Email())->ensureValidity();
}

public function testBodyWithTextIsValid()
{
$email = new Email();
$email->to('[email protected]')
->from('[email protected]')
->text('foo');

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testBodyWithHtmlIsValid()
{
$email = new Email();
$email->to('[email protected]')
->from('[email protected]')
->html('foo');

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testEmptyBodyWithAttachmentsIsValid()
{
$email = new Email();
$email->to('[email protected]')
->from('[email protected]')
->addPart(new DataPart('foo'));

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testSetBodyIsValid()
{
$email = new Email();
$email->to('[email protected]')
->from('[email protected]')
->setBody(new TextPart('foo'));

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}
}

0 comments on commit 917d779

Please sign in to comment.