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;
|
|
|
|
use Swift_Mailer as SwiftMailer;
|
2018-10-05 15:35:14 +02:00
|
|
|
use Swift_Message as SwiftMessage;
|
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()
|
|
|
|
{
|
|
|
|
/** @var Renderer|MockObject $view */
|
|
|
|
$view = $this->createMock(Renderer::class);
|
|
|
|
/** @var SwiftMailer|MockObject $swiftMailer */
|
|
|
|
$swiftMailer = $this->createMock(SwiftMailer::class);
|
|
|
|
/** @var EngelsystemMailer|MockObject $mailer */
|
|
|
|
$mailer = $this->getMockBuilder(EngelsystemMailer::class)
|
|
|
|
->setConstructorArgs(['mailer' => $swiftMailer, 'view' => $view])
|
2019-10-06 21:03:20 +02:00
|
|
|
->onlyMethods(['send'])
|
2018-09-05 13:40:03 +02:00
|
|
|
->getMock();
|
2019-10-06 21:03:20 +02:00
|
|
|
$this->setExpects($mailer, 'send', ['foo@bar.baz', 'Lorem dolor', 'Rendered Stuff!'], 1);
|
|
|
|
$this->setExpects($view, 'render', ['test/template.tpl', ['dev' => true]], 'Rendered Stuff!');
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
$return = $mailer->sendView('foo@bar.baz', 'Lorem dolor', 'test/template.tpl', ['dev' => true]);
|
2020-04-19 23:14:06 +02:00
|
|
|
$this->assertEquals(1, $return);
|
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()
|
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
|
|
|
|
$settings = new Settings([
|
|
|
|
'language' => 'de_DE',
|
|
|
|
'theme' => '',
|
|
|
|
]);
|
|
|
|
$contact = new Contact(['email' => null]);
|
|
|
|
$user = new User([
|
|
|
|
'id' => 42,
|
|
|
|
'name' => 'username',
|
|
|
|
'email' => 'foo@bar.baz',
|
|
|
|
'password' => '',
|
|
|
|
'api_key' => '',
|
|
|
|
]);
|
|
|
|
$user->save();
|
|
|
|
$settings->user()->associate($user)->save();
|
|
|
|
$contact->user()->associate($user)->save();
|
|
|
|
|
|
|
|
/** @var Renderer|MockObject $view */
|
|
|
|
$view = $this->createMock(Renderer::class);
|
|
|
|
/** @var SwiftMailer|MockObject $swiftMailer */
|
|
|
|
$swiftMailer = $this->createMock(SwiftMailer::class);
|
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->createMock(Translator::class);
|
|
|
|
|
|
|
|
/** @var EngelsystemMailer|MockObject $mailer */
|
|
|
|
$mailer = $this->getMockBuilder(EngelsystemMailer::class)
|
|
|
|
->setConstructorArgs(['mailer' => $swiftMailer, 'view' => $view, 'translation' => $translator])
|
|
|
|
->onlyMethods(['sendView'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->setExpects($mailer, 'sendView', ['foo@bar.baz', 'Lorem dolor', 'test/template.tpl', ['dev' => true]], 1);
|
|
|
|
$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']);
|
|
|
|
|
|
|
|
$return = $mailer->sendViewTranslated(
|
|
|
|
$user,
|
|
|
|
'translatable.text',
|
|
|
|
'test/template.tpl',
|
|
|
|
['dev' => true],
|
|
|
|
'de_DE'
|
|
|
|
);
|
2020-04-19 23:14:06 +02:00
|
|
|
$this->assertEquals(1, $return);
|
2019-10-06 21:03:20 +02:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
/** @var SwiftMessage|MockObject $message */
|
|
|
|
$message = $this->createMock(SwiftMessage::class);
|
|
|
|
/** @var SwiftMailer|MockObject $swiftMailer */
|
|
|
|
$swiftMailer = $this->createMock(SwiftMailer::class);
|
2019-10-06 21:03:20 +02:00
|
|
|
$this->setExpects($swiftMailer, 'createMessage', null, $message);
|
|
|
|
$this->setExpects($swiftMailer, 'send', null, 1);
|
|
|
|
$this->setExpects($message, 'setTo', [['to@xam.pel']], $message);
|
|
|
|
$this->setExpects($message, 'setFrom', ['foo@bar.baz', 'Lorem Ipsum'], $message);
|
|
|
|
$this->setExpects($message, 'setSubject', ['[Mail test] Foo Bar'], $message);
|
|
|
|
$this->setExpects($message, 'setBody', ['Lorem Ipsum!'], $message);
|
2018-10-05 15:35:14 +02:00
|
|
|
|
|
|
|
$mailer = new EngelsystemMailer($swiftMailer);
|
|
|
|
$mailer->setFromAddress('foo@bar.baz');
|
|
|
|
$mailer->setFromName('Lorem Ipsum');
|
|
|
|
$mailer->setSubjectPrefix('Mail test');
|
|
|
|
|
|
|
|
$this->assertEquals('Mail test', $mailer->getSubjectPrefix());
|
|
|
|
|
|
|
|
$return = $mailer->send('to@xam.pel', 'Foo Bar', 'Lorem Ipsum!');
|
2020-04-19 23:14:06 +02:00
|
|
|
$this->assertEquals(1, $return);
|
2018-10-05 15:35:14 +02:00
|
|
|
}
|
2018-09-05 13:40:03 +02:00
|
|
|
}
|