Merge branch 'new_emails_renderer_integration'

This commit is contained in:
ErickSkrauch 2019-06-18 01:19:52 +03:00
commit 9319e42a1c
41 changed files with 863 additions and 302 deletions

View File

@ -3,6 +3,7 @@
YII_DEBUG=true
YII_ENV=dev
DOMAIN=https://account.ely.by
EMAILS_RENDERER_HOST=http://emails-renderer:3000
## Параметры, отвечающие за безопасность
JWT_USER_SECRET=

View File

@ -20,7 +20,6 @@ coverage:
- config/*
- runtime/*
- tests/*
- web/*
- codeception.dist.yml
- codeception.yml
c3url: 'http://localhost/api/web/index.php'

View File

@ -20,7 +20,7 @@ class Yii extends \yii\BaseYii {
* @property \yii\swiftmailer\Mailer $mailer
* @property \yii\redis\Connection $redis
* @property \GuzzleHttp\Client $guzzle
* @property \common\components\EmailRenderer $emailRenderer
* @property \common\components\EmailsRenderer\Component $emailsRenderer
* @property \mito\sentry\Component $sentry
* @property \api\components\OAuth2\Component $oauth
* @property \common\components\StatsD $statsd

View File

@ -1,71 +0,0 @@
<?php
namespace common\components;
use Ely\Email\Renderer;
use Ely\Email\TemplateBuilder;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class EmailRenderer extends Component {
/**
* @var string базовый путь после хоста. Должен начинаться слешем и заканчиваться без него.
* Например "/email-images"
*/
public $basePath = '';
/**
* @var Renderer
*/
private $renderer;
/**
* @var string
*/
private $_baseDomain;
public function __construct(array $config = []) {
parent::__construct($config);
if ($this->_baseDomain === null) {
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
if ($this->_baseDomain === null) {
throw new InvalidConfigException('Cannot automatically obtain base domain');
}
}
$this->renderer = new Renderer($this->buildBasePath());
}
public function setBaseDomain(string $baseDomain) {
$this->_baseDomain = $baseDomain;
$this->renderer->setBaseDomain($this->buildBasePath());
}
public function getBaseDomain(): string {
return $this->_baseDomain;
}
/**
* @param string $templateName
* @return TemplateBuilder
*/
public function getTemplate(string $templateName): TemplateBuilder {
return $this->renderer->getTemplate($templateName);
}
/**
* @param TemplateBuilder $template
* @throws \Ely\Email\RendererException
* @return string
*/
public function render(TemplateBuilder $template): string {
return $this->renderer->render($template);
}
private function buildBasePath(): string {
return $this->_baseDomain . $this->basePath;
}
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
class Api {
private $baseUrl;
/**
* @var ClientInterface
*/
private $client;
public function __construct(string $baseUrl) {
$this->baseUrl = $baseUrl;
}
public function setClient(ClientInterface $client): void {
$this->client = $client;
}
/**
* @param \common\components\EmailsRenderer\Request\TemplateRequest $request
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getTemplate(Request\TemplateRequest $request): string {
return $this->getClient()
->request('GET', "/templates/{$request->getLocale()}/{$request->getName()}", [
'query' => $request->getParams(),
])
->getBody()
->getContents();
}
/**
* @return ClientInterface
*/
protected function getClient(): ClientInterface {
if ($this->client === null) {
$this->client = $this->createDefaultClient();
}
return $this->client;
}
private function createDefaultClient(): ClientInterface {
return new GuzzleClient([
'timeout' => 5,
'base_uri' => $this->baseUrl,
]);
}
}

View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer;
use common\components\EmailsRenderer\Request\TemplateRequest;
use common\emails\RendererInterface;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
class Component extends \yii\base\Component implements RendererInterface {
/**
* @var string The address of the templates rendering service.
*/
public $serviceUrl;
/**
* @var string application base domain. Can be omitted for web applications (will be extracted from request)
*/
public $baseDomain;
/**
* @var string base path after the host. For example "/emails-images"
*/
public $basePath = '';
/**
* @var Api
*/
private $api;
public function init(): void {
parent::init();
if ($this->serviceUrl === null) {
throw new InvalidConfigException('serviceUrl is required');
}
if ($this->baseDomain === null) {
$this->baseDomain = Yii::$app->urlManager->getHostInfo();
if ($this->baseDomain === null) {
throw new InvalidConfigException('Cannot automatically obtain base domain');
}
}
}
/**
* @param string $templateName
* @param string $locale
* @param array $params
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function render(string $templateName, string $locale, array $params = []): string {
$request = new TemplateRequest($templateName, $locale, ArrayHelper::merge($params, [
'assetsHost' => $this->buildBasePath(),
]));
return $this->getApi()->getTemplate($request);
}
protected function getApi(): Api {
if ($this->api === null) {
$this->api = new Api($this->serviceUrl);
}
return $this->api;
}
private function buildBasePath(): string {
return trim($this->baseDomain, '/') . '/' . trim($this->basePath, '/');
}
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer\Request;
class TemplateRequest {
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $locale;
/**
* @var array
*/
private $params;
public function __construct(string $name, string $locale, array $params) {
$this->name = $name;
$this->locale = $locale;
$this->params = $params;
}
public function getName(): string {
return $this->name;
}
public function getLocale(): string {
return $this->locale;
}
public function getParams(): array {
return $this->params;
}
}

View File

@ -19,5 +19,8 @@ return [
'mailer' => [
'useFileTransport' => true,
],
'emailsRenderer' => [
'class' => common\tests\_support\EmailsRenderer::class,
],
],
];

View File

@ -89,8 +89,9 @@ return [
'guzzle' => [
'class' => GuzzleHttp\Client::class,
],
'emailRenderer' => [
'class' => common\components\EmailRenderer::class,
'emailsRenderer' => [
'class' => common\components\EmailsRenderer\Component::class,
'serviceUrl' => getenv('EMAILS_RENDERER_HOST'),
'basePath' => '/images/emails',
],
'oauth' => [

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails;
class EmailHelper {

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace common\emails;
interface RendererInterface {
public function render(string $templateName, string $locale, array $params = []): string;
}

View File

@ -1,7 +1,8 @@
<?php
declare(strict_types=1);
namespace common\emails;
use common\emails\exceptions\CannotSendEmailException;
use Yii;
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;
@ -10,28 +11,12 @@ use yii\mail\MessageInterface;
abstract class Template {
/**
* @var \yii\swiftmailer\Mailer
* @var MailerInterface
*/
private $mailer;
/**
* @var string|array
*/
private $to;
/**
* @param string|array $to получатель письма. Задаётся как Email или как массив [email => name]
*/
public function __construct($to) {
$this->mailer = Yii::$app->mailer;
$this->to = $to;
}
/**
* @return array|string
*/
public function getTo() {
return $this->to;
public function __construct(MailerInterface $mailer) {
$this->mailer = $mailer;
}
abstract public function getSubject(): string;
@ -41,7 +26,7 @@ abstract class Template {
* @throws InvalidConfigException
*/
public function getFrom() {
$fromEmail = Yii::$app->params['fromEmail'];
$fromEmail = Yii::$app->params['fromEmail'] ?? '';
if (!$fromEmail) {
throw new InvalidConfigException('Please specify fromEmail app in app params');
}
@ -53,13 +38,14 @@ abstract class Template {
return [];
}
public function getMailer(): MailerInterface {
return $this->mailer;
}
public function send(): void {
if (!$this->createMessage()->send()) {
throw new CannotSendEmailException('Unable send email.');
/**
* @param string|array $to see \yii\mail\MessageInterface::setTo to know the format.
*
* @throws \common\emails\exceptions\CannotSendEmailException
*/
public function send($to): void {
if (!$this->createMessage($to)->send()) {
throw new exceptions\CannotSendEmailException();
}
}
@ -68,10 +54,14 @@ abstract class Template {
*/
abstract protected function getView();
protected function createMessage(): MessageInterface {
final protected function getMailer(): MailerInterface {
return $this->mailer;
}
protected function createMessage($for): MessageInterface {
return $this->getMailer()
->compose($this->getView(), $this->getParams())
->setTo($this->getTo())
->setTo($for)
->setFrom($this->getFrom())
->setSubject($this->getSubject());
}

View File

@ -1,28 +1,30 @@
<?php
declare(strict_types=1);
namespace common\emails;
use common\components\EmailRenderer;
use Yii;
use Exception;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
abstract class TemplateWithRenderer extends Template {
/**
* @var EmailRenderer
* @var RendererInterface
*/
private $emailRenderer;
private $renderer;
/**
* @var string
*/
private $locale;
private $locale = 'en';
/**
* @inheritdoc
*/
public function __construct($to, string $locale) {
parent::__construct($to);
$this->emailRenderer = Yii::$app->emailRenderer;
public function __construct(MailerInterface $mailer, RendererInterface $renderer) {
parent::__construct($mailer);
$this->renderer = $renderer;
}
public function setLocale(string $locale): void {
$this->locale = $locale;
}
@ -30,37 +32,47 @@ abstract class TemplateWithRenderer extends Template {
return $this->locale;
}
public function getEmailRenderer(): EmailRenderer {
return $this->emailRenderer;
}
/**
* Метод должен возвращать имя шаблона, который должен быть использован.
* Имена можно взять в репозитории elyby/email-renderer
* This method should return the template's name, which will be rendered.
* List of available templates names can be found at https://github.com/elyby/emails-renderer
*
* @return string
*/
abstract public function getTemplateName(): string;
final protected function getRenderer(): RendererInterface {
return $this->renderer;
}
final protected function getView() {
return $this->getTemplateName();
}
protected function createMessage(): MessageInterface {
/**
* @param string|array $for
*
* @return MessageInterface
* @throws \common\emails\exceptions\CannotRenderEmailException
*/
protected function createMessage($for): MessageInterface {
return $this->getMailer()
->compose()
->setHtmlBody($this->render())
->setTo($this->getTo())
->setTo($for)
->setFrom($this->getFrom())
->setSubject($this->getSubject());
}
/**
* @return string
* @throws \common\emails\exceptions\CannotRenderEmailException
*/
private function render(): string {
return $this->getEmailRenderer()
->getTemplate($this->getTemplateName())
->setLocale($this->getLocale())
->setParams($this->getParams())
->render();
try {
return $this->getRenderer()->render($this->getTemplateName(), $this->getLocale(), $this->getParams());
} catch (Exception $e) {
throw new exceptions\CannotRenderEmailException($e);
}
}
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace common\emails\exceptions;
use Exception;
use Throwable;
class CannotRenderEmailException extends Exception {
public function __construct(Throwable $previous = null) {
parent::__construct('Unable to render a template', 0, $previous);
}
}

View File

@ -1,8 +1,15 @@
<?php
declare(strict_types=1);
namespace common\emails\exceptions;
use Exception;
use Throwable;
class CannotSendEmailException extends Exception {
public function __construct(Throwable $previous = null) {
parent::__construct('Unable send email', 0, $previous);
}
}

View File

@ -1,14 +1,19 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\Template;
use yii\base\InvalidCallException;
class ChangeEmailConfirmCurrentEmail extends Template {
class ChangeEmail extends Template {
/**
* @var string|null
*/
private $key;
public function __construct($to, string $key) {
parent::__construct($to);
public function setKey(string $key): void {
$this->key = $key;
}
@ -17,14 +22,15 @@ class ChangeEmailConfirmCurrentEmail extends Template {
}
public function getParams(): array {
if ($this->key === null) {
throw new InvalidCallException('You need to set key param first');
}
return [
'key' => $this->key,
];
}
/**
* @return string|array
*/
protected function getView() {
return [
'html' => '@common/emails/views/current-email-confirmation-html',

View File

@ -1,17 +1,28 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\Template;
use yii\base\InvalidCallException;
class ChangeEmailConfirmNewEmail extends Template {
class ConfirmNewEmail extends Template {
/**
* @var string|null
*/
private $username;
/**
* @var string|null
*/
private $key;
public function __construct($to, string $username, string $key) {
parent::__construct($to);
public function setUsername(string $username): void {
$this->username = $username;
}
public function setKey(string $key): void {
$this->key = $key;
}
@ -20,15 +31,16 @@ class ChangeEmailConfirmNewEmail extends Template {
}
public function getParams(): array {
if ($this->username === null || $this->key === null) {
throw new InvalidCallException('You need to set username and key params first');
}
return [
'key' => $this->key,
'username' => $this->username,
'key' => $this->key,
];
}
/**
* @return string|array
*/
protected function getView() {
return [
'html' => '@common/emails/views/new-email-confirmation-html',

View File

@ -1,19 +1,17 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\TemplateWithRenderer;
use yii\base\InvalidCallException;
class ForgotPasswordEmail extends TemplateWithRenderer {
private $params;
/**
* @inheritdoc
* @var ForgotPasswordParams|null
*/
public function __construct($to, string $locale, ForgotPasswordParams $params) {
TemplateWithRenderer::__construct($to, $locale);
$this->params = $params;
}
private $params;
public function getSubject(): string {
return 'Ely.by Account forgot password';
@ -23,7 +21,15 @@ class ForgotPasswordEmail extends TemplateWithRenderer {
return 'forgotPassword';
}
public function setParams(ForgotPasswordParams $params): void {
$this->params = $params;
}
public function getParams(): array {
if ($this->params === null) {
throw new InvalidCallException('You need to set params first');
}
return [
'username' => $this->params->getUsername(),
'code' => $this->params->getCode(),

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
class ForgotPasswordParams {

View File

@ -1,19 +1,17 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\TemplateWithRenderer;
use yii\base\InvalidCallException;
class RegistrationEmail extends TemplateWithRenderer {
private $params;
/**
* @inheritdoc
* @var RegistrationEmailParams|null
*/
public function __construct($to, string $locale, RegistrationEmailParams $params) {
TemplateWithRenderer::__construct($to, $locale);
$this->params = $params;
}
private $params;
public function getSubject(): string {
return 'Ely.by Account registration';
@ -23,7 +21,15 @@ class RegistrationEmail extends TemplateWithRenderer {
return 'register';
}
public function setParams(RegistrationEmailParams $params): void {
$this->params = $params;
}
public function getParams(): array {
if ($this->params === null) {
throw new InvalidCallException('You need to set params first');
}
return [
'username' => $this->params->getUsername(),
'code' => $this->params->getCode(),

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
class RegistrationEmailParams {

View File

@ -1,9 +1,10 @@
<?php
declare(strict_types=1);
namespace common\tasks;
use common\emails\EmailHelper;
use common\emails\templates\ChangeEmailConfirmCurrentEmail;
use common\emails\templates\ChangeEmail;
use common\models\confirmations\CurrentEmailConfirmation;
use Yii;
use yii\queue\RetryableJobInterface;
@ -25,22 +26,23 @@ class SendCurrentEmailConfirmation implements RetryableJobInterface {
return $result;
}
public function getTtr() {
public function getTtr(): int {
return 30;
}
public function canRetry($attempt, $error) {
public function canRetry($attempt, $error): bool {
return true;
}
/**
* @param \yii\queue\Queue $queue
* @throws \common\emails\exceptions\CannotSendEmailException
*/
public function execute($queue) {
Yii::$app->statsd->inc('queue.sendCurrentEmailConfirmation.attempt');
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new ChangeEmailConfirmCurrentEmail($to, $this->code);
$template->send();
$template = new ChangeEmail(Yii::$app->mailer);
$template->setKey($this->code);
$template->send(EmailHelper::buildTo($this->username, $this->email));
}
}

View File

@ -1,9 +1,10 @@
<?php
declare(strict_types=1);
namespace common\tasks;
use common\emails\EmailHelper;
use common\emails\templates\ChangeEmailConfirmNewEmail;
use common\emails\templates\ConfirmNewEmail;
use common\models\confirmations\NewEmailConfirmation;
use Yii;
use yii\queue\RetryableJobInterface;
@ -25,22 +26,24 @@ class SendNewEmailConfirmation implements RetryableJobInterface {
return $result;
}
public function getTtr() {
public function getTtr(): int {
return 30;
}
public function canRetry($attempt, $error) {
public function canRetry($attempt, $error): bool {
return true;
}
/**
* @param \yii\queue\Queue $queue
* @throws \common\emails\exceptions\CannotSendEmailException
*/
public function execute($queue) {
Yii::$app->statsd->inc('queue.sendNewEmailConfirmation.attempt');
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new ChangeEmailConfirmNewEmail($to, $this->username, $this->code);
$template->send();
$template = new ConfirmNewEmail(Yii::$app->mailer);
$template->setKey($this->code);
$template->setUsername($this->username);
$template->send(EmailHelper::buildTo($this->username, $this->email));
}
}

View File

@ -1,5 +1,6 @@
<?php
declare(strict_types=1);
namespace common\tasks;
use common\emails\EmailHelper;
@ -34,11 +35,11 @@ class SendPasswordRecoveryEmail implements RetryableJobInterface {
return $result;
}
public function getTtr() {
public function getTtr(): int {
return 30;
}
public function canRetry($attempt, $error) {
public function canRetry($attempt, $error): bool {
return true;
}
@ -48,10 +49,10 @@ class SendPasswordRecoveryEmail implements RetryableJobInterface {
*/
public function execute($queue) {
Yii::$app->statsd->inc('queue.sendPasswordRecovery.attempt');
$params = new ForgotPasswordParams($this->username, $this->code, $this->link);
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new ForgotPasswordEmail($to, $this->locale, $params);
$template->send();
$template = new ForgotPasswordEmail(Yii::$app->mailer, Yii::$app->emailsRenderer);
$template->setLocale($this->locale);
$template->setParams(new ForgotPasswordParams($this->username, $this->code, $this->link));
$template->send(EmailHelper::buildTo($this->username, $this->email));
}
}

View File

@ -1,5 +1,6 @@
<?php
declare(strict_types=1);
namespace common\tasks;
use common\emails\EmailHelper;
@ -34,11 +35,11 @@ class SendRegistrationEmail implements RetryableJobInterface {
return $result;
}
public function getTtr() {
public function getTtr(): int {
return 30;
}
public function canRetry($attempt, $error) {
public function canRetry($attempt, $error): bool {
return true;
}
@ -48,10 +49,10 @@ class SendRegistrationEmail implements RetryableJobInterface {
*/
public function execute($queue) {
Yii::$app->statsd->inc('queue.sendRegistrationEmail.attempt');
$params = new RegistrationEmailParams($this->username, $this->code, $this->link);
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new RegistrationEmail($to, $this->locale, $params);
$template->send();
$template = new RegistrationEmail(Yii::$app->mailer, Yii::$app->emailsRenderer);
$template->setLocale($this->locale);
$template->setParams(new RegistrationEmailParams($this->username, $this->code, $this->link));
$template->send(EmailHelper::buildTo($this->username, $this->email));
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace common\tests\_support;
use common\components\EmailsRenderer\Component;
class EmailsRenderer extends Component {
public function render(string $templateName, string $locale, array $params = []): string {
return 'template';
}
}

View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\components\EmailsRenderer;
use common\components\EmailsRenderer\Api;
use common\components\EmailsRenderer\Request\TemplateRequest;
use common\tests\unit\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
class ApiTest extends TestCase {
/**
* @var Api
*/
private $api;
/**
* @var MockHandler
*/
private $mockHandler;
/**
* @var \Psr\Http\Message\RequestInterface[]
*/
private $history;
protected function setUp(): void {
parent::setUp();
$this->mockHandler = new MockHandler();
$handlerStack = HandlerStack::create($this->mockHandler);
$this->history = [];
$handlerStack->push(Middleware::history($this->history), 'history');
$client = new Client([
'handler' => $handlerStack,
'base_uri' => 'http://emails-renderer',
]);
$this->api = new Api('http://emails-renderer');
$this->api->setClient($client);
}
public function testGetTemplate() {
$this->mockHandler->append(new Response(200, [], 'mock-response'));
$request = new TemplateRequest('mock-name', 'mock-locale', ['find-me' => 'please']);
$this->assertSame('mock-response', $this->api->getTemplate($request));
/** @var \Psr\Http\Message\RequestInterface $request */
['request' => $request] = $this->history[0];
$this->assertSame('http://emails-renderer/templates/mock-locale/mock-name?find-me=please', (string)$request->getUri());
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\components\EmailsRenderer;
use common\components\EmailsRenderer\Api;
use common\components\EmailsRenderer\Component;
use common\components\EmailsRenderer\Request\TemplateRequest;
use common\tests\unit\TestCase;
class ComponentTest extends TestCase {
/**
* @var Api|\PHPUnit\Framework\MockObject\MockObject
*/
private $api;
/**
* @var Component
*/
private $component;
protected function setUp() {
parent::setUp();
$this->api = $this->createMock(Api::class);
$componentParams = [
'api' => $this->api,
'serviceUrl' => 'http://emails-renderer',
'basePath' => '/images/emails-templates',
];
$this->component = new class($componentParams) extends Component {
public $api;
protected function getApi(): Api {
return $this->api;
}
};
}
public function testRender() {
$expectedRequest = new TemplateRequest('mock-name', 'mock-locale', [
'find-me' => 'please',
'assetsHost' => 'http://localhost/images/emails-templates',
]);
$this->api->expects($this->once())->method('getTemplate')->with($expectedRequest)->willReturn('mock-template');
$result = $this->component->render('mock-name', 'mock-locale', ['find-me' => 'please']);
$this->assertSame('mock-template', $result);
}
}

View File

@ -1,47 +1,73 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails;
use common\emails\exceptions\CannotSendEmailException;
use common\emails\Template;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use Yii;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
class TemplateTest extends TestCase {
use ProtectedCaller;
public function testConstructor() {
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class, ['find-me'])->makePartial();
$this->assertSame('find-me', $template->getTo());
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
/**
* @var Template|\PHPUnit\Framework\MockObject\MockObject $template
*/
private $template;
/**
* @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $mailer;
/**
* @var string
*/
private $initialFromEmail;
public function testGetters() {
$this->assertSame(['find-me' => 'Ely.by Accounts'], $this->template->getFrom());
$this->assertSame([], $this->template->getParams());
}
public function testGetFrom() {
public function testSend() {
$this->runTestForSend(true);
}
public function testNotSend() {
$this->expectException(CannotSendEmailException::class);
$this->runTestForSend(false);
}
protected function _before() {
parent::_before();
$this->mailer = $this->createMock(MailerInterface::class);
$this->template = $this->getMockForAbstractClass(Template::class, [$this->mailer]);
$this->initialFromEmail = Yii::$app->params['fromEmail'];
Yii::$app->params['fromEmail'] = 'find-me';
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class)->makePartial();
$this->assertSame(['find-me' => 'Ely.by Accounts'], $template->getFrom());
}
public function testGetParams() {
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class)->makePartial();
$this->assertSame([], $template->getParams());
protected function _after() {
parent::_after();
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
}
public function testCreateMessage() {
Yii::$app->params['fromEmail'] = 'from@ely.by';
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class, [['to@ely.by' => 'To']])->makePartial();
$template->shouldReceive('getSubject')->andReturn('mock-subject');
/** @var MessageInterface $message */
$message = $this->callProtected($template, 'createMessage');
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertSame(['to@ely.by' => 'To'], $message->getTo());
$this->assertSame(['from@ely.by' => 'Ely.by Accounts'], $message->getFrom());
$this->assertSame('mock-subject', $message->getSubject());
private function runTestForSend(bool $sendResult) {
$this->template->expects($this->once())->method('getSubject')->willReturn('mock-subject');
$this->template->expects($this->once())->method('getView')->willReturn('mock-view');
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $message */
$message = $this->createMock(MessageInterface::class);
$message->expects($this->once())->method('setTo')->with(['to@ely.by' => 'To'])->willReturnSelf();
$message->expects($this->once())->method('setFrom')->with(['find-me' => 'Ely.by Accounts'])->willReturnSelf();
$message->expects($this->once())->method('setSubject')->with('mock-subject')->willReturnSelf();
$message->expects($this->once())->method('send')->willReturn($sendResult);
$this->mailer->expects($this->once())->method('compose')->with('mock-view', [])->willReturn($message);
$this->template->send(['to@ely.by' => 'To']);
}
}

View File

@ -1,49 +1,103 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails;
use common\components\EmailRenderer;
use common\emails\exceptions\CannotRenderEmailException;
use common\emails\RendererInterface;
use common\emails\TemplateWithRenderer;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use Ely\Email\TemplateBuilder;
use Exception;
use Yii;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
class TemplateWithRendererTest extends TestCase {
use ProtectedCaller;
public function testConstructor() {
/** @var TemplateWithRenderer|\Mockery\MockInterface $template */
$template = mock(TemplateWithRenderer::class, ['mock-to', 'mock-locale'])->makePartial();
$this->assertSame('mock-to', $template->getTo());
$this->assertSame('mock-locale', $template->getLocale());
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
$this->assertInstanceOf(EmailRenderer::class, $template->getEmailRenderer());
/**
* @var TemplateWithRenderer|\PHPUnit\Framework\MockObject\MockObject $template
*/
private $template;
/**
* @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $mailer;
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
/**
* @var string
*/
private $initialFromEmail;
public function testGetLocale() {
$this->assertSame('en', $this->template->getLocale());
$this->template->setLocale('find me');
$this->assertSame('find me', $this->template->getLocale());
}
public function testCreateMessage() {
/** @var TemplateBuilder|\Mockery\MockInterface $templateBuilder */
$templateBuilder = mock(TemplateBuilder::class)->makePartial();
$templateBuilder->shouldReceive('render')->andReturn('mock-html');
public function testSend() {
$this->runTestForSend();
}
/** @var EmailRenderer|\Mockery\MockInterface $renderer */
$renderer = mock(EmailRenderer::class)->makePartial();
$renderer->shouldReceive('getTemplate')->with('mock-template')->andReturn($templateBuilder);
public function testSendWithRenderError() {
$renderException = new Exception('find me');
try {
$this->runTestForSend($renderException);
} catch (CannotRenderEmailException $e) {
// Catch exception manually to assert the previous exception
$this->assertSame('Unable to render a template', $e->getMessage());
$this->assertSame($renderException, $e->getPrevious());
/** @var TemplateWithRenderer|\Mockery\MockInterface $template */
$template = mock(TemplateWithRenderer::class, [['to@ely.by' => 'To'], 'mock-locale']);
$template->makePartial();
$template->shouldReceive('getEmailRenderer')->andReturn($renderer);
$template->shouldReceive('getFrom')->andReturn(['from@ely.by' => 'From']);
$template->shouldReceive('getSubject')->andReturn('mock-subject');
$template->shouldReceive('getTemplateName')->andReturn('mock-template');
/** @var \yii\swiftmailer\Message $message */
$message = $this->callProtected($template, 'createMessage');
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertSame(['to@ely.by' => 'To'], $message->getTo());
$this->assertSame(['from@ely.by' => 'From'], $message->getFrom());
$this->assertSame('mock-subject', $message->getSubject());
$this->assertSame('mock-html', $message->getSwiftMessage()->getBody());
return;
}
$this->assertFalse(true, 'no exception was thrown');
}
protected function _before() {
parent::_before();
$this->mailer = $this->createMock(MailerInterface::class);
$this->renderer = $this->createMock(RendererInterface::class);
$this->template = $this->getMockForAbstractClass(TemplateWithRenderer::class, [$this->mailer, $this->renderer]);
$this->initialFromEmail = Yii::$app->params['fromEmail'];
Yii::$app->params['fromEmail'] = 'find-me';
}
protected function _after() {
parent::_after();
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
}
private function runTestForSend($renderException = null) {
$renderMethodExpectation = $this->renderer->expects($this->once())->method('render')->with('mock-template', 'mock-locale', []);
if ($renderException === null) {
$renderMethodExpectation->willReturn('mock-template-contents');
$times = [$this, 'once'];
} else {
$renderMethodExpectation->willThrowException($renderException);
$times = [$this, 'any'];
}
$this->template->expects($times())->method('getSubject')->willReturn('mock-subject');
$this->template->expects($times())->method('getTemplateName')->willReturn('mock-template');
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $message */
$message = $this->createMock(MessageInterface::class);
$message->expects($times())->method('setTo')->with(['to@ely.by' => 'To'])->willReturnSelf();
$message->expects($times())->method('setHtmlBody')->with('mock-template-contents')->willReturnSelf();
$message->expects($times())->method('setFrom')->with(['find-me' => 'Ely.by Accounts'])->willReturnSelf();
$message->expects($times())->method('setSubject')->with('mock-subject')->willReturnSelf();
$message->expects($times())->method('send')->willReturn(true);
$this->mailer->expects($times())->method('compose')->willReturn($message);
$this->template->setLocale('mock-locale');
$this->template->send(['to@ely.by' => 'To']);
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\templates\ChangeEmail;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ChangeEmailTest extends TestCase {
/**
* @var ChangeEmail()|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setKey('mock-key');
$params = $this->template->getParams();
$this->assertSame('mock-key', $params['key']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
$this->template = new ChangeEmail($mailer);
}
}

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\templates\ConfirmNewEmail;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ConfirmNewEmailTest extends TestCase {
/**
* @var ConfirmNewEmail|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setUsername('mock-username');
$this->template->setKey('mock-key');
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-key', $params['key']);
}
/**
* @dataProvider getInvalidCallsCases
*/
public function testInvalidCallOfParams(?string $username, ?string $key) {
$this->expectException(InvalidCallException::class);
$username !== null && $this->template->setUsername($username);
$key !== null && $this->template->setKey($key);
$this->template->getParams();
}
public function getInvalidCallsCases() {
yield [null, null];
yield ['value', null];
yield [null, 'value'];
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
$this->template = new ConfirmNewEmail($mailer);
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\RendererInterface;
use common\emails\templates\ForgotPasswordEmail;
use common\emails\templates\ForgotPasswordParams;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ForgotPasswordEmailTest extends TestCase {
/**
* @var ForgotPasswordEmail|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setParams(new ForgotPasswordParams('mock-username', 'mock-code', 'mock-link'));
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-code', $params['code']);
$this->assertSame('mock-link', $params['link']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
/** @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject $renderer */
$renderer = $this->createMock(RendererInterface::class);
$this->template = new ForgotPasswordEmail($mailer, $renderer);
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\RendererInterface;
use common\emails\templates\RegistrationEmail;
use common\emails\templates\RegistrationEmailParams;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class RegistrationEmailTest extends TestCase {
/**
* @var RegistrationEmail()|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setParams(new RegistrationEmailParams('mock-username', 'mock-code', 'mock-link'));
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-code', $params['code']);
$this->assertSame('mock-link', $params['link']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
/** @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject $renderer */
$renderer = $this->createMock(RendererInterface::class);
$this->template = new RegistrationEmail($mailer, $renderer);
}
}

View File

@ -1,14 +1,23 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\emails\RendererInterface;
use common\models\Account;
use common\models\confirmations\ForgotPassword;
use common\tasks\SendPasswordRecoveryEmail;
use common\tests\unit\TestCase;
use Yii;
use yii\queue\Queue;
class SendPasswordRecoveryEmailTest extends TestCase {
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
public function testCreateFromConfirmation() {
$account = new Account();
$account->username = 'mock-username';
@ -21,7 +30,6 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$confirmation->shouldReceive('getAccount')->andReturn($account);
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
$this->assertInstanceOf(SendPasswordRecoveryEmail::class, $result);
$this->assertSame('mock-username', $result->username);
$this->assertSame('mock@ely.by', $result->email);
$this->assertSame('ABCDEFG', $result->code);
@ -37,6 +45,12 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$task->link = 'https://account.ely.by/recover-password/ABCDEFG';
$task->locale = 'ru';
$this->renderer->expects($this->once())->method('render')->with('forgotPassword', 'ru', [
'username' => 'mock-username',
'code' => 'GFEDCBA',
'link' => 'https://account.ely.by/recover-password/ABCDEFG',
])->willReturn('mock-template');
$task->execute(mock(Queue::class));
$this->tester->canSeeEmailIsSent(1);
@ -44,10 +58,14 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$email = $this->tester->grabSentEmails()[0];
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
$this->assertSame('Ely.by Account forgot password', $email->getSubject());
$body = $email->getSwiftMessage()->getBody();
$this->assertContains('Привет, mock-username', $body);
$this->assertContains('GFEDCBA', $body);
$this->assertContains('https://account.ely.by/recover-password/ABCDEFG', $body);
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
}
protected function _before() {
parent::_before();
$this->renderer = $this->createMock(RendererInterface::class);
Yii::$app->set('emailsRenderer', $this->renderer);
}
}

View File

@ -1,14 +1,23 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\emails\RendererInterface;
use common\models\Account;
use common\models\confirmations\RegistrationConfirmation;
use common\tasks\SendRegistrationEmail;
use common\tests\unit\TestCase;
use Yii;
use yii\queue\Queue;
class SendRegistrationEmailTest extends TestCase {
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
public function testCreateFromConfirmation() {
$account = new Account();
$account->username = 'mock-username';
@ -21,7 +30,6 @@ class SendRegistrationEmailTest extends TestCase {
$confirmation->shouldReceive('getAccount')->andReturn($account);
$result = SendRegistrationEmail::createFromConfirmation($confirmation);
$this->assertInstanceOf(SendRegistrationEmail::class, $result);
$this->assertSame('mock-username', $result->username);
$this->assertSame('mock@ely.by', $result->email);
$this->assertSame('ABCDEFG', $result->code);
@ -37,17 +45,27 @@ class SendRegistrationEmailTest extends TestCase {
$task->link = 'https://account.ely.by/activation/ABCDEFG';
$task->locale = 'ru';
$task->execute(mock(Queue::class));
$this->renderer->expects($this->once())->method('render')->with('register', 'ru', [
'username' => 'mock-username',
'code' => 'GFEDCBA',
'link' => 'https://account.ely.by/activation/ABCDEFG',
])->willReturn('mock-template');
$task->execute($this->createMock(Queue::class));
$this->tester->canSeeEmailIsSent(1);
/** @var \yii\swiftmailer\Message $email */
$email = $this->tester->grabSentEmails()[0];
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
$this->assertSame('Ely.by Account registration', $email->getSubject());
$body = $email->getSwiftMessage()->getBody();
$this->assertContains('Привет, mock-username', $body);
$this->assertContains('GFEDCBA', $body);
$this->assertContains('https://account.ely.by/activation/ABCDEFG', $body);
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
}
protected function _before() {
parent::_before();
$this->renderer = $this->createMock(RendererInterface::class);
Yii::$app->set('emailsRenderer', $this->renderer);
}
}

View File

@ -13,7 +13,6 @@
"ext-simplexml": "*",
"bacon/bacon-qr-code": "^1.0",
"domnikl/statsd": "^2.6",
"ely/email-renderer": "dev-master",
"ely/mojang-api": "^0.2.0",
"ely/yii2-tempmail-validator": "^2.0",
"emarref/jwt": "~1.0.3",
@ -52,10 +51,6 @@
{
"type": "composer",
"url": "https://asset-packagist.org"
},
{
"type": "git",
"url": "https://gitlab+deploy-token-1:FDGgmcnLdykcsyJJ_8Uv@gitlab.ely.by/elyby/email-renderer.git"
}
],
"config": {

45
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "68086c7aa86b976c390b02cc53b182f9",
"content-hash": "752322170e3433ba42cb90c1dd6122a2",
"packages": [
{
"name": "bacon/bacon-qr-code",
@ -166,7 +166,7 @@
"version": "3.27.0",
"source": {
"type": "git",
"url": "git@github.com:getsentry/raven-js-bower.git",
"url": "https://github.com/getsentry/raven-js-bower.git",
"reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235"
},
"dist": {
@ -606,46 +606,6 @@
],
"time": "2018-12-04T22:38:24+00:00"
},
{
"name": "ely/email-renderer",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://gitlab+deploy-token-1:FDGgmcnLdykcsyJJ_8Uv@gitlab.ely.by/elyby/email-renderer.git",
"reference": "3b06c19b39a298e6cb00f711b5fed40c315cd95e"
},
"require": {
"php": ">=5.6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Ely\\Email\\": "src-php/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Ely.by team",
"email": "team@ely.by"
},
{
"name": "ErickSkrauch",
"email": "erickskrauch@ely.by"
},
{
"name": "SleepWalker",
"email": "dev@udf.su"
}
],
"homepage": "http://ely.by",
"keywords": [
""
],
"time": "2019-05-13T21:56:45+00:00"
},
{
"name": "ely/mojang-api",
"version": "0.2.0",
@ -6180,7 +6140,6 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"ely/email-renderer": 20,
"roave/security-advisories": 20
},
"prefer-stable": false,

View File

@ -9,6 +9,7 @@ services:
build_env: dev
depends_on:
- app
- emails-renderer
env_file: .env
volumes:
- ./:/var/www/html
@ -62,6 +63,9 @@ services:
- ./:/var/www/html
env_file: .env
emails-renderer:
image: elyby/emails-renderer:dev
db:
build:
context: .

View File

@ -11,6 +11,7 @@ services:
restart: always
depends_on:
- app
- emails-renderer
env_file: .env
volumes:
- ./frontend:/var/www/html/frontend
@ -53,6 +54,10 @@ services:
env_file: .env
logging: *default-logging
emails-renderer:
image: elyby/emails-renderer:dev
logging: *default-logging
db:
image: registry.ely.by/elyby/accounts-mariadb:latest
restart: always

View File

@ -41,13 +41,14 @@ server {
try_files $uri /index.html =404;
}
location /api {
try_files $uri /api/index.php$is_args$args;
location /images/emails/assets {
proxy_pass http://emails-renderer:3000/assets/;
expires $cache_duration;
access_log off;
}
location /images/emails/assets {
alias '${root_path}/vendor/ely/email-renderer/dist/assets';
access_log off;
location /api {
try_files $uri /api/index.php$is_args$args;
}
location ~* \.php$ {