Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
1aafa73e
Commit
1aafa73e
authored
Nov 07, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Render methods removed from 'yii\mail\MessageInterface'.
Method 'yii\mail\MailerInterface::compose()' reworked allowing rendering message body.
parent
ad7761f9
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
87 additions
and
200 deletions
+87
-200
Mailer.php
extensions/swiftmailer/yii/swiftmailer/Mailer.php
+1
-1
BaseMailer.php
framework/yii/mail/BaseMailer.php
+30
-28
BaseMessage.php
framework/yii/mail/BaseMessage.php
+0
-48
MailerInterface.php
framework/yii/mail/MailerInterface.php
+10
-3
MessageInterface.php
framework/yii/mail/MessageInterface.php
+0
-43
MessageTest.php
tests/unit/extensions/swiftmailer/MessageTest.php
+1
-1
BaseMailerTest.php
tests/unit/framework/mail/BaseMailerTest.php
+35
-35
BaseMessageTest.php
tests/unit/framework/mail/BaseMessageTest.php
+10
-41
No files found.
extensions/swiftmailer/yii/swiftmailer/Mailer.php
View file @
1aafa73e
...
...
@@ -36,7 +36,7 @@ use Yii;
*
* @see http://swiftmailer.org
*
* @method Message
message(array $config = []) creates new message instance from given configuration
.
* @method Message
compose($view = null, array $params = []) creates new message optionally filling up its body via view rendering
.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
...
...
framework/yii/mail/BaseMailer.php
View file @
1aafa73e
...
...
@@ -33,7 +33,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
/**
* @var string directory containing view files for this email messages.
*/
public
$viewPath
=
'@app/mail
view
s'
;
public
$viewPath
=
'@app/mails'
;
/**
* @var string HTML layout view name.
*/
...
...
@@ -54,17 +54,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* - 'subject' argument for [[MessageInterface::subject()]]
* - 'text' argument for [[MessageInterface::text()]]
* - 'html' argument for [[MessageInterface::html()]]
* - 'body' argument for [[MessageInterface::body()]]
* - 'renderText' list of arguments for [[MessageInterface::renderText()]]
* - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]]
* - 'renderBody' list of arguments for [[MessageInterface::renderBody()]]
* For example:
* ~~~
* array(
* 'charset' => 'UTF-8',
* 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com',
* 'renderText' => ['default/text', ['companyName' => 'YiiApp']],
* 'bcc' => 'developer@mydomain.com',
* )
* ~~~
*/
...
...
@@ -111,16 +106,37 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
}
/**
* Creates new message instance from given configuration.
* Message configuration will be merged with [[messageConfig]].
* If 'class' parameter is omitted [[messageClass]], will be used.
* @param array $config message configuration. See [[messageConfig]]
* for the configuration format details.
* @inheritdoc
*/
public
function
compose
(
$view
=
null
,
array
$params
=
[])
{
$message
=
$this
->
createMessage
();
if
(
$view
!==
null
)
{
$params
[
'message'
]
=
$message
;
if
(
is_array
(
$view
))
{
if
(
array_key_exists
(
'html'
,
$view
))
{
$message
->
html
(
$this
->
render
(
$view
[
'html'
],
$params
,
$this
->
htmlLayout
));
}
if
(
array_key_exists
(
'text'
,
$view
))
{
$message
->
text
(
$this
->
render
(
$view
[
'text'
],
$params
,
$this
->
textLayout
));
}
}
else
{
$html
=
$this
->
render
(
$view
,
$params
,
$this
->
htmlLayout
);
$message
->
html
(
$html
);
$message
->
text
(
strip_tags
(
$html
));
}
}
return
$message
;
}
/**
* Creates mew message instance using configuration from [[messageConfig]].
* If 'class' parameter is omitted, [[messageClass]] will be used.
* @return MessageInterface message instance.
*/
p
ublic
function
message
(
array
$config
=
[]
)
p
rotected
function
createMessage
(
)
{
$config
=
array_merge
(
$this
->
messageConfig
,
$config
)
;
$config
=
$this
->
messageConfig
;
if
(
!
array_key_exists
(
'class'
,
$config
))
{
$config
[
'class'
]
=
$this
->
messageClass
;
}
...
...
@@ -133,32 +149,18 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
'subject'
,
'text'
,
'html'
,
'body'
,
];
$setupMethodNames
=
[
'renderText'
,
'renderHtml'
,
'renderBody'
,
];
$directSetterConfig
=
[];
$setupMethodConfig
=
[];
foreach
(
$config
as
$name
=>
$value
)
{
if
(
in_array
(
$name
,
$directSetterNames
,
true
))
{
$directSetterConfig
[
$name
]
=
$value
;
unset
(
$config
[
$name
]);
}
if
(
in_array
(
$name
,
$setupMethodNames
,
true
))
{
$setupMethodConfig
[
$name
]
=
$value
;
unset
(
$config
[
$name
]);
}
}
$message
=
Yii
::
createObject
(
$config
);
foreach
(
$directSetterConfig
as
$name
=>
$value
)
{
$message
->
$name
(
$value
);
}
foreach
(
$setupMethodConfig
as
$method
=>
$arguments
)
{
call_user_func_array
(
array
(
$message
,
$method
),
$arguments
);
}
return
$message
;
}
...
...
framework/yii/mail/BaseMessage.php
View file @
1aafa73e
...
...
@@ -44,54 +44,6 @@ abstract class BaseMessage extends Object implements MessageInterface
}
/**
* @inheritdoc
*/
public
function
body
(
$body
)
{
if
(
is_array
(
$body
))
{
$this
->
html
(
$body
[
'html'
]);
$this
->
text
(
$body
[
'text'
]);
}
else
{
$this
->
html
(
$body
);
$this
->
text
(
strip_tags
(
$body
));
}
return
$this
;
}
/**
* @inheritdoc
*/
public
function
renderHtml
(
$view
,
$params
=
[])
{
$this
->
html
(
$this
->
getMailer
()
->
render
(
$view
,
$params
,
$this
->
getMailer
()
->
htmlLayout
));
return
$this
;
}
/**
* @inheritdoc
*/
public
function
renderText
(
$view
,
$params
=
[])
{
$this
->
text
(
$this
->
getMailer
()
->
render
(
$view
,
$params
,
$this
->
getMailer
()
->
textLayout
));
return
$this
;
}
/**
* @inheritdoc
*/
public
function
renderBody
(
$view
,
$params
=
[])
{
if
(
is_array
(
$view
))
{
$this
->
renderHtml
(
$view
[
'html'
],
$params
);
$this
->
renderText
(
$view
[
'text'
],
$params
);
}
else
{
$html
=
$this
->
getMailer
()
->
render
(
$view
,
$params
,
$this
->
getMailer
()
->
htmlLayout
);
$this
->
body
(
$html
);
}
return
$this
;
}
/**
* PHP magic method that returns the string representation of this object.
* @return string the string representation of this object.
*/
...
...
framework/yii/mail/MailerInterface.php
View file @
1aafa73e
...
...
@@ -18,11 +18,18 @@ namespace yii\mail;
interface
MailerInterface
{
/**
* Creates new message instance from given configuration.
* @param array $config message configuration.
* Creates new message optionally filling up its body via view rendering.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string|array $view view, which should be used to render message body
* - if string - the view name or the path alias of the HTML body view file, in this case
* text body will be composed automatically from html one.
* - if array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView']
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return MessageInterface message instance.
*/
public
function
message
(
array
$config
=
[]);
public
function
compose
(
$view
=
null
,
array
$params
=
[]);
/**
* Sends the given email message.
...
...
framework/yii/mail/MessageInterface.php
View file @
1aafa73e
...
...
@@ -97,16 +97,6 @@ interface MessageInterface
public
function
html
(
$html
);
/**
* Sets message HTML and plain text content.
* @param string|array $body varies method behavior depending on type:
* - string - the HTML body content, in this case text body will be composed from
* html one using [[strip_tags()]] function.
* - array - list of body contents for each body type in format: ['html' => 'htmlContent', 'text' => 'textContent']
* @return static self reference.
*/
public
function
body
(
$body
);
/**
* Attaches existing file to the email message.
* @param string $fileName full file name
* @param array $options options for embed file. Valid options are:
...
...
@@ -155,39 +145,6 @@ interface MessageInterface
public
function
send
();
/**
* Fills up HTML body rendering a view.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string $view the view name or the path alias of the view file.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public
function
renderHtml
(
$view
,
$params
=
[]);
/**
* Fills up plain text body rendering a view.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string $view the view name or the path alias of the view file.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public
function
renderText
(
$view
,
$params
=
[]);
/**
* Composes the message HTML and plain text body.
* @param string|array $view varies method behavior depending on type:
* - string - the view name or the path alias of the HTML body view file, in this case
* text body will be composed from html one using [[strip_tags()]] function.
* - array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView']
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public
function
renderBody
(
$view
,
$params
=
[]);
/**
* Returns string representation of this message.
* @return string the string representation of this message.
*/
...
...
tests/unit/extensions/swiftmailer/MessageTest.php
View file @
1aafa73e
...
...
@@ -63,7 +63,7 @@ class MessageTest extends VendorTestCase
*/
protected
function
createTestMessage
()
{
return
Yii
::
$app
->
getComponent
(
'mail'
)
->
messag
e
();
return
Yii
::
$app
->
getComponent
(
'mail'
)
->
compos
e
();
}
/**
...
...
tests/unit/framework/mail/BaseMailerTest.php
View file @
1aafa73e
...
...
@@ -93,26 +93,16 @@ class BaseMailerTest extends TestCase
$this
->
assertTrue
(
is_object
(
$view
),
'Unable to get default view!'
);
}
public
function
testC
ompos
eMessage
()
public
function
testC
reat
eMessage
()
{
$mailer
=
new
Mailer
();
$message
=
$mailer
->
messag
e
();
$message
=
$mailer
->
compos
e
();
$this
->
assertTrue
(
is_object
(
$message
),
'Unable to create message instance!'
);
$this
->
assertEquals
(
$mailer
->
messageClass
,
get_class
(
$message
),
'Invalid message class!'
);
$messageConfig
=
array
(
'id'
=>
'test-id'
,
'encoding'
=>
'test-encoding'
,
);
$message
=
$mailer
->
message
(
$messageConfig
);
foreach
(
$messageConfig
as
$name
=>
$value
)
{
$this
->
assertEquals
(
$value
,
$message
->
$name
,
'Unable to apply message config!'
);
}
}
/**
* @depends testC
ompos
eMessage
* @depends testC
reat
eMessage
*/
public
function
testDefaultMessageConfig
()
{
...
...
@@ -135,7 +125,7 @@ class BaseMailerTest extends TestCase
$messageConfig
=
array_merge
(
$notPropertyConfig
,
$propertyConfig
);
$mailer
->
messageConfig
=
$messageConfig
;
$message
=
$mailer
->
messag
e
();
$message
=
$mailer
->
compos
e
();
foreach
(
$notPropertyConfig
as
$name
=>
$value
)
{
$this
->
assertEquals
(
$value
,
$message
->
{
'_'
.
$name
});
...
...
@@ -165,48 +155,58 @@ class BaseMailerTest extends TestCase
}
/**
* @depends testComposeMessage
* @depends testRender
*/
public
function
test
ComposeSetupMethods
()
public
function
test
RenderLayout
()
{
$mailer
=
$this
->
getTestMailComponent
();
$mailer
->
textLayout
=
false
;
$filePath
=
$this
->
getTestFilePath
();
$viewName
=
'test_view'
;
$viewFileName
=
$
this
->
getTestFilePath
()
.
DIRECTORY_SEPARATOR
.
$viewName
.
'.php'
;
$viewFileName
=
$
filePath
.
DIRECTORY_SEPARATOR
.
$viewName
.
'.php'
;
$viewFileContent
=
'view file content'
;
file_put_contents
(
$viewFileName
,
$viewFileContent
);
$
messageConfig
=
array
(
'renderText'
=>
[
$viewName
],
)
;
$message
=
$mailer
->
message
(
$messageConfig
);
$
layoutName
=
'test_layout'
;
$layoutFileName
=
$filePath
.
DIRECTORY_SEPARATOR
.
$layoutName
.
'.php'
;
$layoutFileContent
=
'Begin Layout <?php echo $content; ?> End Layout'
;
file_put_contents
(
$layoutFileName
,
$layoutFileContent
);
$this
->
assertEquals
(
$viewFileContent
,
$message
->
_text
);
$renderResult
=
$mailer
->
render
(
$viewName
,
[],
$layoutName
);
$this
->
assertEquals
(
'Begin Layout '
.
$viewFileContent
.
' End Layout'
,
$renderResult
);
}
/**
* @depends testCreateMessage
* @depends testRender
*/
public
function
test
RenderLayout
()
public
function
test
Compose
()
{
$mailer
=
$this
->
getTestMailComponent
();
$mailer
->
htmlLayout
=
false
;
$mailer
->
textLayout
=
false
;
$filePath
=
$this
->
getTestFilePath
();
$htmlViewName
=
'test_html_view'
;
$htmlViewFileName
=
$this
->
getTestFilePath
()
.
DIRECTORY_SEPARATOR
.
$htmlViewName
.
'.php'
;
$htmlViewFileContent
=
'HTML <b>view file</b> content'
;
file_put_contents
(
$htmlViewFileName
,
$htmlViewFileContent
);
$
viewName
=
'tes
t_view'
;
$
viewFileName
=
$filePath
.
DIRECTORY_SEPARATOR
.
$v
iewName
.
'.php'
;
$
viewFileContent
=
'
view file content'
;
file_put_contents
(
$
viewFileName
,
$v
iewFileContent
);
$
textViewName
=
'test_tex
t_view'
;
$
textViewFileName
=
$this
->
getTestFilePath
()
.
DIRECTORY_SEPARATOR
.
$textV
iewName
.
'.php'
;
$
textViewFileContent
=
'Plain text
view file content'
;
file_put_contents
(
$
textViewFileName
,
$textV
iewFileContent
);
$layoutName
=
'test_layout'
;
$layoutFileName
=
$filePath
.
DIRECTORY_SEPARATOR
.
$layoutName
.
'.php'
;
$layoutFileContent
=
'Begin Layout <?php echo $content; ?> End Layout'
;
file_put_contents
(
$layoutFileName
,
$layoutFileContent
);
$message
=
$mailer
->
compose
([
'html'
=>
$htmlViewName
,
'text'
=>
$textViewName
,
]);
$this
->
assertEquals
(
$htmlViewFileContent
,
$message
->
_html
,
'Unable to render html!'
);
$this
->
assertEquals
(
$textViewFileContent
,
$message
->
_text
,
'Unable to render text!'
);
$renderResult
=
$mailer
->
render
(
$viewName
,
[],
$layoutName
);
$this
->
assertEquals
(
'Begin Layout '
.
$viewFileContent
.
' End Layout'
,
$renderResult
);
$message
=
$mailer
->
compose
(
$htmlViewName
);
$this
->
assertEquals
(
$htmlViewFileContent
,
$message
->
_html
,
'Unable to render html by direct view!'
);
$this
->
assertEquals
(
strip_tags
(
$htmlViewFileContent
),
$message
->
_text
,
'Unable to render text by direct view!'
);
}
}
...
...
tests/unit/framework/mail/BaseMessageTest.php
View file @
1aafa73e
...
...
@@ -40,52 +40,26 @@ class BaseMessageTest extends TestCase
// Tests :
public
function
test
Rend
er
()
public
function
test
GetMail
er
()
{
$mailer
=
$this
->
getMailer
();
$message
=
$mailer
->
message
();
$viewName
=
'test/text/view'
;
$message
->
renderText
(
$viewName
);
$expectedText
=
'view='
.
$viewName
.
' layout='
.
$mailer
->
textLayout
;
$this
->
assertEquals
(
$expectedText
,
$message
->
text
,
'Unable to render text!'
);
$viewName
=
'test/html/view'
;
$message
->
renderHtml
(
$viewName
);
$expectedHtml
=
'view='
.
$viewName
.
' layout='
.
$mailer
->
htmlLayout
;
$this
->
assertEquals
(
$expectedHtml
,
$message
->
html
,
'Unable to render html!'
);
$message
=
$mailer
->
compose
();
$this
->
assertEquals
(
$mailer
,
$message
->
getMailer
());
}
/**
* @depends testRender
*/
public
function
testComposeBody
()
public
function
testSend
()
{
$mailer
=
$this
->
getMailer
();
$message
=
$mailer
->
message
();
$viewName
=
'test/html/view'
;
$message
->
renderBody
(
$viewName
);
$expectedHtml
=
'view='
.
$viewName
.
' layout='
.
$mailer
->
htmlLayout
;
$this
->
assertEquals
(
$expectedHtml
,
$message
->
html
,
'Unable to compose html!'
);
$expectedText
=
strip_tags
(
$expectedHtml
);
$this
->
assertEquals
(
$expectedText
,
$message
->
text
,
'Unable to compose text from html!'
);
$textViewName
=
'test/text/view'
;
$htmlViewName
=
'test/html/view'
;
$message
->
renderBody
([
'text'
=>
$textViewName
,
'html'
=>
$htmlViewName
]);
$expectedHtml
=
'view='
.
$htmlViewName
.
' layout='
.
$mailer
->
htmlLayout
;
$this
->
assertEquals
(
$expectedHtml
,
$message
->
html
,
'Unable to compose html from separated view!'
);
$expectedText
=
'view='
.
$textViewName
.
' layout='
.
$mailer
->
textLayout
;
$this
->
assertEquals
(
$expectedText
,
$message
->
text
,
'Unable to compose text from separated view!'
);
$message
=
$mailer
->
compose
();
$message
->
send
();
$this
->
assertEquals
(
$message
,
$mailer
->
sentMessages
[
0
],
'Unable to send message!'
);
}
public
function
test
Send
()
public
function
test
ToString
()
{
$mailer
=
$this
->
getMailer
();
$message
=
$mailer
->
message
();
$message
->
send
();
$this
->
assertEquals
(
$message
,
$mailer
->
sentMessages
[
0
],
'Unable to send message!'
);
$message
=
$mailer
->
compose
();
$this
->
assertEquals
(
$message
->
toString
(),
''
.
$message
);
}
}
...
...
@@ -97,11 +71,6 @@ class TestMailer extends BaseMailer
public
$messageClass
=
'yiiunit\framework\mail\TestMessage'
;
public
$sentMessages
=
array
();
public
function
render
(
$view
,
$params
=
[],
$layout
=
false
)
{
return
'view='
.
$view
.
' layout='
.
$layout
;
}
public
function
send
(
$message
)
{
$this
->
sentMessages
[]
=
$message
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment