Files
myep-team/tests/Service/Teamer/TeamerMailingDraftHandlerTest.php
2026-09-01 11:24:44 +02:00

70 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service\Teamer;
use App\Model\TeamerMailingDto;
use App\Service\Teamer\TeamerMailingDraftHandler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class TeamerMailingDraftHandlerTest extends TestCase
{
private TeamerMailingDraftHandler $handler;
protected function setUp(): void
{
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$requestStack = new RequestStack([$request]);
$this->handler = new TeamerMailingDraftHandler($requestStack);
}
public function testANewMailingOpensWithTheGreeting(): void
{
$draft = $this->handler->getDraft();
$this->assertNull($draft->getSubject());
$this->assertSame(TeamerMailingDraftHandler::NEW_MESSAGE, $draft->getMessage());
}
public function testSavedDraftSurvivesUntilItIsReset(): void
{
$this->handler->saveDraft(
(new TeamerMailingDto())
->setSubject('Wintersaison {{vorname}}')
->setMessage("Hallo {{vorname}},\n\nzweite Zeile.")
);
$draft = $this->handler->getDraft();
$this->assertSame('Wintersaison {{vorname}}', $draft->getSubject());
$this->assertSame("Hallo {{vorname}},\n\nzweite Zeile.", $draft->getMessage());
$this->handler->resetDraft();
// discarding brings back the starting point of a new mailing, not a blank page
$this->assertNull($this->handler->getDraft()->getSubject());
$this->assertSame(TeamerMailingDraftHandler::NEW_MESSAGE, $this->handler->getDraft()->getMessage());
$this->assertFalse($this->handler->hasDraft());
}
public function testAHalfWrittenDraftIsKeptAsItIs(): void
{
$this->handler->saveDraft((new TeamerMailingDto())->setSubject('nur ein Betreff'));
$draft = $this->handler->getDraft();
// an emptied message stays empty, the greeting is only the starting point
$this->assertSame('nur ein Betreff', $draft->getSubject());
$this->assertNull($draft->getMessage());
$this->assertTrue($this->handler->hasDraft());
}
}