mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 10:42:16 +05:30
Merge branch 'new_emails_renderer_integration'
This commit is contained in:
commit
9319e42a1c
@ -3,6 +3,7 @@
|
|||||||
YII_DEBUG=true
|
YII_DEBUG=true
|
||||||
YII_ENV=dev
|
YII_ENV=dev
|
||||||
DOMAIN=https://account.ely.by
|
DOMAIN=https://account.ely.by
|
||||||
|
EMAILS_RENDERER_HOST=http://emails-renderer:3000
|
||||||
|
|
||||||
## Параметры, отвечающие за безопасность
|
## Параметры, отвечающие за безопасность
|
||||||
JWT_USER_SECRET=
|
JWT_USER_SECRET=
|
||||||
|
@ -20,7 +20,6 @@ coverage:
|
|||||||
- config/*
|
- config/*
|
||||||
- runtime/*
|
- runtime/*
|
||||||
- tests/*
|
- tests/*
|
||||||
- web/*
|
|
||||||
- codeception.dist.yml
|
- codeception.dist.yml
|
||||||
- codeception.yml
|
- codeception.yml
|
||||||
c3url: 'http://localhost/api/web/index.php'
|
c3url: 'http://localhost/api/web/index.php'
|
||||||
|
@ -20,7 +20,7 @@ class Yii extends \yii\BaseYii {
|
|||||||
* @property \yii\swiftmailer\Mailer $mailer
|
* @property \yii\swiftmailer\Mailer $mailer
|
||||||
* @property \yii\redis\Connection $redis
|
* @property \yii\redis\Connection $redis
|
||||||
* @property \GuzzleHttp\Client $guzzle
|
* @property \GuzzleHttp\Client $guzzle
|
||||||
* @property \common\components\EmailRenderer $emailRenderer
|
* @property \common\components\EmailsRenderer\Component $emailsRenderer
|
||||||
* @property \mito\sentry\Component $sentry
|
* @property \mito\sentry\Component $sentry
|
||||||
* @property \api\components\OAuth2\Component $oauth
|
* @property \api\components\OAuth2\Component $oauth
|
||||||
* @property \common\components\StatsD $statsd
|
* @property \common\components\StatsD $statsd
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
59
common/components/EmailsRenderer/Api.php
Normal file
59
common/components/EmailsRenderer/Api.php
Normal 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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
77
common/components/EmailsRenderer/Component.php
Normal file
77
common/components/EmailsRenderer/Component.php
Normal 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, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
41
common/components/EmailsRenderer/Request/TemplateRequest.php
Normal file
41
common/components/EmailsRenderer/Request/TemplateRequest.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,5 +19,8 @@ return [
|
|||||||
'mailer' => [
|
'mailer' => [
|
||||||
'useFileTransport' => true,
|
'useFileTransport' => true,
|
||||||
],
|
],
|
||||||
|
'emailsRenderer' => [
|
||||||
|
'class' => common\tests\_support\EmailsRenderer::class,
|
||||||
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -89,8 +89,9 @@ return [
|
|||||||
'guzzle' => [
|
'guzzle' => [
|
||||||
'class' => GuzzleHttp\Client::class,
|
'class' => GuzzleHttp\Client::class,
|
||||||
],
|
],
|
||||||
'emailRenderer' => [
|
'emailsRenderer' => [
|
||||||
'class' => common\components\EmailRenderer::class,
|
'class' => common\components\EmailsRenderer\Component::class,
|
||||||
|
'serviceUrl' => getenv('EMAILS_RENDERER_HOST'),
|
||||||
'basePath' => '/images/emails',
|
'basePath' => '/images/emails',
|
||||||
],
|
],
|
||||||
'oauth' => [
|
'oauth' => [
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails;
|
namespace common\emails;
|
||||||
|
|
||||||
class EmailHelper {
|
class EmailHelper {
|
||||||
|
10
common/emails/RendererInterface.php
Normal file
10
common/emails/RendererInterface.php
Normal 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;
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails;
|
namespace common\emails;
|
||||||
|
|
||||||
use common\emails\exceptions\CannotSendEmailException;
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\mail\MailerInterface;
|
use yii\mail\MailerInterface;
|
||||||
@ -10,28 +11,12 @@ use yii\mail\MessageInterface;
|
|||||||
abstract class Template {
|
abstract class Template {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \yii\swiftmailer\Mailer
|
* @var MailerInterface
|
||||||
*/
|
*/
|
||||||
private $mailer;
|
private $mailer;
|
||||||
|
|
||||||
/**
|
public function __construct(MailerInterface $mailer) {
|
||||||
* @var string|array
|
$this->mailer = $mailer;
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function getSubject(): string;
|
abstract public function getSubject(): string;
|
||||||
@ -41,7 +26,7 @@ abstract class Template {
|
|||||||
* @throws InvalidConfigException
|
* @throws InvalidConfigException
|
||||||
*/
|
*/
|
||||||
public function getFrom() {
|
public function getFrom() {
|
||||||
$fromEmail = Yii::$app->params['fromEmail'];
|
$fromEmail = Yii::$app->params['fromEmail'] ?? '';
|
||||||
if (!$fromEmail) {
|
if (!$fromEmail) {
|
||||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||||
}
|
}
|
||||||
@ -53,13 +38,14 @@ abstract class Template {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMailer(): MailerInterface {
|
/**
|
||||||
return $this->mailer;
|
* @param string|array $to see \yii\mail\MessageInterface::setTo to know the format.
|
||||||
}
|
*
|
||||||
|
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||||
public function send(): void {
|
*/
|
||||||
if (!$this->createMessage()->send()) {
|
public function send($to): void {
|
||||||
throw new CannotSendEmailException('Unable send email.');
|
if (!$this->createMessage($to)->send()) {
|
||||||
|
throw new exceptions\CannotSendEmailException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,10 +54,14 @@ abstract class Template {
|
|||||||
*/
|
*/
|
||||||
abstract protected function getView();
|
abstract protected function getView();
|
||||||
|
|
||||||
protected function createMessage(): MessageInterface {
|
final protected function getMailer(): MailerInterface {
|
||||||
|
return $this->mailer;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createMessage($for): MessageInterface {
|
||||||
return $this->getMailer()
|
return $this->getMailer()
|
||||||
->compose($this->getView(), $this->getParams())
|
->compose($this->getView(), $this->getParams())
|
||||||
->setTo($this->getTo())
|
->setTo($for)
|
||||||
->setFrom($this->getFrom())
|
->setFrom($this->getFrom())
|
||||||
->setSubject($this->getSubject());
|
->setSubject($this->getSubject());
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails;
|
namespace common\emails;
|
||||||
|
|
||||||
use common\components\EmailRenderer;
|
use Exception;
|
||||||
use Yii;
|
use yii\mail\MailerInterface;
|
||||||
use yii\mail\MessageInterface;
|
use yii\mail\MessageInterface;
|
||||||
|
|
||||||
abstract class TemplateWithRenderer extends Template {
|
abstract class TemplateWithRenderer extends Template {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EmailRenderer
|
* @var RendererInterface
|
||||||
*/
|
*/
|
||||||
private $emailRenderer;
|
private $renderer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $locale;
|
private $locale = 'en';
|
||||||
|
|
||||||
/**
|
public function __construct(MailerInterface $mailer, RendererInterface $renderer) {
|
||||||
* @inheritdoc
|
parent::__construct($mailer);
|
||||||
*/
|
$this->renderer = $renderer;
|
||||||
public function __construct($to, string $locale) {
|
}
|
||||||
parent::__construct($to);
|
|
||||||
$this->emailRenderer = Yii::$app->emailRenderer;
|
public function setLocale(string $locale): void {
|
||||||
$this->locale = $locale;
|
$this->locale = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,37 +32,47 @@ abstract class TemplateWithRenderer extends Template {
|
|||||||
return $this->locale;
|
return $this->locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEmailRenderer(): EmailRenderer {
|
|
||||||
return $this->emailRenderer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод должен возвращать имя шаблона, который должен быть использован.
|
* This method should return the template's name, which will be rendered.
|
||||||
* Имена можно взять в репозитории elyby/email-renderer
|
* List of available templates names can be found at https://github.com/elyby/emails-renderer
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
abstract public function getTemplateName(): string;
|
abstract public function getTemplateName(): string;
|
||||||
|
|
||||||
|
final protected function getRenderer(): RendererInterface {
|
||||||
|
return $this->renderer;
|
||||||
|
}
|
||||||
|
|
||||||
final protected function getView() {
|
final protected function getView() {
|
||||||
return $this->getTemplateName();
|
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()
|
return $this->getMailer()
|
||||||
->compose()
|
->compose()
|
||||||
->setHtmlBody($this->render())
|
->setHtmlBody($this->render())
|
||||||
->setTo($this->getTo())
|
->setTo($for)
|
||||||
->setFrom($this->getFrom())
|
->setFrom($this->getFrom())
|
||||||
->setSubject($this->getSubject());
|
->setSubject($this->getSubject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws \common\emails\exceptions\CannotRenderEmailException
|
||||||
|
*/
|
||||||
private function render(): string {
|
private function render(): string {
|
||||||
return $this->getEmailRenderer()
|
try {
|
||||||
->getTemplate($this->getTemplateName())
|
return $this->getRenderer()->render($this->getTemplateName(), $this->getLocale(), $this->getParams());
|
||||||
->setLocale($this->getLocale())
|
} catch (Exception $e) {
|
||||||
->setParams($this->getParams())
|
throw new exceptions\CannotRenderEmailException($e);
|
||||||
->render();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
15
common/emails/exceptions/CannotRenderEmailException.php
Normal file
15
common/emails/exceptions/CannotRenderEmailException.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\exceptions;
|
namespace common\emails\exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class CannotSendEmailException extends Exception {
|
class CannotSendEmailException extends Exception {
|
||||||
|
|
||||||
|
public function __construct(Throwable $previous = null) {
|
||||||
|
parent::__construct('Unable send email', 0, $previous);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
use common\emails\Template;
|
use common\emails\Template;
|
||||||
|
use yii\base\InvalidCallException;
|
||||||
|
|
||||||
class ChangeEmailConfirmCurrentEmail extends Template {
|
class ChangeEmail extends Template {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
private $key;
|
private $key;
|
||||||
|
|
||||||
public function __construct($to, string $key) {
|
public function setKey(string $key): void {
|
||||||
parent::__construct($to);
|
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,14 +22,15 @@ class ChangeEmailConfirmCurrentEmail extends Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getParams(): array {
|
public function getParams(): array {
|
||||||
|
if ($this->key === null) {
|
||||||
|
throw new InvalidCallException('You need to set key param first');
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'key' => $this->key,
|
'key' => $this->key,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|array
|
|
||||||
*/
|
|
||||||
protected function getView() {
|
protected function getView() {
|
||||||
return [
|
return [
|
||||||
'html' => '@common/emails/views/current-email-confirmation-html',
|
'html' => '@common/emails/views/current-email-confirmation-html',
|
@ -1,17 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
use common\emails\Template;
|
use common\emails\Template;
|
||||||
|
use yii\base\InvalidCallException;
|
||||||
|
|
||||||
class ChangeEmailConfirmNewEmail extends Template {
|
class ConfirmNewEmail extends Template {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
private $username;
|
private $username;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
private $key;
|
private $key;
|
||||||
|
|
||||||
public function __construct($to, string $username, string $key) {
|
public function setUsername(string $username): void {
|
||||||
parent::__construct($to);
|
|
||||||
$this->username = $username;
|
$this->username = $username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setKey(string $key): void {
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,15 +31,16 @@ class ChangeEmailConfirmNewEmail extends Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getParams(): array {
|
public function getParams(): array {
|
||||||
|
if ($this->username === null || $this->key === null) {
|
||||||
|
throw new InvalidCallException('You need to set username and key params first');
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'key' => $this->key,
|
|
||||||
'username' => $this->username,
|
'username' => $this->username,
|
||||||
|
'key' => $this->key,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|array
|
|
||||||
*/
|
|
||||||
protected function getView() {
|
protected function getView() {
|
||||||
return [
|
return [
|
||||||
'html' => '@common/emails/views/new-email-confirmation-html',
|
'html' => '@common/emails/views/new-email-confirmation-html',
|
@ -1,19 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
use common\emails\TemplateWithRenderer;
|
use common\emails\TemplateWithRenderer;
|
||||||
|
use yii\base\InvalidCallException;
|
||||||
|
|
||||||
class ForgotPasswordEmail extends TemplateWithRenderer {
|
class ForgotPasswordEmail extends TemplateWithRenderer {
|
||||||
|
|
||||||
private $params;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @var ForgotPasswordParams|null
|
||||||
*/
|
*/
|
||||||
public function __construct($to, string $locale, ForgotPasswordParams $params) {
|
private $params;
|
||||||
TemplateWithRenderer::__construct($to, $locale);
|
|
||||||
$this->params = $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSubject(): string {
|
public function getSubject(): string {
|
||||||
return 'Ely.by Account forgot password';
|
return 'Ely.by Account forgot password';
|
||||||
@ -23,7 +21,15 @@ class ForgotPasswordEmail extends TemplateWithRenderer {
|
|||||||
return 'forgotPassword';
|
return 'forgotPassword';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setParams(ForgotPasswordParams $params): void {
|
||||||
|
$this->params = $params;
|
||||||
|
}
|
||||||
|
|
||||||
public function getParams(): array {
|
public function getParams(): array {
|
||||||
|
if ($this->params === null) {
|
||||||
|
throw new InvalidCallException('You need to set params first');
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'username' => $this->params->getUsername(),
|
'username' => $this->params->getUsername(),
|
||||||
'code' => $this->params->getCode(),
|
'code' => $this->params->getCode(),
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
class ForgotPasswordParams {
|
class ForgotPasswordParams {
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
use common\emails\TemplateWithRenderer;
|
use common\emails\TemplateWithRenderer;
|
||||||
|
use yii\base\InvalidCallException;
|
||||||
|
|
||||||
class RegistrationEmail extends TemplateWithRenderer {
|
class RegistrationEmail extends TemplateWithRenderer {
|
||||||
|
|
||||||
private $params;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @var RegistrationEmailParams|null
|
||||||
*/
|
*/
|
||||||
public function __construct($to, string $locale, RegistrationEmailParams $params) {
|
private $params;
|
||||||
TemplateWithRenderer::__construct($to, $locale);
|
|
||||||
$this->params = $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSubject(): string {
|
public function getSubject(): string {
|
||||||
return 'Ely.by Account registration';
|
return 'Ely.by Account registration';
|
||||||
@ -23,7 +21,15 @@ class RegistrationEmail extends TemplateWithRenderer {
|
|||||||
return 'register';
|
return 'register';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setParams(RegistrationEmailParams $params): void {
|
||||||
|
$this->params = $params;
|
||||||
|
}
|
||||||
|
|
||||||
public function getParams(): array {
|
public function getParams(): array {
|
||||||
|
if ($this->params === null) {
|
||||||
|
throw new InvalidCallException('You need to set params first');
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'username' => $this->params->getUsername(),
|
'username' => $this->params->getUsername(),
|
||||||
'code' => $this->params->getCode(),
|
'code' => $this->params->getCode(),
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\emails\templates;
|
namespace common\emails\templates;
|
||||||
|
|
||||||
class RegistrationEmailParams {
|
class RegistrationEmailParams {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tasks;
|
namespace common\tasks;
|
||||||
|
|
||||||
use common\emails\EmailHelper;
|
use common\emails\EmailHelper;
|
||||||
use common\emails\templates\ChangeEmailConfirmCurrentEmail;
|
use common\emails\templates\ChangeEmail;
|
||||||
use common\models\confirmations\CurrentEmailConfirmation;
|
use common\models\confirmations\CurrentEmailConfirmation;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\queue\RetryableJobInterface;
|
use yii\queue\RetryableJobInterface;
|
||||||
@ -25,22 +26,23 @@ class SendCurrentEmailConfirmation implements RetryableJobInterface {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTtr() {
|
public function getTtr(): int {
|
||||||
return 30;
|
return 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canRetry($attempt, $error) {
|
public function canRetry($attempt, $error): bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \yii\queue\Queue $queue
|
* @param \yii\queue\Queue $queue
|
||||||
|
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||||
*/
|
*/
|
||||||
public function execute($queue) {
|
public function execute($queue) {
|
||||||
Yii::$app->statsd->inc('queue.sendCurrentEmailConfirmation.attempt');
|
Yii::$app->statsd->inc('queue.sendCurrentEmailConfirmation.attempt');
|
||||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
$template = new ChangeEmail(Yii::$app->mailer);
|
||||||
$template = new ChangeEmailConfirmCurrentEmail($to, $this->code);
|
$template->setKey($this->code);
|
||||||
$template->send();
|
$template->send(EmailHelper::buildTo($this->username, $this->email));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tasks;
|
namespace common\tasks;
|
||||||
|
|
||||||
use common\emails\EmailHelper;
|
use common\emails\EmailHelper;
|
||||||
use common\emails\templates\ChangeEmailConfirmNewEmail;
|
use common\emails\templates\ConfirmNewEmail;
|
||||||
use common\models\confirmations\NewEmailConfirmation;
|
use common\models\confirmations\NewEmailConfirmation;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\queue\RetryableJobInterface;
|
use yii\queue\RetryableJobInterface;
|
||||||
@ -25,22 +26,24 @@ class SendNewEmailConfirmation implements RetryableJobInterface {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTtr() {
|
public function getTtr(): int {
|
||||||
return 30;
|
return 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canRetry($attempt, $error) {
|
public function canRetry($attempt, $error): bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \yii\queue\Queue $queue
|
* @param \yii\queue\Queue $queue
|
||||||
|
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||||
*/
|
*/
|
||||||
public function execute($queue) {
|
public function execute($queue) {
|
||||||
Yii::$app->statsd->inc('queue.sendNewEmailConfirmation.attempt');
|
Yii::$app->statsd->inc('queue.sendNewEmailConfirmation.attempt');
|
||||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
$template = new ConfirmNewEmail(Yii::$app->mailer);
|
||||||
$template = new ChangeEmailConfirmNewEmail($to, $this->username, $this->code);
|
$template->setKey($this->code);
|
||||||
$template->send();
|
$template->setUsername($this->username);
|
||||||
|
$template->send(EmailHelper::buildTo($this->username, $this->email));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tasks;
|
namespace common\tasks;
|
||||||
|
|
||||||
use common\emails\EmailHelper;
|
use common\emails\EmailHelper;
|
||||||
@ -34,11 +35,11 @@ class SendPasswordRecoveryEmail implements RetryableJobInterface {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTtr() {
|
public function getTtr(): int {
|
||||||
return 30;
|
return 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canRetry($attempt, $error) {
|
public function canRetry($attempt, $error): bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +49,10 @@ class SendPasswordRecoveryEmail implements RetryableJobInterface {
|
|||||||
*/
|
*/
|
||||||
public function execute($queue) {
|
public function execute($queue) {
|
||||||
Yii::$app->statsd->inc('queue.sendPasswordRecovery.attempt');
|
Yii::$app->statsd->inc('queue.sendPasswordRecovery.attempt');
|
||||||
$params = new ForgotPasswordParams($this->username, $this->code, $this->link);
|
$template = new ForgotPasswordEmail(Yii::$app->mailer, Yii::$app->emailsRenderer);
|
||||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
$template->setLocale($this->locale);
|
||||||
$template = new ForgotPasswordEmail($to, $this->locale, $params);
|
$template->setParams(new ForgotPasswordParams($this->username, $this->code, $this->link));
|
||||||
$template->send();
|
$template->send(EmailHelper::buildTo($this->username, $this->email));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tasks;
|
namespace common\tasks;
|
||||||
|
|
||||||
use common\emails\EmailHelper;
|
use common\emails\EmailHelper;
|
||||||
@ -34,11 +35,11 @@ class SendRegistrationEmail implements RetryableJobInterface {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTtr() {
|
public function getTtr(): int {
|
||||||
return 30;
|
return 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canRetry($attempt, $error) {
|
public function canRetry($attempt, $error): bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +49,10 @@ class SendRegistrationEmail implements RetryableJobInterface {
|
|||||||
*/
|
*/
|
||||||
public function execute($queue) {
|
public function execute($queue) {
|
||||||
Yii::$app->statsd->inc('queue.sendRegistrationEmail.attempt');
|
Yii::$app->statsd->inc('queue.sendRegistrationEmail.attempt');
|
||||||
$params = new RegistrationEmailParams($this->username, $this->code, $this->link);
|
$template = new RegistrationEmail(Yii::$app->mailer, Yii::$app->emailsRenderer);
|
||||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
$template->setLocale($this->locale);
|
||||||
$template = new RegistrationEmail($to, $this->locale, $params);
|
$template->setParams(new RegistrationEmailParams($this->username, $this->code, $this->link));
|
||||||
$template->send();
|
$template->send(EmailHelper::buildTo($this->username, $this->email));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
14
common/tests/_support/EmailsRenderer.php
Normal file
14
common/tests/_support/EmailsRenderer.php
Normal 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';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
58
common/tests/unit/components/EmailsRenderer/ApiTest.php
Normal file
58
common/tests/unit/components/EmailsRenderer/ApiTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,47 +1,73 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tests\unit\emails;
|
namespace common\tests\unit\emails;
|
||||||
|
|
||||||
|
use common\emails\exceptions\CannotSendEmailException;
|
||||||
use common\emails\Template;
|
use common\emails\Template;
|
||||||
use common\tests\_support\ProtectedCaller;
|
|
||||||
use common\tests\unit\TestCase;
|
use common\tests\unit\TestCase;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\mail\MailerInterface;
|
use yii\mail\MailerInterface;
|
||||||
use yii\mail\MessageInterface;
|
use yii\mail\MessageInterface;
|
||||||
|
|
||||||
class TemplateTest extends TestCase {
|
class TemplateTest extends TestCase {
|
||||||
use ProtectedCaller;
|
|
||||||
|
|
||||||
public function testConstructor() {
|
/**
|
||||||
/** @var Template|\Mockery\MockInterface $template */
|
* @var Template|\PHPUnit\Framework\MockObject\MockObject $template
|
||||||
$template = mock(Template::class, ['find-me'])->makePartial();
|
*/
|
||||||
$this->assertSame('find-me', $template->getTo());
|
private $template;
|
||||||
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
|
|
||||||
|
/**
|
||||||
|
* @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';
|
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() {
|
protected function _after() {
|
||||||
/** @var Template|\Mockery\MockInterface $template */
|
parent::_after();
|
||||||
$template = mock(Template::class)->makePartial();
|
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
|
||||||
$this->assertSame([], $template->getParams());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateMessage() {
|
private function runTestForSend(bool $sendResult) {
|
||||||
Yii::$app->params['fromEmail'] = 'from@ely.by';
|
$this->template->expects($this->once())->method('getSubject')->willReturn('mock-subject');
|
||||||
/** @var Template|\Mockery\MockInterface $template */
|
$this->template->expects($this->once())->method('getView')->willReturn('mock-view');
|
||||||
$template = mock(Template::class, [['to@ely.by' => 'To']])->makePartial();
|
|
||||||
$template->shouldReceive('getSubject')->andReturn('mock-subject');
|
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $message */
|
||||||
/** @var MessageInterface $message */
|
$message = $this->createMock(MessageInterface::class);
|
||||||
$message = $this->callProtected($template, 'createMessage');
|
$message->expects($this->once())->method('setTo')->with(['to@ely.by' => 'To'])->willReturnSelf();
|
||||||
$this->assertInstanceOf(MessageInterface::class, $message);
|
$message->expects($this->once())->method('setFrom')->with(['find-me' => 'Ely.by Accounts'])->willReturnSelf();
|
||||||
$this->assertSame(['to@ely.by' => 'To'], $message->getTo());
|
$message->expects($this->once())->method('setSubject')->with('mock-subject')->willReturnSelf();
|
||||||
$this->assertSame(['from@ely.by' => 'Ely.by Accounts'], $message->getFrom());
|
$message->expects($this->once())->method('send')->willReturn($sendResult);
|
||||||
$this->assertSame('mock-subject', $message->getSubject());
|
|
||||||
|
$this->mailer->expects($this->once())->method('compose')->with('mock-view', [])->willReturn($message);
|
||||||
|
|
||||||
|
$this->template->send(['to@ely.by' => 'To']);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,49 +1,103 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tests\unit\emails;
|
namespace common\tests\unit\emails;
|
||||||
|
|
||||||
use common\components\EmailRenderer;
|
use common\emails\exceptions\CannotRenderEmailException;
|
||||||
|
use common\emails\RendererInterface;
|
||||||
use common\emails\TemplateWithRenderer;
|
use common\emails\TemplateWithRenderer;
|
||||||
use common\tests\_support\ProtectedCaller;
|
|
||||||
use common\tests\unit\TestCase;
|
use common\tests\unit\TestCase;
|
||||||
use Ely\Email\TemplateBuilder;
|
use Exception;
|
||||||
|
use Yii;
|
||||||
use yii\mail\MailerInterface;
|
use yii\mail\MailerInterface;
|
||||||
use yii\mail\MessageInterface;
|
use yii\mail\MessageInterface;
|
||||||
|
|
||||||
class TemplateWithRendererTest extends TestCase {
|
class TemplateWithRendererTest extends TestCase {
|
||||||
use ProtectedCaller;
|
|
||||||
|
|
||||||
public function testConstructor() {
|
/**
|
||||||
/** @var TemplateWithRenderer|\Mockery\MockInterface $template */
|
* @var TemplateWithRenderer|\PHPUnit\Framework\MockObject\MockObject $template
|
||||||
$template = mock(TemplateWithRenderer::class, ['mock-to', 'mock-locale'])->makePartial();
|
*/
|
||||||
$this->assertSame('mock-to', $template->getTo());
|
private $template;
|
||||||
$this->assertSame('mock-locale', $template->getLocale());
|
|
||||||
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
|
/**
|
||||||
$this->assertInstanceOf(EmailRenderer::class, $template->getEmailRenderer());
|
* @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() {
|
public function testSend() {
|
||||||
/** @var TemplateBuilder|\Mockery\MockInterface $templateBuilder */
|
$this->runTestForSend();
|
||||||
$templateBuilder = mock(TemplateBuilder::class)->makePartial();
|
}
|
||||||
$templateBuilder->shouldReceive('render')->andReturn('mock-html');
|
|
||||||
|
|
||||||
/** @var EmailRenderer|\Mockery\MockInterface $renderer */
|
public function testSendWithRenderError() {
|
||||||
$renderer = mock(EmailRenderer::class)->makePartial();
|
$renderException = new Exception('find me');
|
||||||
$renderer->shouldReceive('getTemplate')->with('mock-template')->andReturn($templateBuilder);
|
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 */
|
return;
|
||||||
$template = mock(TemplateWithRenderer::class, [['to@ely.by' => 'To'], 'mock-locale']);
|
}
|
||||||
$template->makePartial();
|
|
||||||
$template->shouldReceive('getEmailRenderer')->andReturn($renderer);
|
$this->assertFalse(true, 'no exception was thrown');
|
||||||
$template->shouldReceive('getFrom')->andReturn(['from@ely.by' => 'From']);
|
}
|
||||||
$template->shouldReceive('getSubject')->andReturn('mock-subject');
|
|
||||||
$template->shouldReceive('getTemplateName')->andReturn('mock-template');
|
protected function _before() {
|
||||||
/** @var \yii\swiftmailer\Message $message */
|
parent::_before();
|
||||||
$message = $this->callProtected($template, 'createMessage');
|
$this->mailer = $this->createMock(MailerInterface::class);
|
||||||
$this->assertInstanceOf(MessageInterface::class, $message);
|
$this->renderer = $this->createMock(RendererInterface::class);
|
||||||
$this->assertSame(['to@ely.by' => 'To'], $message->getTo());
|
$this->template = $this->getMockForAbstractClass(TemplateWithRenderer::class, [$this->mailer, $this->renderer]);
|
||||||
$this->assertSame(['from@ely.by' => 'From'], $message->getFrom());
|
$this->initialFromEmail = Yii::$app->params['fromEmail'];
|
||||||
$this->assertSame('mock-subject', $message->getSubject());
|
Yii::$app->params['fromEmail'] = 'find-me';
|
||||||
$this->assertSame('mock-html', $message->getSwiftMessage()->getBody());
|
}
|
||||||
|
|
||||||
|
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']);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
36
common/tests/unit/emails/templates/ChangeEmailTest.php
Normal file
36
common/tests/unit/emails/templates/ChangeEmailTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
common/tests/unit/emails/templates/ConfirmNewEmailTest.php
Normal file
49
common/tests/unit/emails/templates/ConfirmNewEmailTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
42
common/tests/unit/emails/templates/RegistrationEmailTest.php
Normal file
42
common/tests/unit/emails/templates/RegistrationEmailTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,14 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tests\unit\tasks;
|
namespace common\tests\unit\tasks;
|
||||||
|
|
||||||
|
use common\emails\RendererInterface;
|
||||||
use common\models\Account;
|
use common\models\Account;
|
||||||
use common\models\confirmations\ForgotPassword;
|
use common\models\confirmations\ForgotPassword;
|
||||||
use common\tasks\SendPasswordRecoveryEmail;
|
use common\tasks\SendPasswordRecoveryEmail;
|
||||||
use common\tests\unit\TestCase;
|
use common\tests\unit\TestCase;
|
||||||
|
use Yii;
|
||||||
use yii\queue\Queue;
|
use yii\queue\Queue;
|
||||||
|
|
||||||
class SendPasswordRecoveryEmailTest extends TestCase {
|
class SendPasswordRecoveryEmailTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
|
||||||
|
*/
|
||||||
|
private $renderer;
|
||||||
|
|
||||||
public function testCreateFromConfirmation() {
|
public function testCreateFromConfirmation() {
|
||||||
$account = new Account();
|
$account = new Account();
|
||||||
$account->username = 'mock-username';
|
$account->username = 'mock-username';
|
||||||
@ -21,7 +30,6 @@ class SendPasswordRecoveryEmailTest extends TestCase {
|
|||||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||||
|
|
||||||
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
|
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
|
||||||
$this->assertInstanceOf(SendPasswordRecoveryEmail::class, $result);
|
|
||||||
$this->assertSame('mock-username', $result->username);
|
$this->assertSame('mock-username', $result->username);
|
||||||
$this->assertSame('mock@ely.by', $result->email);
|
$this->assertSame('mock@ely.by', $result->email);
|
||||||
$this->assertSame('ABCDEFG', $result->code);
|
$this->assertSame('ABCDEFG', $result->code);
|
||||||
@ -37,6 +45,12 @@ class SendPasswordRecoveryEmailTest extends TestCase {
|
|||||||
$task->link = 'https://account.ely.by/recover-password/ABCDEFG';
|
$task->link = 'https://account.ely.by/recover-password/ABCDEFG';
|
||||||
$task->locale = 'ru';
|
$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));
|
$task->execute(mock(Queue::class));
|
||||||
|
|
||||||
$this->tester->canSeeEmailIsSent(1);
|
$this->tester->canSeeEmailIsSent(1);
|
||||||
@ -44,10 +58,14 @@ class SendPasswordRecoveryEmailTest extends TestCase {
|
|||||||
$email = $this->tester->grabSentEmails()[0];
|
$email = $this->tester->grabSentEmails()[0];
|
||||||
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
|
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
|
||||||
$this->assertSame('Ely.by Account forgot password', $email->getSubject());
|
$this->assertSame('Ely.by Account forgot password', $email->getSubject());
|
||||||
$body = $email->getSwiftMessage()->getBody();
|
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
|
||||||
$this->assertContains('Привет, mock-username', $body);
|
}
|
||||||
$this->assertContains('GFEDCBA', $body);
|
|
||||||
$this->assertContains('https://account.ely.by/recover-password/ABCDEFG', $body);
|
protected function _before() {
|
||||||
|
parent::_before();
|
||||||
|
|
||||||
|
$this->renderer = $this->createMock(RendererInterface::class);
|
||||||
|
Yii::$app->set('emailsRenderer', $this->renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\tests\unit\tasks;
|
namespace common\tests\unit\tasks;
|
||||||
|
|
||||||
|
use common\emails\RendererInterface;
|
||||||
use common\models\Account;
|
use common\models\Account;
|
||||||
use common\models\confirmations\RegistrationConfirmation;
|
use common\models\confirmations\RegistrationConfirmation;
|
||||||
use common\tasks\SendRegistrationEmail;
|
use common\tasks\SendRegistrationEmail;
|
||||||
use common\tests\unit\TestCase;
|
use common\tests\unit\TestCase;
|
||||||
|
use Yii;
|
||||||
use yii\queue\Queue;
|
use yii\queue\Queue;
|
||||||
|
|
||||||
class SendRegistrationEmailTest extends TestCase {
|
class SendRegistrationEmailTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
|
||||||
|
*/
|
||||||
|
private $renderer;
|
||||||
|
|
||||||
public function testCreateFromConfirmation() {
|
public function testCreateFromConfirmation() {
|
||||||
$account = new Account();
|
$account = new Account();
|
||||||
$account->username = 'mock-username';
|
$account->username = 'mock-username';
|
||||||
@ -21,7 +30,6 @@ class SendRegistrationEmailTest extends TestCase {
|
|||||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||||
|
|
||||||
$result = SendRegistrationEmail::createFromConfirmation($confirmation);
|
$result = SendRegistrationEmail::createFromConfirmation($confirmation);
|
||||||
$this->assertInstanceOf(SendRegistrationEmail::class, $result);
|
|
||||||
$this->assertSame('mock-username', $result->username);
|
$this->assertSame('mock-username', $result->username);
|
||||||
$this->assertSame('mock@ely.by', $result->email);
|
$this->assertSame('mock@ely.by', $result->email);
|
||||||
$this->assertSame('ABCDEFG', $result->code);
|
$this->assertSame('ABCDEFG', $result->code);
|
||||||
@ -37,17 +45,27 @@ class SendRegistrationEmailTest extends TestCase {
|
|||||||
$task->link = 'https://account.ely.by/activation/ABCDEFG';
|
$task->link = 'https://account.ely.by/activation/ABCDEFG';
|
||||||
$task->locale = 'ru';
|
$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);
|
$this->tester->canSeeEmailIsSent(1);
|
||||||
/** @var \yii\swiftmailer\Message $email */
|
/** @var \yii\swiftmailer\Message $email */
|
||||||
$email = $this->tester->grabSentEmails()[0];
|
$email = $this->tester->grabSentEmails()[0];
|
||||||
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
|
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
|
||||||
$this->assertSame('Ely.by Account registration', $email->getSubject());
|
$this->assertSame('Ely.by Account registration', $email->getSubject());
|
||||||
$body = $email->getSwiftMessage()->getBody();
|
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
|
||||||
$this->assertContains('Привет, mock-username', $body);
|
}
|
||||||
$this->assertContains('GFEDCBA', $body);
|
|
||||||
$this->assertContains('https://account.ely.by/activation/ABCDEFG', $body);
|
protected function _before() {
|
||||||
|
parent::_before();
|
||||||
|
|
||||||
|
$this->renderer = $this->createMock(RendererInterface::class);
|
||||||
|
Yii::$app->set('emailsRenderer', $this->renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
"ext-simplexml": "*",
|
"ext-simplexml": "*",
|
||||||
"bacon/bacon-qr-code": "^1.0",
|
"bacon/bacon-qr-code": "^1.0",
|
||||||
"domnikl/statsd": "^2.6",
|
"domnikl/statsd": "^2.6",
|
||||||
"ely/email-renderer": "dev-master",
|
|
||||||
"ely/mojang-api": "^0.2.0",
|
"ely/mojang-api": "^0.2.0",
|
||||||
"ely/yii2-tempmail-validator": "^2.0",
|
"ely/yii2-tempmail-validator": "^2.0",
|
||||||
"emarref/jwt": "~1.0.3",
|
"emarref/jwt": "~1.0.3",
|
||||||
@ -52,10 +51,6 @@
|
|||||||
{
|
{
|
||||||
"type": "composer",
|
"type": "composer",
|
||||||
"url": "https://asset-packagist.org"
|
"url": "https://asset-packagist.org"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://gitlab+deploy-token-1:FDGgmcnLdykcsyJJ_8Uv@gitlab.ely.by/elyby/email-renderer.git"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"config": {
|
"config": {
|
||||||
|
45
composer.lock
generated
45
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "68086c7aa86b976c390b02cc53b182f9",
|
"content-hash": "752322170e3433ba42cb90c1dd6122a2",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "bacon/bacon-qr-code",
|
"name": "bacon/bacon-qr-code",
|
||||||
@ -166,7 +166,7 @@
|
|||||||
"version": "3.27.0",
|
"version": "3.27.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git@github.com:getsentry/raven-js-bower.git",
|
"url": "https://github.com/getsentry/raven-js-bower.git",
|
||||||
"reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235"
|
"reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
@ -606,46 +606,6 @@
|
|||||||
],
|
],
|
||||||
"time": "2018-12-04T22:38:24+00:00"
|
"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",
|
"name": "ely/mojang-api",
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
@ -6180,7 +6140,6 @@
|
|||||||
"aliases": [],
|
"aliases": [],
|
||||||
"minimum-stability": "stable",
|
"minimum-stability": "stable",
|
||||||
"stability-flags": {
|
"stability-flags": {
|
||||||
"ely/email-renderer": 20,
|
|
||||||
"roave/security-advisories": 20
|
"roave/security-advisories": 20
|
||||||
},
|
},
|
||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
|
@ -9,6 +9,7 @@ services:
|
|||||||
build_env: dev
|
build_env: dev
|
||||||
depends_on:
|
depends_on:
|
||||||
- app
|
- app
|
||||||
|
- emails-renderer
|
||||||
env_file: .env
|
env_file: .env
|
||||||
volumes:
|
volumes:
|
||||||
- ./:/var/www/html
|
- ./:/var/www/html
|
||||||
@ -62,6 +63,9 @@ services:
|
|||||||
- ./:/var/www/html
|
- ./:/var/www/html
|
||||||
env_file: .env
|
env_file: .env
|
||||||
|
|
||||||
|
emails-renderer:
|
||||||
|
image: elyby/emails-renderer:dev
|
||||||
|
|
||||||
db:
|
db:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
|
@ -11,6 +11,7 @@ services:
|
|||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
- app
|
- app
|
||||||
|
- emails-renderer
|
||||||
env_file: .env
|
env_file: .env
|
||||||
volumes:
|
volumes:
|
||||||
- ./frontend:/var/www/html/frontend
|
- ./frontend:/var/www/html/frontend
|
||||||
@ -53,6 +54,10 @@ services:
|
|||||||
env_file: .env
|
env_file: .env
|
||||||
logging: *default-logging
|
logging: *default-logging
|
||||||
|
|
||||||
|
emails-renderer:
|
||||||
|
image: elyby/emails-renderer:dev
|
||||||
|
logging: *default-logging
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: registry.ely.by/elyby/accounts-mariadb:latest
|
image: registry.ely.by/elyby/accounts-mariadb:latest
|
||||||
restart: always
|
restart: always
|
||||||
|
@ -41,13 +41,14 @@ server {
|
|||||||
try_files $uri /index.html =404;
|
try_files $uri /index.html =404;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /api {
|
location /images/emails/assets {
|
||||||
try_files $uri /api/index.php$is_args$args;
|
proxy_pass http://emails-renderer:3000/assets/;
|
||||||
|
expires $cache_duration;
|
||||||
|
access_log off;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /images/emails/assets {
|
location /api {
|
||||||
alias '${root_path}/vendor/ely/email-renderer/dist/assets';
|
try_files $uri /api/index.php$is_args$args;
|
||||||
access_log off;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
location ~* \.php$ {
|
location ~* \.php$ {
|
||||||
|
Loading…
Reference in New Issue
Block a user