2018-09-05 13:40:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Mail;
|
|
|
|
|
2019-10-06 21:03:20 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2018-09-05 13:40:03 +02:00
|
|
|
use Engelsystem\Mail\EngelsystemMailer;
|
2019-10-06 21:03:20 +02:00
|
|
|
use Engelsystem\Models\User\Contact;
|
|
|
|
use Engelsystem\Models\User\Settings;
|
|
|
|
use Engelsystem\Models\User\User;
|
2018-09-05 13:40:03 +02:00
|
|
|
use Engelsystem\Renderer\Renderer;
|
2019-10-06 21:03:20 +02:00
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2018-09-05 13:40:03 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-12-20 21:07:00 +01:00
|
|
|
use Symfony\Component\Mailer\Envelope;
|
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
|
|
use Symfony\Component\Mime\RawMessage;
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
class EngelsystemMailerTest extends TestCase
|
|
|
|
{
|
2019-10-06 21:03:20 +02:00
|
|
|
use HasDatabase;
|
|
|
|
|
2018-09-05 13:40:03 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::__construct
|
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::sendView
|
|
|
|
*/
|
|
|
|
public function testSendView(): void
|
|
|
|
{
|
|
|
|
/** @var Renderer|MockObject $view */
|
|
|
|
$view = $this->createMock(Renderer::class);
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->getMockForAbstractClass(MailerInterface::class);
|
2018-09-05 13:40:03 +02:00
|
|
|
/** @var EngelsystemMailer|MockObject $mailer */
|
|
|
|
$mailer = $this->getMockBuilder(EngelsystemMailer::class)
|
2021-12-20 21:07:00 +01:00
|
|
|
->setConstructorArgs(['mailer' => $symfonyMailer, 'view' => $view])
|
2019-10-06 21:03:20 +02:00
|
|
|
->onlyMethods(['send'])
|
2018-09-05 13:40:03 +02:00
|
|
|
->getMock();
|
2021-12-20 21:07:00 +01:00
|
|
|
$this->setExpects($mailer, 'send', ['foo@bar.baz', 'Lorem dolor', 'Rendered Stuff!']);
|
2019-10-06 21:03:20 +02:00
|
|
|
$this->setExpects($view, 'render', ['test/template.tpl', ['dev' => true]], 'Rendered Stuff!');
|
2018-09-05 13:40:03 +02:00
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->sendView('foo@bar.baz', 'Lorem dolor', 'test/template.tpl', ['dev' => true]);
|
2018-09-05 13:40:03 +02:00
|
|
|
}
|
2018-10-05 15:35:14 +02:00
|
|
|
|
2019-10-06 21:03:20 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::sendViewTranslated
|
|
|
|
*/
|
|
|
|
public function testSendViewTranslated(): void
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
|
2021-06-29 00:27:57 +02:00
|
|
|
$user = User::factory(['email' => 'foo@bar.baz'])
|
|
|
|
->has(Settings::factory(['language' => 'de_DE']))
|
|
|
|
->has(Contact::factory(['email' => null]))
|
|
|
|
->create();
|
2019-10-06 21:03:20 +02:00
|
|
|
|
|
|
|
/** @var Renderer|MockObject $view */
|
|
|
|
$view = $this->createMock(Renderer::class);
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->createMock(MailerInterface::class);
|
2019-10-06 21:03:20 +02:00
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->createMock(Translator::class);
|
|
|
|
|
|
|
|
/** @var EngelsystemMailer|MockObject $mailer */
|
|
|
|
$mailer = $this->getMockBuilder(EngelsystemMailer::class)
|
2021-12-20 21:07:00 +01:00
|
|
|
->setConstructorArgs(['mailer' => $symfonyMailer, 'view' => $view, 'translation' => $translator])
|
2019-10-06 21:03:20 +02:00
|
|
|
->onlyMethods(['sendView'])
|
|
|
|
->getMock();
|
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$this->setExpects($mailer, 'sendView', ['foo@bar.baz', 'Lorem dolor', 'test/template.tpl', ['dev' => true]]);
|
2019-10-06 21:03:20 +02:00
|
|
|
$this->setExpects($translator, 'getLocales', null, ['de_DE' => 'de_DE', 'en_US' => 'en_US']);
|
|
|
|
$this->setExpects($translator, 'getLocale', null, 'en_US');
|
2020-12-28 16:04:05 +01:00
|
|
|
$this->setExpects($translator, 'translate', ['translatable.text', ['dev' => true]], 'Lorem dolor');
|
2019-10-06 21:03:20 +02:00
|
|
|
$translator->expects($this->exactly(2))
|
|
|
|
->method('setLocale')
|
|
|
|
->withConsecutive(['de_DE'], ['en_US']);
|
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->sendViewTranslated(
|
2019-10-06 21:03:20 +02:00
|
|
|
$user,
|
|
|
|
'translatable.text',
|
|
|
|
'test/template.tpl',
|
|
|
|
['dev' => true],
|
|
|
|
'de_DE'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-05 15:35:14 +02:00
|
|
|
/**
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::getSubjectPrefix
|
2018-10-05 15:35:14 +02:00
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::send
|
|
|
|
* @covers \Engelsystem\Mail\EngelsystemMailer::setSubjectPrefix
|
|
|
|
*/
|
|
|
|
public function testSend(): void
|
|
|
|
{
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->createMock(MailerInterface::class);
|
|
|
|
|
|
|
|
$symfonyMailer->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->willReturnCallback(function (RawMessage $message, Envelope $envelope = null): void {
|
|
|
|
$this->assertStringContainsString('foo@bar.baz', $message->toString());
|
|
|
|
$this->assertStringContainsString('Foo Bar', $message->toString());
|
|
|
|
$this->assertStringContainsString('Mail test', $message->toString());
|
|
|
|
$this->assertStringContainsString('to@xam.pel', $message->toString());
|
|
|
|
$this->assertStringContainsString('Lorem Ipsum!', $message->toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
$mailer = new EngelsystemMailer($symfonyMailer);
|
2018-10-05 15:35:14 +02:00
|
|
|
$mailer->setFromAddress('foo@bar.baz');
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->setFromName('Foo Bar');
|
2018-10-05 15:35:14 +02:00
|
|
|
$mailer->setSubjectPrefix('Mail test');
|
|
|
|
|
|
|
|
$this->assertEquals('Mail test', $mailer->getSubjectPrefix());
|
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->send('to@xam.pel', 'Foo Bar ', 'Lorem Ipsum!');
|
2018-10-05 15:35:14 +02:00
|
|
|
}
|
2018-09-05 13:40:03 +02:00
|
|
|
}
|