Skip to content

Commit 618597a

Browse files
committed
[Mime] Fix TextPart using an unknown File
1 parent 2e3107e commit 618597a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Part/TextPart.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ public function getName(): ?string
123123
public function getBody(): string
124124
{
125125
if ($this->body instanceof File) {
126-
return file_get_contents($this->body->getPath());
126+
if (false === $ret = @file_get_contents($this->body->getPath())) {
127+
throw new InvalidArgumentException(error_get_last()['message']);
128+
}
129+
130+
return $ret;
127131
}
128132

129133
if (null === $this->seekable) {

Tests/Part/TextPartTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Mime\Tests\Part;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\Exception\InvalidArgumentException;
1516
use Symfony\Component\Mime\Header\Headers;
1617
use Symfony\Component\Mime\Header\ParameterizedHeader;
1718
use Symfony\Component\Mime\Header\UnstructuredHeader;
@@ -55,6 +56,16 @@ public function testConstructorWithFile()
5556
$this->assertSame('content', implode('', iterator_to_array($p->bodyToIterable())));
5657
}
5758

59+
public function testConstructorWithUnknownFile()
60+
{
61+
$p = new TextPart(new File(\dirname(__DIR__).'/Fixtures/unknown.txt'));
62+
63+
// Exception should be thrown only when the body is accessed
64+
$this->expectException(InvalidArgumentException::class);
65+
$this->expectExceptionMessageMatches('{Failed to open stream}');
66+
$p->getBody();
67+
}
68+
5869
public function testConstructorWithNonStringOrResource()
5970
{
6071
$this->expectException(\TypeError::class);

0 commit comments

Comments
 (0)