Skip to content

Commit

Permalink
[Mime] Forbid messages that are generators to be used more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 15, 2023
1 parent ff0015e commit 1eba11a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions RawMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class RawMessage
{
private iterable|string|null $message = null;
private bool $isGeneratorClosed;

public function __construct(iterable|string $message)
{
Expand All @@ -41,12 +42,29 @@ public function toString(): string

public function toIterable(): iterable
{
if ($this->isGeneratorClosed ?? false) {
trigger_deprecation('symfony/mime', '6.4', 'Sending an email with a closed generator is deprecated and will throw in 7.0.');
// throw new LogicException('Unable to send the email as its generator is already closed.');
}

if (\is_string($this->message)) {
yield $this->message;

return;
}

if ($this->message instanceof \Generator) {
$message = '';
foreach ($this->message as $chunk) {
$message .= $chunk;
yield $chunk;
}
$this->isGeneratorClosed = !$this->message->valid();
$this->message = $message;

return;
}

foreach ($this->message as $chunk) {
yield $chunk;
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/RawMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
namespace Symfony\Component\Mime\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Mime\RawMessage;

class RawMessageTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @dataProvider provideMessages
*/
Expand Down Expand Up @@ -46,6 +49,37 @@ public function testSerialization(mixed $messageParameter, bool $supportReuse)
}
}

/**
* @dataProvider provideMessages
*/
public function testToIterable(mixed $messageParameter, bool $supportReuse)
{
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));

if ($supportReuse) {
// calling methods more than once work
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
}

/**
* @dataProvider provideMessages
*
* @group legacy
*/
public function testToIterableLegacy(mixed $messageParameter, bool $supportReuse)
{
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));

if (!$supportReuse) {
// in 7.0, the test with a generator will throw an exception
$this->expectDeprecation('Since symfony/mime 6.4: Sending an email with a closed generator is deprecated and will throw in 7.0.');
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
}

public static function provideMessages(): array
{
return [
Expand Down

0 comments on commit 1eba11a

Please sign in to comment.